2019-06-24 12:16:18 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
import re
|
2019-09-11 10:52:42 +00:00
|
|
|
import os
|
2019-12-16 08:53:29 +00:00
|
|
|
import logging
|
2019-06-24 12:16:18 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
from django.conf import settings
|
2019-06-25 06:32:25 +00:00
|
|
|
from django.dispatch import receiver
|
2019-06-24 12:16:18 +00:00
|
|
|
from django.core.signals import request_finished
|
|
|
|
from django.db import connection
|
2019-12-05 07:09:25 +00:00
|
|
|
from django.conf import LazySettings
|
|
|
|
from django.db.utils import ProgrammingError, OperationalError
|
2019-06-24 12:16:18 +00:00
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
from jumpserver.utils import get_current_request
|
2019-06-24 12:16:18 +00:00
|
|
|
|
2019-06-25 06:32:25 +00:00
|
|
|
from .local import thread_local
|
2019-12-05 07:09:25 +00:00
|
|
|
from .signals import django_ready
|
2019-06-24 12:16:18 +00:00
|
|
|
|
|
|
|
pattern = re.compile(r'FROM `(\w+)`')
|
2019-12-16 08:53:29 +00:00
|
|
|
logger = logging.getLogger("jumpserver.common")
|
2019-09-12 09:58:52 +00:00
|
|
|
DEBUG_DB = os.environ.get('DEBUG_DB', '0') == '1'
|
2019-06-24 12:16:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Counter:
|
|
|
|
def __init__(self):
|
|
|
|
self.counter = 0
|
|
|
|
self.time = 0
|
|
|
|
|
|
|
|
def __gt__(self, other):
|
|
|
|
return self.counter > other.counter
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
return self.counter < other.counter
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.counter == other.counter
|
|
|
|
|
|
|
|
|
|
|
|
def on_request_finished_logging_db_query(sender, **kwargs):
|
|
|
|
queries = connection.queries
|
|
|
|
counters = defaultdict(Counter)
|
|
|
|
for query in queries:
|
2019-10-18 07:05:45 +00:00
|
|
|
if not query['sql'] or not query['sql'].startswith('SELECT'):
|
2019-06-24 12:16:18 +00:00
|
|
|
continue
|
|
|
|
tables = pattern.findall(query['sql'])
|
|
|
|
table_name = ''.join(tables)
|
|
|
|
time = query['time']
|
|
|
|
counters[table_name].counter += 1
|
|
|
|
counters[table_name].time += float(time)
|
|
|
|
counters['total'].counter += 1
|
|
|
|
counters['total'].time += float(time)
|
|
|
|
|
|
|
|
counters = sorted(counters.items(), key=lambda x: x[1])
|
2019-12-16 08:53:29 +00:00
|
|
|
if not counters:
|
|
|
|
return
|
|
|
|
method = 'GET'
|
|
|
|
path = '/Unknown'
|
|
|
|
current_request = get_current_request()
|
|
|
|
if current_request:
|
|
|
|
method = current_request.method
|
|
|
|
path = current_request.get_full_path()
|
|
|
|
logger.debug(">>> [{}] {}".format(method, path))
|
2019-06-24 12:16:18 +00:00
|
|
|
for name, counter in counters:
|
|
|
|
logger.debug("Query {:3} times using {:.2f}s {}".format(
|
|
|
|
counter.counter, counter.time, name)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-06-25 06:32:25 +00:00
|
|
|
def on_request_finished_release_local(sender, **kwargs):
|
|
|
|
thread_local.__release_local__()
|
|
|
|
|
|
|
|
|
2019-09-12 09:58:52 +00:00
|
|
|
if settings.DEBUG and DEBUG_DB:
|
2019-06-24 12:16:18 +00:00
|
|
|
request_finished.connect(on_request_finished_logging_db_query)
|
2019-12-16 08:53:29 +00:00
|
|
|
else:
|
|
|
|
request_finished.connect(on_request_finished_release_local)
|
2019-06-24 12:16:18 +00:00
|
|
|
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
@receiver(django_ready)
|
|
|
|
def monkey_patch_settings(sender, **kwargs):
|
|
|
|
def monkey_patch_getattr(self, name):
|
|
|
|
val = getattr(self._wrapped, name)
|
|
|
|
if callable(val):
|
|
|
|
val = val()
|
|
|
|
return val
|
2019-06-24 12:16:18 +00:00
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
try:
|
|
|
|
LazySettings.__getattr__ = monkey_patch_getattr
|
|
|
|
except (ProgrammingError, OperationalError):
|
|
|
|
pass
|