Skip to content

Commit 7937aea

Browse files
SebCorbinalbertyw
authored andcommitted
Add a column to retain number of duplicated, display in requests list
1 parent d0843d6 commit 7937aea

File tree

6 files changed

+31
-8
lines changed

6 files changed

+31
-8
lines changed

silk/collector.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import marshal
44
import pstats
5+
from collections import defaultdict
56
from io import StringIO
67
from threading import local
78

@@ -149,10 +150,13 @@ def finalise(self):
149150
self.request.prof_file = f.name
150151

151152
sql_queries = []
153+
duplicate_queries = defaultdict(lambda: -1)
152154
for identifier, query in self.queries.items():
153155
query['identifier'] = identifier
154156
sql_query = models.SQLQuery(**query)
155157
sql_queries += [sql_query]
158+
duplicate_queries[sql_query.query_structure] += 1
159+
self.request.num_duplicated_queries = sum(duplicate_queries.values())
156160

157161
models.SQLQuery.objects.bulk_create(sql_queries)
158162
sql_queries = models.SQLQuery.objects.filter(request=self.request)

silk/migrations/0009_sqlquery_query_structure.py renamed to silk/migrations/0009_duplicated_queries.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 3.2.16 on 2022-10-21 15:55
1+
# Generated by Django 3.2.16 on 2022-10-25 17:11
22

33
from django.db import migrations, models
44

@@ -10,6 +10,11 @@ class Migration(migrations.Migration):
1010
]
1111

1212
operations = [
13+
migrations.AddField(
14+
model_name='request',
15+
name='num_duplicated_queries',
16+
field=models.IntegerField(blank=True, null=True),
17+
),
1318
migrations.AddField(
1419
model_name='sqlquery',
1520
name='query_structure',

silk/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class Request(models.Model):
7575
meta_time = FloatField(null=True, blank=True)
7676
meta_num_queries = IntegerField(null=True, blank=True)
7777
meta_time_spent_queries = FloatField(null=True, blank=True)
78+
num_duplicated_queries = IntegerField(null=True, blank=True)
7879
pyprofile = TextField(blank=True, default='')
7980
prof_file = FileField(max_length=300, blank=True, storage=silk_storage)
8081

silk/templates/silk/inclusion/request_summary.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
<span class="appenage">on queries<span class="meta">{% if silk_request.meta_time_spent_queries %} +{{ silk_request.meta_time_spent_queries | floatformat:"0" }}<span class="unit">ms</span>{% endif %}</span></div>
1313
<div class="num-queries-div">
1414
<span class="numeric">{{ silk_request.num_sql_queries }}</span>
15-
<span class="appendage">queries<span class="meta">{% if silk_request.meta_num_queries %} +{{ silk_request.meta_num_queries }}{% endif %}</span>
15+
<span class="appendage">queries{% if silk_request.meta_num_queries %}<span class="meta"> +{{ silk_request.meta_num_queries }}</span>{% endif %}</span>
1616
</div>
17+
{% if silk_request.num_duplicated_queries %}
18+
<div class="num-duplicated-queries-div">
19+
<span class="numeric">{{ silk_request.num_duplicated_queries }}</span>
20+
<span class="appendage">duplicated queries</span>
21+
</div>
22+
{% endif %}
1723
</div>

silk/templates/silk/inclusion/request_summary_row.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@
88
</div>
99
<div class="col time-taken-queries-div spacing">
1010
<span class="col numeric">{{ silk_request.time_spent_on_sql_queries|floatformat:"0" }}<span class="unit">ms</span></span>
11-
<span class="col appenage">on queries<span class="meta">{% if silk_request.meta_time_spent_queries %} +{{ silk_request.meta_time_spent_queries | floatformat:"0" }}<span class="unit">ms</span>{% endif %}</span></div>
11+
<span class="col appenage">on queries<span class="meta">{% if silk_request.meta_time_spent_queries %} +{{ silk_request.meta_time_spent_queries | floatformat:"0" }}<span class="unit">ms</span>{% endif %}</span></span>
12+
</div>
1213
<div class="col num-queries-div spacing">
1314
<span class="col numeric">{{ silk_request.num_sql_queries }}</span>
14-
<span class="col appendage">queries<span class="meta">{% if silk_request.meta_num_queries %} +{{ silk_request.meta_num_queries }}{% endif %}</span></span>
15+
<span class="col appendage">queries{% if silk_request.meta_num_queries %}<span class="meta"> +{{ silk_request.meta_num_queries }}</span>{% endif %}</span>
16+
</div>
17+
{% if silk_request.num_duplicated_queries %}
18+
<div class="col num-duplicated-queries-div spacing">
19+
<span class="col numeric">{{ silk_request.num_duplicated_queries }}</span>
20+
<span class="col appendage">duplicated queries</span>
1521
</div>
22+
{% endif %}

silk/views/sql.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ def get(self, request, *_, **kwargs):
2222
'request': request,
2323
}
2424
if request_id:
25-
query_duplicates = defaultdict(lambda: -1)
25+
duplicate_queries = defaultdict(lambda: -1)
2626
silk_request = Request.objects.get(id=request_id)
2727
query_set = SQLQuery.objects.filter(request=silk_request).order_by('-start_time')
2828
for q in query_set:
2929
q.start_time_relative = q.start_time - silk_request.start_time
30-
query_duplicates[q.query_structure] += 1
31-
structures = list(query_duplicates.keys())
30+
duplicate_queries[q.query_structure] += 1
31+
structures = list(duplicate_queries.keys())
3232
for q in query_set:
33-
q.num_duplicates = query_duplicates[q.query_structure]
33+
q.num_duplicates = duplicate_queries[q.query_structure]
3434
q.duplicate_id = structures.index(q.query_structure)
3535
page = _page(request, query_set)
3636
context['silk_request'] = silk_request

0 commit comments

Comments
 (0)