Skip to content

Commit 8d68d30

Browse files
committed
Color-code SQL: code review changes; fallback to black after 2**24 colors
1 parent eb77938 commit 8d68d30

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

debug_toolbar/panels/sql/panel.py

Lines changed: 2 additions & 3 deletions
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
@@ -137,7 +138,7 @@ def disable_instrumentation(self):
137138

138139
def process_response(self, request, response):
139140
colors = contrasting_color_generator()
140-
trace_colors = {}
141+
trace_colors = defaultdict(lambda: next(colors))
141142
if self._queries:
142143
width_ratio_tally = 0
143144
factor = int(256.0 / (len(self._databases) * 2.5))
@@ -198,8 +199,6 @@ def process_response(self, request, response):
198199
query['stacktrace'] = render_stacktrace(query['stacktrace'])
199200
i += 1
200201

201-
if query['stacktrace'] not in trace_colors:
202-
trace_colors[query['stacktrace']] = colors.next()
203202
query['trace_color'] = trace_colors[query['stacktrace']]
204203

205204
if trans_id:

debug_toolbar/panels/sql/utils.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,22 @@ def swap_fields(sql):
4040
def contrasting_color_generator():
4141
"""
4242
Generate constrasting colors by varying most significant bit of RGB first,
43-
and then vary subsebequent bits systematically.
43+
and then vary subsequent bits systematically.
4444
"""
4545
def rgb_to_hex(rgb):
4646
return '#%02x%02x%02x' % tuple(rgb)
4747

4848
triples = [(1, 0, 0), (0, 1, 0), (0, 0, 1),
4949
(1, 1, 0), (0, 1, 1), (1, 0, 1), (1, 1, 1)]
50-
n = 128
50+
n = 1 << 7
5151
so_far = [[0, 0, 0]]
52-
while (1):
52+
while True:
53+
if n == 0: # This happens after 2**24 colours; presumably, never
54+
yield "#000000" # black
5355
copy_so_far = list(so_far)
5456
for triple in triples:
5557
for previous in copy_so_far:
5658
rgb = [n * triple[i] + previous[i] for i in range(3)]
5759
so_far.append(rgb)
5860
yield rgb_to_hex(rgb)
59-
n = n / 2
61+
n >>= 1

0 commit comments

Comments
 (0)