Skip to content

Commit

Permalink
#398 Added similar queries count
Browse files Browse the repository at this point in the history
  • Loading branch information
nasirhjafri committed Aug 21, 2020
1 parent c69114a commit 106a414
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
19 changes: 19 additions & 0 deletions silk/migrations/0008_sqlquery_query_structure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.1 on 2020-08-21 19:47

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('silk', '0007_sqlquery_identifier'),
]

operations = [
migrations.AddField(
model_name='sqlquery',
name='query_structure',
field=models.TextField(default=''),
preserve_default=False,
),
]
1 change: 1 addition & 0 deletions silk/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def bulk_create(self, *args, **kwargs):

class SQLQuery(models.Model):
query = TextField()
query_structure = TextField()
start_time = DateTimeField(null=True, blank=True, default=timezone.now)
end_time = DateTimeField(null=True, blank=True)
time_taken = FloatField(blank=True, null=True)
Expand Down
1 change: 1 addition & 0 deletions silk/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def execute_sql(self, *args, **kwargs):
if _should_wrap(sql_query):
query_dict = {
'query': sql_query,
'query_structure': q,
'start_time': timezone.now(),
'traceback': tb
}
Expand Down
2 changes: 2 additions & 0 deletions silk/templates/silk/sql.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
<th class="left-aligned">Tables</th>
<th class="right-aligned">Num. Joins</th>
<th class="right-aligned">Execution Time (ms)</th>
<th class="right-aligned">Num. Duplicates</th>
</tr>
{% for sql_query in items %}
<!-- TODO: Pretty grimy... -->
Expand All @@ -124,6 +125,7 @@
<td class="left-aligned">{{ sql_query.tables_involved|join:", " }}</td>
<td class="right-aligned">{{ sql_query.num_joins }}</td>
<td class="right-aligned">{{ sql_query.time_taken | floatformat:6 }}</td>
<td class="right-aligned">{{ sql_query.num_duplicate }}</td>
</tr>
{% endfor %}

Expand Down
7 changes: 7 additions & 0 deletions silk/views/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ def get(self, request, *_, **kwargs):
if request_id:
silk_request = Request.objects.get(id=request_id)
query_set = SQLQuery.objects.filter(request=silk_request).order_by('-start_time')
query_structure_count = {}
for q in query_set:
q.start_time_relative = q.start_time - silk_request.start_time
if q.query_structure not in query_structure_count:
query_structure_count[q.query_structure] = -1
query_structure_count[q.query_structure] += 1
for q in query_set:
q.num_duplicate = query_structure_count[q.query_structure]

page = _page(request, query_set)
context['silk_request'] = silk_request
if profile_id:
Expand Down

0 comments on commit 106a414

Please sign in to comment.