From 7937aea08dd92d12a1fa9e7442da46cdbd8b4ed2 Mon Sep 17 00:00:00 2001 From: Sebastien Corbin <seb.corbin@gmail.com> Date: Tue, 25 Oct 2022 17:22:37 +0200 Subject: [PATCH] Add a column to retain number of duplicated, display in requests list --- silk/collector.py | 4 ++++ ..._query_structure.py => 0009_duplicated_queries.py} | 7 ++++++- silk/models.py | 1 + silk/templates/silk/inclusion/request_summary.html | 8 +++++++- .../templates/silk/inclusion/request_summary_row.html | 11 +++++++++-- silk/views/sql.py | 8 ++++---- 6 files changed, 31 insertions(+), 8 deletions(-) rename silk/migrations/{0009_sqlquery_query_structure.py => 0009_duplicated_queries.py} (59%) diff --git a/silk/collector.py b/silk/collector.py index ebfc9d4a..b2c22b70 100644 --- a/silk/collector.py +++ b/silk/collector.py @@ -2,6 +2,7 @@ import logging import marshal import pstats +from collections import defaultdict from io import StringIO from threading import local @@ -149,10 +150,13 @@ def finalise(self): self.request.prof_file = f.name sql_queries = [] + duplicate_queries = defaultdict(lambda: -1) for identifier, query in self.queries.items(): query['identifier'] = identifier sql_query = models.SQLQuery(**query) sql_queries += [sql_query] + duplicate_queries[sql_query.query_structure] += 1 + self.request.num_duplicated_queries = sum(duplicate_queries.values()) models.SQLQuery.objects.bulk_create(sql_queries) sql_queries = models.SQLQuery.objects.filter(request=self.request) diff --git a/silk/migrations/0009_sqlquery_query_structure.py b/silk/migrations/0009_duplicated_queries.py similarity index 59% rename from silk/migrations/0009_sqlquery_query_structure.py rename to silk/migrations/0009_duplicated_queries.py index 46201a22..ec9b40f7 100644 --- a/silk/migrations/0009_sqlquery_query_structure.py +++ b/silk/migrations/0009_duplicated_queries.py @@ -1,4 +1,4 @@ -# Generated by Django 3.2.16 on 2022-10-21 15:55 +# Generated by Django 3.2.16 on 2022-10-25 17:11 from django.db import migrations, models @@ -10,6 +10,11 @@ class Migration(migrations.Migration): ] operations = [ + migrations.AddField( + model_name='request', + name='num_duplicated_queries', + field=models.IntegerField(blank=True, null=True), + ), migrations.AddField( model_name='sqlquery', name='query_structure', diff --git a/silk/models.py b/silk/models.py index 7642fdc0..279f1c19 100644 --- a/silk/models.py +++ b/silk/models.py @@ -75,6 +75,7 @@ class Request(models.Model): meta_time = FloatField(null=True, blank=True) meta_num_queries = IntegerField(null=True, blank=True) meta_time_spent_queries = FloatField(null=True, blank=True) + num_duplicated_queries = IntegerField(null=True, blank=True) pyprofile = TextField(blank=True, default='') prof_file = FileField(max_length=300, blank=True, storage=silk_storage) diff --git a/silk/templates/silk/inclusion/request_summary.html b/silk/templates/silk/inclusion/request_summary.html index 18caa4bb..cf7649ec 100644 --- a/silk/templates/silk/inclusion/request_summary.html +++ b/silk/templates/silk/inclusion/request_summary.html @@ -12,6 +12,12 @@ <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> <div class="num-queries-div"> <span class="numeric">{{ silk_request.num_sql_queries }}</span> - <span class="appendage">queries<span class="meta">{% if silk_request.meta_num_queries %} +{{ silk_request.meta_num_queries }}{% endif %}</span> + <span class="appendage">queries{% if silk_request.meta_num_queries %}<span class="meta"> +{{ silk_request.meta_num_queries }}</span>{% endif %}</span> </div> + {% if silk_request.num_duplicated_queries %} + <div class="num-duplicated-queries-div"> + <span class="numeric">{{ silk_request.num_duplicated_queries }}</span> + <span class="appendage">duplicated queries</span> + </div> + {% endif %} </div> diff --git a/silk/templates/silk/inclusion/request_summary_row.html b/silk/templates/silk/inclusion/request_summary_row.html index 20b18b04..cca9fea3 100644 --- a/silk/templates/silk/inclusion/request_summary_row.html +++ b/silk/templates/silk/inclusion/request_summary_row.html @@ -8,8 +8,15 @@ </div> <div class="col time-taken-queries-div spacing"> <span class="col numeric">{{ silk_request.time_spent_on_sql_queries|floatformat:"0" }}<span class="unit">ms</span></span> - <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> + <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> + </div> <div class="col num-queries-div spacing"> <span class="col numeric">{{ silk_request.num_sql_queries }}</span> - <span class="col appendage">queries<span class="meta">{% if silk_request.meta_num_queries %} +{{ silk_request.meta_num_queries }}{% endif %}</span></span> + <span class="col appendage">queries{% if silk_request.meta_num_queries %}<span class="meta"> +{{ silk_request.meta_num_queries }}</span>{% endif %}</span> + </div> + {% if silk_request.num_duplicated_queries %} + <div class="col num-duplicated-queries-div spacing"> + <span class="col numeric">{{ silk_request.num_duplicated_queries }}</span> + <span class="col appendage">duplicated queries</span> </div> + {% endif %} diff --git a/silk/views/sql.py b/silk/views/sql.py index 46c18cc9..c545934d 100644 --- a/silk/views/sql.py +++ b/silk/views/sql.py @@ -22,15 +22,15 @@ def get(self, request, *_, **kwargs): 'request': request, } if request_id: - query_duplicates = defaultdict(lambda: -1) + duplicate_queries = defaultdict(lambda: -1) silk_request = Request.objects.get(id=request_id) query_set = SQLQuery.objects.filter(request=silk_request).order_by('-start_time') for q in query_set: q.start_time_relative = q.start_time - silk_request.start_time - query_duplicates[q.query_structure] += 1 - structures = list(query_duplicates.keys()) + duplicate_queries[q.query_structure] += 1 + structures = list(duplicate_queries.keys()) for q in query_set: - q.num_duplicates = query_duplicates[q.query_structure] + q.num_duplicates = duplicate_queries[q.query_structure] q.duplicate_id = structures.index(q.query_structure) page = _page(request, query_set) context['silk_request'] = silk_request