Skip to content

Commit d0843d6

Browse files
SebCorbinalbertyw
authored andcommitted
#398 Added similar queries count and highlight
1 parent 843c898 commit d0843d6

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
@@ -82,6 +82,7 @@ def execute_sql(self, *args, **kwargs):
8282
if _should_wrap(sql_query):
8383
query_dict = {
8484
'query': sql_query,
85+
'query_structure': q,
8586
'start_time': timezone.now(),
8687
'traceback': tb
8788
}

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;
@@ -109,10 +109,14 @@
109109
<th class="left-aligned">Tables</th>
110110
<th class="right-aligned">Num. Joins</th>
111111
<th class="right-aligned">Execution Time (ms)</th>
112+
<th class="right-aligned">Num. Duplicates</th>
112113
</tr>
113114
{% for sql_query in items %}
114115
<!-- TODO: Pretty grimy... -->
115-
<tr class="data-row" onclick="window.location=' \
116+
<tr
117+
class="data-row"
118+
data-duplicate-id="{{ sql_query.duplicate_id }}"
119+
onclick="window.location=' \
116120
{% if profile and silk_request %}\
117121
{% url "silk:request_and_profile_sql_detail" silk_request.id profile.id sql_query.id %}\
118122
{% elif profile %}\
@@ -126,6 +130,7 @@
126130
<td class="left-aligned">{{ sql_query.tables_involved|join:", " }}</td>
127131
<td class="right-aligned">{{ sql_query.num_joins }}</td>
128132
<td class="right-aligned">{{ sql_query.time_taken | floatformat:6 }}</td>
133+
<td class="right-aligned">{{ sql_query.num_duplicates }}</td>
129134
</tr>
130135
{% endfor %}
131136

@@ -153,8 +158,20 @@
153158
</div>
154159
</div>
155160

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

159176

160177
{% 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)