middleware to log db queries
This commit is contained in:
parent
249f6f709e
commit
918c1fa17d
|
|
@ -1,5 +1,8 @@
|
||||||
"""Custom middlewares."""
|
"""Custom middlewares."""
|
||||||
|
import logging
|
||||||
|
|
||||||
from django.utils import translation, timezone
|
from django.utils import translation, timezone
|
||||||
|
from django.db import connection
|
||||||
|
|
||||||
from account.models import User
|
from account.models import User
|
||||||
from configuration.models import TranslationSettings
|
from configuration.models import TranslationSettings
|
||||||
|
|
@ -7,6 +10,8 @@ from main.methods import determine_user_city
|
||||||
from main.models import SiteSettings
|
from main.models import SiteSettings
|
||||||
from translation.models import Language
|
from translation.models import Language
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def get_locale(cookie_dict):
|
def get_locale(cookie_dict):
|
||||||
return cookie_dict.get('locale')
|
return cookie_dict.get('locale')
|
||||||
|
|
@ -92,3 +97,26 @@ def user_last_ip(get_response):
|
||||||
return response
|
return response
|
||||||
|
|
||||||
return middleware
|
return middleware
|
||||||
|
|
||||||
|
|
||||||
|
def log_db_queries_per_API_request(get_response):
|
||||||
|
"""Middleware-helper to optimize requests performance"""
|
||||||
|
|
||||||
|
def middleware(request):
|
||||||
|
total_time = 0
|
||||||
|
response = get_response(request)
|
||||||
|
for query in connection.queries:
|
||||||
|
query_time = query.get('time')
|
||||||
|
if query_time is None:
|
||||||
|
query_time = query.get('duration', 0) / 1000
|
||||||
|
total_time += float(query_time)
|
||||||
|
|
||||||
|
total_queries = len(connection.queries)
|
||||||
|
if total_queries > 10:
|
||||||
|
logger.error(
|
||||||
|
f'\t{len(connection.queries)} queries run, total {total_time} seconds \t'
|
||||||
|
f'URL: "{request.method} {request.get_full_path_info()}\t"'
|
||||||
|
)
|
||||||
|
return response
|
||||||
|
|
||||||
|
return middleware
|
||||||
|
|
|
||||||
|
|
@ -81,3 +81,6 @@ EMAIL_HOST = 'smtp.gmail.com'
|
||||||
EMAIL_HOST_USER = 'anatolyfeteleu@gmail.com'
|
EMAIL_HOST_USER = 'anatolyfeteleu@gmail.com'
|
||||||
EMAIL_HOST_PASSWORD = 'nggrlnbehzksgmbt'
|
EMAIL_HOST_PASSWORD = 'nggrlnbehzksgmbt'
|
||||||
EMAIL_PORT = 587
|
EMAIL_PORT = 587
|
||||||
|
|
||||||
|
|
||||||
|
MIDDLEWARE.append('utils.middleware.log_db_queries_per_API_request')
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user