Skip to content

feat: Add colorization based on health level for panels and toolbar #2178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion debug_toolbar/panels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.utils.functional import classproperty

from debug_toolbar import settings as dt_settings
from debug_toolbar.utils import get_name_from_obj
from debug_toolbar.utils import ToolbarHealthLevel, get_name_from_obj


class Panel:
Expand Down Expand Up @@ -129,6 +129,19 @@ def scripts(self):
"""
return []

@property
def health_level(self):
"""
Returns the health level of the panel as a `ToolbarHealthLevel` enum value.

This property is used by the toolbar to determine the overall health status of each panel.
The default implementation returns `ToolbarHealthLevel.NONE`, indicating no issues.

Subclasses should override this property to provide custom health logic, returning
`ToolbarHealthLevel.WARNING` or `ToolbarHealthLevel.ERROR` as appropriate based on panel-specific conditions.
"""
return ToolbarHealthLevel.NONE

# Panel early initialization

@classmethod
Expand Down
12 changes: 11 additions & 1 deletion debug_toolbar/panels/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.utils.translation import gettext_lazy as _

from debug_toolbar.panels import Panel
from debug_toolbar.utils import is_processable_html_response
from debug_toolbar.utils import ToolbarHealthLevel, is_processable_html_response


class FormParser(HTMLParser):
Expand Down Expand Up @@ -92,6 +92,16 @@ def nav_subtitle(self):
else:
return ""

@property
def health_level(self):
"""
Return the health level of the panel based on the alerts.
"""
if not self.alerts:
return ToolbarHealthLevel.NONE

return ToolbarHealthLevel.CRITICAL

def add_alert(self, alert):
self.alerts.append(alert)

Expand Down
14 changes: 13 additions & 1 deletion debug_toolbar/panels/sql/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
is_select_query,
reformat_sql,
)
from debug_toolbar.utils import render_stacktrace
from debug_toolbar.utils import ToolbarHealthLevel, render_stacktrace


def get_isolation_level_display(vendor, level):
Expand Down Expand Up @@ -186,6 +186,18 @@ def title(self):
count,
) % {"count": count}

@property
def health_level(self):
""" """
stats = self.get_stats()
slow_queries = len([1 for q in stats.get("queries", []) if q.get("is_slow")])
if slow_queries > 10:
return ToolbarHealthLevel.CRITICAL
elif slow_queries > 0:
return ToolbarHealthLevel.WARNING

return super().health_level

template = "debug_toolbar/panels/sql.html"

@classmethod
Expand Down
33 changes: 33 additions & 0 deletions debug_toolbar/static/debug_toolbar/css/toolbar.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
--djdt-button-border-color: var(--djdt-table-border-color);
--djdt-pre-border-color: var(--djdt-table-border-color);
--djdt-raw-border-color: var(--djdt-table-border-color);

--djdt-health-bg-1: #4b3f1b;
--djdt-health-color-1: #ffe761;
--djdt-health-bc-1: #ffcc00;
--djdt-health-bg-2: #5a2327;
--djdt-health-color-2: #ffb3b3;
--djdt-health-bc-2: #ff0000;
}

#djDebug[data-theme="dark"] {
Expand All @@ -56,6 +63,13 @@
--djdt-button-border-color: var(--djdt-table-border-color);
--djdt-pre-border-color: var(--djdt-table-border-color);
--djdt-raw-border-color: var(--djdt-table-border-color);

--djdt-health-bg-1: #4b3f1b;
--djdt-health-color-1: #ffe761;
--djdt-health-bc-1: #ffcc00;
--djdt-health-bg-2: #5a2327;
--djdt-health-color-2: #ffb3b3;
--djdt-health-bc-2: #ff0000;
}

/* Debug Toolbar CSS Reset, adapted from Eric Meyer's CSS Reset */
Expand Down Expand Up @@ -377,6 +391,25 @@
animation: spin 2s linear infinite;
}

/* Panel and Toolbar hidden button health states */
#djDebug .djdt-health-1 {
background: var(--djdt-health-bg-1) !important;
color: var(--djdt-health-color-1) !important;
}

#djDebug .djdt-health-2 {
background: var(--djdt-health-bg-2) !important;
color: var(--djdt-health-color-2) !important;
}

#djDebug .djdt-toolbarhandle-health-1 {
border-color: var(--djdt-health-bc-1) !important;
}

#djDebug .djdt-toolbarhandle-health-2 {
border-color: var(--djdt-health-bc-2) !important;
}

@keyframes spin {
0% {
transform: rotate(0deg);
Expand Down
2 changes: 1 addition & 1 deletion debug_toolbar/templates/debug_toolbar/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</ul>
</div>
<div class="djdt-hidden" id="djDebugToolbarHandle">
<div title="{% translate 'Show toolbar' %}" id="djShowToolBarButton">
<div title="{% translate 'Show toolbar' %}" id="djShowToolBarButton" class="{% if toolbar.health_level %} djdt-toolbarhandle-health-{{ toolbar.health_level }}{% endif %}">
<span id="djShowToolBarD">D</span><span id="djShowToolBarJ">J</span>DT
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% load i18n %}

<li id="djdt-{{ panel.panel_id }}" class="djDebugPanelButton">
<li id="djdt-{{ panel.panel_id }}" class="djDebugPanelButton{% if panel.health_level %} djdt-health-{{ panel.health_level }}{% endif %}">
<input type="checkbox" data-cookie="djdt{{ panel.panel_id }}" {% if panel.enabled %}checked title="{% translate "Disable for next and successive requests" %}"{% else %}title="{% translate "Enable for next and successive requests" %}"{% endif %}>
{% if panel.has_content and panel.enabled %}
<a href="#" title="{{ panel.title }}" class="{{ panel.panel_id }}">
Expand Down
8 changes: 8 additions & 0 deletions debug_toolbar/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ def csp_nonce(self):
"""
return getattr(self.request, "csp_nonce", None)

@property
def health_level(self):
"""
Return the maximum health level across all panels.
This is used to color the toolbar hidden button.
"""
return max(panel.health_level for panel in self.panels)

def get_panel_by_id(self, panel_id):
"""
Get the panel with the given id, which is the class name by default.
Expand Down
7 changes: 7 additions & 0 deletions debug_toolbar/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
import warnings
from collections.abc import Sequence
from enum import IntEnum
from pprint import PrettyPrinter, pformat
from typing import Any

Expand Down Expand Up @@ -401,3 +402,9 @@ def is_processable_html_response(response):
and content_encoding == ""
and content_type in _HTML_TYPES
)


class ToolbarHealthLevel(IntEnum):
NONE = 0
WARNING = 1
CRITICAL = 2