Skip to content

Color-code SQL query "Timeline" stripes according to stacktrace #543

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

Merged
merged 3 commits into from
Feb 12, 2014
Merged
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
7 changes: 6 additions & 1 deletion debug_toolbar/panels/sql/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import uuid
from copy import copy
from collections import defaultdict

from django.conf.urls import patterns, url
from django.db import connections
Expand All @@ -10,7 +11,7 @@
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


Expand Down Expand Up @@ -136,6 +137,8 @@ def disable_instrumentation(self):
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))
Expand Down Expand Up @@ -196,6 +199,8 @@ def process_response(self, request, response):
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

Expand Down
24 changes: 24 additions & 0 deletions debug_toolbar/panels/sql/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this function is a one-liner and is called exactly once, why don't you inline it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather have it named, since otherwise it's too cryptic for my taste. Mind if I leave this as is?


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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The canonical pattern for duplicating a list is so_far[:], but in this case the intent is clear enough, so it doesn't really matter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen this one debated. Not a big deal, as you say. I think [:] is a bit more cryptic.

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
2 changes: 1 addition & 1 deletion debug_toolbar/templates/debug_toolbar/panels/sql.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</div>
</td>
<td class="timeline">
<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>
<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>
</td>
<td class="time">
{{ query.duration|floatformat:"2" }}
Expand Down