Skip to content

Commit 28e8568

Browse files
authored
Merge pull request #434 from MongoEngine/debug-panel-update
Debug panel source code simplified
2 parents 668fc76 + 93e2416 commit 28e8568

File tree

2 files changed

+32
-57
lines changed

2 files changed

+32
-57
lines changed

flask_mongoengine/operation_tracker.py

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"updates",
1919
"removes",
2020
"install_tracker",
21-
"uninstall_tracker",
2221
"reset",
2322
"response_sizes",
2423
]
@@ -486,33 +485,6 @@ def install_tracker():
486485
pymongo.command_cursor.CommandCursor._unpack_response = _unpack_response
487486

488487

489-
def uninstall_tracker():
490-
if pymongo.collection.Collection.insert == _insert:
491-
pymongo.collection.Collection.insert = _original_methods["insert"]
492-
if pymongo.collection.Collection.insert_one == _insert_one:
493-
pymongo.collection.Collection.insert_one = _original_methods["insert_one"]
494-
if pymongo.collection.Collection.insert_many == _insert_many:
495-
pymongo.collection.Collection.insert_many = _original_methods["insert_many"]
496-
if pymongo.collection.Collection.update == _update:
497-
pymongo.collection.Collection.update = _original_methods["update"]
498-
if pymongo.collection.Collection.update_one == _update_one:
499-
pymongo.collection.Collection.update_one = _original_methods["update_one"]
500-
if pymongo.collection.Collection.update_many == _update_many:
501-
pymongo.collection.Collection.update_many = _original_methods["update_many"]
502-
if pymongo.collection.Collection.remove == _remove:
503-
pymongo.collection.Collection.remove = _original_methods["remove"]
504-
if pymongo.collection.Collection.delete_one == _delete_one:
505-
pymongo.collection.Collection.delete_one = _original_methods["delete_one"]
506-
if pymongo.collection.Collection.delete_many == _delete_many:
507-
pymongo.collection.Collection.delete_many = _original_methods["delete_many"]
508-
if pymongo.cursor.Cursor._refresh == _cursor_refresh:
509-
pymongo.cursor.Cursor._refresh = _original_methods["cursor_refresh"]
510-
if pymongo.command_cursor.CommandCursor._unpack_response == _unpack_response:
511-
pymongo.command_cursor.CommandCursor._unpack_response = _original_methods[
512-
"_unpack_response"
513-
]
514-
515-
516488
def reset():
517489
global queries, inserts, updates, removes, response_sizes
518490
queries = []
@@ -548,7 +520,8 @@ def _tidy_stacktrace():
548520
fname = sys._getframe(i).f_code.co_filename
549521
if ".html" in fname:
550522
fnames.append(fname)
551-
except Exception:
523+
# Stack can have not enough frames
524+
except ValueError:
552525
break
553526
fnames = list(set(fnames))
554527
trace = []

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)