Skip to content

Commit

Permalink
Initial stab at djdt API
Browse files Browse the repository at this point in the history
  • Loading branch information
robhudson committed Jan 3, 2025
1 parent e1c0755 commit eb2973c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
4 changes: 4 additions & 0 deletions debug_toolbar/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ def get_urls(cls):
# Global URLs
urlpatterns = [
path("render_panel/", views.render_panel, name="render_panel"),
path("requests/", views.api_requests, name="api_requests"),
path(
"request/<str:request_id>/", views.api_request, name="api_request"
),
]
# Per-panel URLs
for panel_class in cls.get_panel_classes():
Expand Down
58 changes: 58 additions & 0 deletions debug_toolbar/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
from django.utils.translation import gettext as _

from debug_toolbar.decorators import render_with_toolbar_language, require_show_toolbar
from debug_toolbar.panels.history import HistoryPanel
from debug_toolbar.panels.sql import SQLPanel
from debug_toolbar.panels.timer import TimerPanel
from debug_toolbar.store import get_store
from debug_toolbar.toolbar import DebugToolbar


Expand All @@ -23,3 +27,57 @@ def render_panel(request):
content = panel.content
scripts = panel.scripts
return JsonResponse({"content": content, "scripts": scripts})


@require_show_toolbar
def api_requests(request):
"""Return a JSON representation of the requests in the store.
This always contains the `request_id`, but may include other meta data
depending on the panels that have been enabled.
"""
store = get_store()
if not store:
return JsonResponse({"requests": []})

requests = []
for request_id in reversed(store.request_ids()):
history_stats = store.panel(request_id, HistoryPanel.panel_id)
timing_stats = store.panel(request_id, TimerPanel.panel_id)
sql_stats = store.panel(request_id, SQLPanel.panel_id)
request = {
"request_id": request_id,
}
if history_stats:
request.update(history_stats)
if timing_stats:
request.update(
{
"total_time": timing_stats["total_time"],
}
)
if sql_stats:
request.update(
{
"sql_time": sql_stats["sql_time"],
"sql_queries": len(sql_stats["queries"]),
}
)
requests.append(request)

return JsonResponse({"requests": requests})


@require_show_toolbar
def api_request(request, request_id):
"""Return a JSON representation of all the stored data for the given `request_id`."""
store = get_store()
if not store:
return JsonResponse({"requests": []})

data = {}
for panel, panel_data in store.panels(request_id):
data[panel.removesuffix("Panel")] = panel_data

return JsonResponse({"data": data})

0 comments on commit eb2973c

Please sign in to comment.