-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpanel.py
More file actions
63 lines (52 loc) · 1.76 KB
/
panel.py
File metadata and controls
63 lines (52 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from django.db import connections
from django.utils.translation import gettext_lazy as _, ngettext
from debug_toolbar.panels.sql.panel import SQLPanel
from django_mongodb_extensions.debug_toolbar.panels.mql.tracking import (
patch_get_collection,
)
class MQLPanel(SQLPanel):
"""
Panel that displays information about the MQL queries run while processing
the request.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._sql_time = 0
self._queries = []
self._databases = {}
# Implement Panel API
nav_title = _("MQL")
template = "debug_toolbar/panels/mql.html"
@property
def nav_subtitle(self):
query_count = len(self._queries)
return ngettext(
"%(query_count)d query in %(sql_time).2fms",
"%(query_count)d queries in %(sql_time).2fms",
query_count,
) % {
"query_count": query_count,
"sql_time": self._sql_time,
}
@property
def title(self):
count = len(self._databases)
return ngettext(
"MQL queries from %(count)d connection",
"MQL queries from %(count)d connections",
count,
) % {"count": count}
def enable_instrumentation(self):
# This is thread-safe because database connections are thread-local.
for connection in connections.all():
patch_get_collection(connection)
connection._djdt_logger = self
def disable_instrumentation(self):
for connection in connections.all():
connection._djdt_logger = None
def generate_stats(self, request, response):
self.record_stats(
{
"queries": self._queries,
}
)