server/api: log queries in debug mode
This commit is contained in:
parent
7610761ec8
commit
d813601d92
|
@ -1,6 +1,7 @@
|
||||||
[basic]
|
[basic]
|
||||||
function-rgx=^_?[a-z_][a-z0-9_]{2,}$|^test_
|
function-rgx=^_?[a-z_][a-z0-9_]{2,}$|^test_
|
||||||
method-rgx=^[a-z_][a-z0-9_]{2,}$|^test_
|
method-rgx=^[a-z_][a-z0-9_]{2,}$|^test_
|
||||||
|
good-names=ex,_,logger
|
||||||
|
|
||||||
[variables]
|
[variables]
|
||||||
dummy-variables-rgx=_|dummy
|
dummy-variables-rgx=_|dummy
|
||||||
|
|
|
@ -6,3 +6,4 @@ SQLAlchemy>=1.0.12
|
||||||
pytest>=2.9.1
|
pytest>=2.9.1
|
||||||
pytest-cov>=2.2.1
|
pytest-cov>=2.2.1
|
||||||
freezegun>=0.3.6
|
freezegun>=0.3.6
|
||||||
|
coloredlogs==5.0
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
''' Exports create_app. '''
|
''' Exports create_app. '''
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import coloredlogs
|
||||||
import falcon
|
import falcon
|
||||||
from szurubooru import api, errors, middleware
|
from szurubooru import api, config, errors, middleware
|
||||||
|
|
||||||
def _on_auth_error(ex, _request, _response, _params):
|
def _on_auth_error(ex, _request, _response, _params):
|
||||||
raise falcon.HTTPForbidden(
|
raise falcon.HTTPForbidden(
|
||||||
|
@ -38,6 +40,11 @@ def create_app():
|
||||||
''' Create a WSGI compatible App object. '''
|
''' Create a WSGI compatible App object. '''
|
||||||
falcon.responders.create_method_not_allowed = create_method_not_allowed
|
falcon.responders.create_method_not_allowed = create_method_not_allowed
|
||||||
|
|
||||||
|
coloredlogs.install(fmt='[%(asctime)-15s] %(name)s %(message)s')
|
||||||
|
if config.config['debug']:
|
||||||
|
logging.getLogger('szurubooru').setLevel(logging.INFO)
|
||||||
|
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
|
||||||
|
|
||||||
app = falcon.API(
|
app = falcon.API(
|
||||||
request_type=api.Request,
|
request_type=api.Request,
|
||||||
middleware=[
|
middleware=[
|
||||||
|
|
|
@ -18,4 +18,7 @@ from szurubooru.db.comment import (
|
||||||
Comment,
|
Comment,
|
||||||
CommentScore)
|
CommentScore)
|
||||||
from szurubooru.db.snapshot import Snapshot
|
from szurubooru.db.snapshot import Snapshot
|
||||||
from szurubooru.db.session import session
|
from szurubooru.db.session import (
|
||||||
|
session,
|
||||||
|
reset_query_count,
|
||||||
|
get_query_count)
|
||||||
|
|
|
@ -1,8 +1,23 @@
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
from szurubooru import config
|
from szurubooru import config
|
||||||
|
|
||||||
# pylint: disable=invalid-name
|
class QueryCounter(object):
|
||||||
_engine = sqlalchemy.create_engine(
|
_query_count = 0
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def bump():
|
||||||
|
QueryCounter._query_count += 1
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def reset():
|
||||||
|
QueryCounter._query_count = 0
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get():
|
||||||
|
return QueryCounter._query_count
|
||||||
|
|
||||||
|
def create_session():
|
||||||
|
_engine = sqlalchemy.create_engine(
|
||||||
'{schema}://{user}:{password}@{host}:{port}/{name}'.format(
|
'{schema}://{user}:{password}@{host}:{port}/{name}'.format(
|
||||||
schema=config.config['database']['schema'],
|
schema=config.config['database']['schema'],
|
||||||
user=config.config['database']['user'],
|
user=config.config['database']['user'],
|
||||||
|
@ -10,5 +25,12 @@ _engine = sqlalchemy.create_engine(
|
||||||
host=config.config['database']['host'],
|
host=config.config['database']['host'],
|
||||||
port=config.config['database']['port'],
|
port=config.config['database']['port'],
|
||||||
name=config.config['database']['name']))
|
name=config.config['database']['name']))
|
||||||
_session_maker = sqlalchemy.orm.sessionmaker(bind=_engine)
|
sqlalchemy.event.listen(
|
||||||
session = sqlalchemy.orm.scoped_session(_session_maker)
|
_engine, 'after_execute', lambda *args: QueryCounter.bump())
|
||||||
|
_session_maker = sqlalchemy.orm.sessionmaker(bind=_engine)
|
||||||
|
return sqlalchemy.orm.scoped_session(_session_maker)
|
||||||
|
|
||||||
|
# pylint: disable=invalid-name
|
||||||
|
session = create_session()
|
||||||
|
reset_query_count = QueryCounter.reset
|
||||||
|
get_query_count = QueryCounter.get
|
||||||
|
|
|
@ -1,10 +1,16 @@
|
||||||
from szurubooru import db
|
import logging
|
||||||
|
from szurubooru import config, db
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class DbSession(object):
|
class DbSession(object):
|
||||||
''' Attaches database session to the context of every request. '''
|
''' Attaches database session to the context of every request. '''
|
||||||
|
|
||||||
def process_request(self, request, _response):
|
def process_request(self, request, _response):
|
||||||
request.context.session = db.session()
|
request.context.session = db.session()
|
||||||
|
db.reset_query_count()
|
||||||
|
|
||||||
def process_response(self, _request, _response, _resource):
|
def process_response(self, _request, _response, _resource):
|
||||||
db.session.remove()
|
db.session.remove()
|
||||||
|
if config.config['debug']:
|
||||||
|
logger.info('Executed %d queries', db.get_query_count())
|
||||||
|
|
Loading…
Reference in New Issue