Skip to content

Commit 04ec6c1

Browse files
committed
Simplify debug panel code
1 parent 668fc76 commit 04ec6c1

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

flask_mongoengine/panels.py

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ def _maybe_patch_jinja_loader(jinja_env):
1818

1919

2020
class MongoDebugPanel(DebugPanel):
21-
"""Panel that shows information about MongoDB operations (including stack)
22-
23-
Adapted from https://github.com/hmarr/django-debug-toolbar-mongo
24-
"""
21+
"""Panel that shows information about MongoDB operations."""
2522

2623
name = "MongoDB"
2724
has_content = True
@@ -34,33 +31,38 @@ def __init__(self, *args, **kwargs):
3431
def process_request(self, request):
3532
operation_tracker.reset()
3633

37-
def nav_title(self):
38-
return "MongoDB"
39-
40-
def nav_subtitle(self):
41-
attrs = ["queries", "inserts", "updates", "removes"]
42-
ops = sum(
43-
sum((1 for o in getattr(operation_tracker, a) if not o["internal"]))
44-
for a in attrs
45-
)
46-
total_time = sum(
47-
sum(o["time"] for o in getattr(operation_tracker, a)) for a in attrs
48-
)
49-
return "{0} operations in {1:.2f}ms".format(ops, total_time)
50-
51-
def title(self):
34+
def nav_title(self) -> str:
35+
return self.name
36+
37+
def nav_subtitle(self) -> str:
38+
"""Count operations and total time, excluding any toolbar related operations."""
39+
total_time = 0
40+
operations_count = 0
41+
for query_type in {"queries", "inserts", "updates", "removes"}:
42+
for operation in getattr(operation_tracker, query_type):
43+
if operation.get("internal", False):
44+
continue
45+
46+
operations_count += 1
47+
total_time += operation.get("time", 0)
48+
49+
return "{0} operations in {1:.2f}ms".format(operations_count, total_time)
50+
51+
def title(self) -> str:
5252
return "MongoDB Operations"
5353

54-
def url(self):
54+
def url(self) -> str:
5555
return ""
5656

5757
def content(self):
58-
context = self.context.copy()
59-
context["queries"] = operation_tracker.queries
60-
context["inserts"] = operation_tracker.inserts
61-
context["updates"] = operation_tracker.updates
62-
context["removes"] = operation_tracker.removes
63-
context["slow_query_limit"] = current_app.config.get(
64-
"MONGO_DEBUG_PANEL_SLOW_QUERY_LIMIT", 100
65-
)
58+
"""Gather all template required variables in one dict."""
59+
context = {
60+
"queries": operation_tracker.queries,
61+
"inserts": operation_tracker.inserts,
62+
"updates": operation_tracker.updates,
63+
"removes": operation_tracker.removes,
64+
"slow_query_limit": current_app.config.get(
65+
"MONGO_DEBUG_PANEL_SLOW_QUERY_LIMIT", 100
66+
),
67+
}
6668
return self.render("panels/mongo-panel.html", context)

0 commit comments

Comments
 (0)