server/api: log queries in debug mode
This commit is contained in:
parent
7610761ec8
commit
d813601d92
6 changed files with 53 additions and 13 deletions
|
@ -1,6 +1,7 @@
|
|||
[basic]
|
||||
function-rgx=^_?[a-z_][a-z0-9_]{2,}$|^test_
|
||||
method-rgx=^[a-z_][a-z0-9_]{2,}$|^test_
|
||||
good-names=ex,_,logger
|
||||
|
||||
[variables]
|
||||
dummy-variables-rgx=_|dummy
|
||||
|
|
|
@ -6,3 +6,4 @@ SQLAlchemy>=1.0.12
|
|||
pytest>=2.9.1
|
||||
pytest-cov>=2.2.1
|
||||
freezegun>=0.3.6
|
||||
coloredlogs==5.0
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
''' Exports create_app. '''
|
||||
|
||||
import logging
|
||||
import coloredlogs
|
||||
import falcon
|
||||
from szurubooru import api, errors, middleware
|
||||
from szurubooru import api, config, errors, middleware
|
||||
|
||||
def _on_auth_error(ex, _request, _response, _params):
|
||||
raise falcon.HTTPForbidden(
|
||||
|
@ -38,6 +40,11 @@ def create_app():
|
|||
''' Create a WSGI compatible App object. '''
|
||||
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(
|
||||
request_type=api.Request,
|
||||
middleware=[
|
||||
|
|
|
@ -18,4 +18,7 @@ from szurubooru.db.comment import (
|
|||
Comment,
|
||||
CommentScore)
|
||||
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,14 +1,36 @@
|
|||
import sqlalchemy
|
||||
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
|
||||
_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']))
|
||||
_session_maker = sqlalchemy.orm.sessionmaker(bind=_engine)
|
||||
session = sqlalchemy.orm.scoped_session(_session_maker)
|
||||
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):
|
||||
''' Attaches database session to the context of every request. '''
|
||||
|
||||
def process_request(self, request, _response):
|
||||
request.context.session = db.session()
|
||||
db.reset_query_count()
|
||||
|
||||
def process_response(self, _request, _response, _resource):
|
||||
db.session.remove()
|
||||
if config.config['debug']:
|
||||
logger.info('Executed %d queries', db.get_query_count())
|
||||
|
|
Loading…
Reference in a new issue