server/api: log queries in debug mode

This commit is contained in:
rr- 2016-05-08 19:41:35 +02:00
parent 7610761ec8
commit d813601d92
6 changed files with 53 additions and 13 deletions

View File

@ -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

View File

@ -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

View File

@ -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=[

View File

@ -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)

View File

@ -1,14 +1,36 @@
import sqlalchemy import sqlalchemy
from szurubooru import config from szurubooru import config
class QueryCounter(object):
_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=config.config['database']['schema'],
user=config.config['database']['user'],
password=config.config['database']['pass'],
host=config.config['database']['host'],
port=config.config['database']['port'],
name=config.config['database']['name']))
sqlalchemy.event.listen(
_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 # pylint: disable=invalid-name
_engine = sqlalchemy.create_engine( session = create_session()
'{schema}://{user}:{password}@{host}:{port}/{name}'.format( reset_query_count = QueryCounter.reset
schema=config.config['database']['schema'], get_query_count = QueryCounter.get
user=config.config['database']['user'],
password=config.config['database']['pass'],
host=config.config['database']['host'],
port=config.config['database']['port'],
name=config.config['database']['name']))
_session_maker = sqlalchemy.orm.sessionmaker(bind=_engine)
session = sqlalchemy.orm.scoped_session(_session_maker)

View File

@ -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())