From 6041b0da8317b3c8df2d1551f8282a9e59c336c3 Mon Sep 17 00:00:00 2001 From: David Wolfe Date: Sat, 8 Feb 2014 18:37:43 -0400 Subject: Color-code SQL query "Timeline" stripes according to stacktrace --- debug_toolbar/panels/sql/panel.py | 9 ++++++++- debug_toolbar/panels/sql/utils.py | 22 ++++++++++++++++++++++ .../templates/debug_toolbar/panels/sql.html | 2 +- 3 files changed, 31 insertions(+), 2 deletions(-) (limited to 'debug_toolbar') diff --git a/debug_toolbar/panels/sql/panel.py b/debug_toolbar/panels/sql/panel.py index 2534849..cae5214 100644 --- a/debug_toolbar/panels/sql/panel.py +++ b/debug_toolbar/panels/sql/panel.py @@ -10,7 +10,7 @@ from django.utils.translation import ugettext_lazy as _, ungettext_lazy as __ from debug_toolbar.panels import Panel from debug_toolbar.panels.sql.forms import SQLSelectForm from debug_toolbar.utils import render_stacktrace -from debug_toolbar.panels.sql.utils import reformat_sql +from debug_toolbar.panels.sql.utils import reformat_sql, contrasting_color_generator from debug_toolbar.panels.sql.tracking import wrap_cursor, unwrap_cursor @@ -136,6 +136,8 @@ class SQLPanel(Panel): unwrap_cursor(connection) def process_response(self, request, response): + colors = contrasting_color_generator() + trace_colors = {} if self._queries: width_ratio_tally = 0 factor = int(256.0 / (len(self._databases) * 2.5)) @@ -196,6 +198,11 @@ class SQLPanel(Panel): query['stacktrace'] = render_stacktrace(query['stacktrace']) i += 1 + if not trace_colors.get(query['stacktrace']): + c = colors.next() + trace_colors[query['stacktrace']] = c + query['trace_color'] = trace_colors[query['stacktrace']] + if trans_id: self._queries[(i - 1)][1]['ends_trans'] = True diff --git a/debug_toolbar/panels/sql/utils.py b/debug_toolbar/panels/sql/utils.py index 3943168..d6f0173 100644 --- a/debug_toolbar/panels/sql/utils.py +++ b/debug_toolbar/panels/sql/utils.py @@ -35,3 +35,25 @@ def swap_fields(sql): r'\1 ' r'FROM') return re.sub(expr, subs, sql) + + +def contrasting_color_generator(): + """ + Generate constrasting colors by varying most significant bit of RGB first, + and then vary subsebequent bits systematically. + """ + def rgb_to_hex(rgb): + return '#%02x%02x%02x' % tuple(rgb) + + triples = [(1, 0, 0), (0, 1, 0), (0, 0, 1), + (1, 1, 0), (0, 1, 1), (1, 0, 1), (1, 1, 1)] + n = 128 + so_far = [[0, 0, 0]] + while (1): + copy_so_far = list(so_far) + for triple in triples: + for previous in copy_so_far: + rgb = [n * triple[i] + previous[i] for i in range(3)] + so_far.append(rgb) + yield rgb_to_hex(rgb) + n = n / 2 diff --git a/debug_toolbar/templates/debug_toolbar/panels/sql.html b/debug_toolbar/templates/debug_toolbar/panels/sql.html index f927e5d..4d95a80 100644 --- a/debug_toolbar/templates/debug_toolbar/panels/sql.html +++ b/debug_toolbar/templates/debug_toolbar/panels/sql.html @@ -34,7 +34,7 @@ -
{{ query.width_ratio }}%
+
{{ query.width_ratio }}%
{{ query.duration|floatformat:"2" }} -- cgit v1.2.3