Skip to content

Commit 8c690d7

Browse files
committed
Merge pull request #543 from wolfe/group-sql
Color-code SQL query "Timeline" stripes according to stacktrace
2 parents 296a089 + 8d68d30 commit 8c690d7

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

debug_toolbar/panels/sql/panel.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import uuid
44
from copy import copy
5+
from collections import defaultdict
56

67
from django.conf.urls import patterns, url
78
from django.db import connections
@@ -10,7 +11,7 @@
1011
from debug_toolbar.panels import Panel
1112
from debug_toolbar.panels.sql.forms import SQLSelectForm
1213
from debug_toolbar.utils import render_stacktrace
13-
from debug_toolbar.panels.sql.utils import reformat_sql
14+
from debug_toolbar.panels.sql.utils import reformat_sql, contrasting_color_generator
1415
from debug_toolbar.panels.sql.tracking import wrap_cursor, unwrap_cursor
1516

1617

@@ -136,6 +137,8 @@ def disable_instrumentation(self):
136137
unwrap_cursor(connection)
137138

138139
def process_response(self, request, response):
140+
colors = contrasting_color_generator()
141+
trace_colors = defaultdict(lambda: next(colors))
139142
if self._queries:
140143
width_ratio_tally = 0
141144
factor = int(256.0 / (len(self._databases) * 2.5))
@@ -196,6 +199,8 @@ def process_response(self, request, response):
196199
query['stacktrace'] = render_stacktrace(query['stacktrace'])
197200
i += 1
198201

202+
query['trace_color'] = trace_colors[query['stacktrace']]
203+
199204
if trans_id:
200205
self._queries[(i - 1)][1]['ends_trans'] = True
201206

debug_toolbar/panels/sql/utils.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,27 @@ def swap_fields(sql):
3535
r'<a class="djDebugCollapsed djDebugToggle" href="#">\1</a> '
3636
r'<strong>FROM')
3737
return re.sub(expr, subs, sql)
38+
39+
40+
def contrasting_color_generator():
41+
"""
42+
Generate constrasting colors by varying most significant bit of RGB first,
43+
and then vary subsequent bits systematically.
44+
"""
45+
def rgb_to_hex(rgb):
46+
return '#%02x%02x%02x' % tuple(rgb)
47+
48+
triples = [(1, 0, 0), (0, 1, 0), (0, 0, 1),
49+
(1, 1, 0), (0, 1, 1), (1, 0, 1), (1, 1, 1)]
50+
n = 1 << 7
51+
so_far = [[0, 0, 0]]
52+
while True:
53+
if n == 0: # This happens after 2**24 colours; presumably, never
54+
yield "#000000" # black
55+
copy_so_far = list(so_far)
56+
for triple in triples:
57+
for previous in copy_so_far:
58+
rgb = [n * triple[i] + previous[i] for i in range(3)]
59+
so_far.append(rgb)
60+
yield rgb_to_hex(rgb)
61+
n >>= 1

debug_toolbar/templates/debug_toolbar/panels/sql.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
</div>
3535
</td>
3636
<td class="timeline">
37-
<div class="djDebugTimeline"><div class="djDebugLineChart{% if query.is_slow %} djDebugLineChartWarning{% endif %}" style="left:{{ query.start_offset|unlocalize }}%;"><strong style="width:{{ query.width_ratio_relative|unlocalize }}%;">{{ query.width_ratio }}%</strong></div></div>
37+
<div class="djDebugTimeline"><div class="djDebugLineChart{% if query.is_slow %} djDebugLineChartWarning{% endif %}" style="left:{{ query.start_offset|unlocalize }}%;"><strong style="width:{{ query.width_ratio_relative|unlocalize }}%; background-color:{{ query.trace_color }};">{{ query.width_ratio }}%</strong></div></div>
3838
</td>
3939
<td class="time">
4040
{{ query.duration|floatformat:"2" }}

0 commit comments

Comments
 (0)