Skip to content

Commit 9214ddc

Browse files
committed
#398 Added similar queries count and highlight
1 parent 7ba0a55 commit 9214ddc

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 3.2.16 on 2022-10-21 15:55
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('silk', '0008_sqlquery_analysis'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='sqlquery',
15+
name='query_structure',
16+
field=models.TextField(default=''),
17+
),
18+
]

silk/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ def bulk_create(self, *args, **kwargs):
237237

238238
class SQLQuery(models.Model):
239239
query = TextField()
240+
query_structure = TextField(default='')
240241
start_time = DateTimeField(null=True, blank=True, default=timezone.now)
241242
end_time = DateTimeField(null=True, blank=True)
242243
time_taken = FloatField(blank=True, null=True)

silk/sql.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def execute_sql(self, *args, **kwargs):
8181
if _should_wrap(sql_query):
8282
query_dict = {
8383
'query': sql_query,
84+
'query_structure': q,
8485
'start_time': timezone.now(),
8586
'traceback': tb
8687
}

silk/templates/silk/sql.html

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
height: 20px;
4242
}
4343

44-
tr.data-row:hover {
44+
tr.data-row:hover, tr.data-row.highlight {
4545
background-color: rgb(51, 51, 68);
4646
color: white;
4747
cursor: pointer;
@@ -108,10 +108,14 @@
108108
<th class="left-aligned">Tables</th>
109109
<th class="right-aligned">Num. Joins</th>
110110
<th class="right-aligned">Execution Time (ms)</th>
111+
<th class="right-aligned">Num. Duplicates</th>
111112
</tr>
112113
{% for sql_query in items %}
113114
<!-- TODO: Pretty grimy... -->
114-
<tr class="data-row" onclick="window.location=' \
115+
<tr
116+
class="data-row"
117+
data-duplicate-id="{{ sql_query.duplicate_id }}"
118+
onclick="window.location=' \
115119
{% if profile and silk_request %}\
116120
{% url "silk:request_and_profile_sql_detail" silk_request.id profile.id sql_query.id %}\
117121
{% elif profile %}\
@@ -124,6 +128,7 @@
124128
<td class="left-aligned">{{ sql_query.tables_involved|join:", " }}</td>
125129
<td class="right-aligned">{{ sql_query.num_joins }}</td>
126130
<td class="right-aligned">{{ sql_query.time_taken | floatformat:6 }}</td>
131+
<td class="right-aligned">{{ sql_query.num_duplicates }}</td>
127132
</tr>
128133
{% endfor %}
129134

@@ -151,8 +156,20 @@
151156
</div>
152157
</div>
153158

154-
155-
159+
<script>
160+
const rows = document.querySelectorAll('tr.data-row').forEach(function (row) {
161+
row.addEventListener('mouseenter', function () {
162+
document.querySelectorAll('tr[data-duplicate-id="' + row.dataset.duplicateId + '"]').forEach(function (e) {
163+
e.classList.add('highlight');
164+
});
165+
});
166+
row.addEventListener('mouseleave', function () {
167+
document.querySelectorAll('tr[data-duplicate-id="' + row.dataset.duplicateId + '"]').forEach(function (e) {
168+
e.classList.remove('highlight');
169+
});
170+
});
171+
});
172+
</script>
156173

157174

158175
{% endblock %}

silk/views/sql.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from collections import defaultdict
2+
13
from django.shortcuts import render
24
from django.utils.decorators import method_decorator
35
from django.views.generic import View
@@ -20,10 +22,16 @@ def get(self, request, *_, **kwargs):
2022
'request': request,
2123
}
2224
if request_id:
25+
query_duplicates = defaultdict(lambda: -1)
2326
silk_request = Request.objects.get(id=request_id)
2427
query_set = SQLQuery.objects.filter(request=silk_request).order_by('-start_time')
2528
for q in query_set:
2629
q.start_time_relative = q.start_time - silk_request.start_time
30+
query_duplicates[q.query_structure] += 1
31+
structures = list(query_duplicates.keys())
32+
for q in query_set:
33+
q.num_duplicates = query_duplicates[q.query_structure]
34+
q.duplicate_id = structures.index(q.query_structure)
2735
page = _page(request, query_set)
2836
context['silk_request'] = silk_request
2937
if profile_id:

0 commit comments

Comments
 (0)