diff options
| author | Aymeric Augustin | 2014-02-12 08:31:54 +0100 |
|---|---|---|
| committer | Aymeric Augustin | 2014-02-12 08:31:54 +0100 |
| commit | 8c690d7cf0af9e1408e446f1f658b3db2b9fa572 (patch) | |
| tree | 2ab96881b2cd34eeec1694651a95980c4e8060f1 /debug_toolbar/panels/sql | |
| parent | 296a08939383e0c2e52e7632fe5e377a37a667d9 (diff) | |
| parent | 8d68d307af04166e7aa4fde915b95a41560b8f19 (diff) | |
| download | django-debug-toolbar-8c690d7cf0af9e1408e446f1f658b3db2b9fa572.tar.bz2 | |
Merge pull request #543 from wolfe/group-sql
Color-code SQL query "Timeline" stripes according to stacktrace
Diffstat (limited to 'debug_toolbar/panels/sql')
| -rw-r--r-- | debug_toolbar/panels/sql/panel.py | 7 | ||||
| -rw-r--r-- | debug_toolbar/panels/sql/utils.py | 24 |
2 files changed, 30 insertions, 1 deletions
diff --git a/debug_toolbar/panels/sql/panel.py b/debug_toolbar/panels/sql/panel.py index 2534849..f6ce954 100644 --- a/debug_toolbar/panels/sql/panel.py +++ b/debug_toolbar/panels/sql/panel.py @@ -2,6 +2,7 @@ from __future__ import absolute_import, unicode_literals import uuid from copy import copy +from collections import defaultdict from django.conf.urls import patterns, url from django.db import connections @@ -10,7 +11,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 +137,8 @@ class SQLPanel(Panel): unwrap_cursor(connection) def process_response(self, request, response): + colors = contrasting_color_generator() + trace_colors = defaultdict(lambda: next(colors)) if self._queries: width_ratio_tally = 0 factor = int(256.0 / (len(self._databases) * 2.5)) @@ -196,6 +199,8 @@ class SQLPanel(Panel): query['stacktrace'] = render_stacktrace(query['stacktrace']) i += 1 + 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..65d26bd 100644 --- a/debug_toolbar/panels/sql/utils.py +++ b/debug_toolbar/panels/sql/utils.py @@ -35,3 +35,27 @@ def swap_fields(sql): r'<a class="djDebugCollapsed djDebugToggle" href="#">\1</a> ' r'<strong>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 subsequent 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 = 1 << 7 + so_far = [[0, 0, 0]] + while True: + if n == 0: # This happens after 2**24 colours; presumably, never + yield "#000000" # black + 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 >>= 1 |
