Skip to content

Commit

Permalink
#398 Added similar queries count and highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
SebCorbin committed Oct 21, 2022
1 parent 7ba0a55 commit 9214ddc
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
18 changes: 18 additions & 0 deletions silk/migrations/0009_sqlquery_query_structure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.16 on 2022-10-21 15:55

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('silk', '0008_sqlquery_analysis'),
]

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

class SQLQuery(models.Model):
query = TextField()
query_structure = TextField(default='')
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 @@ -81,6 +81,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
25 changes: 21 additions & 4 deletions silk/templates/silk/sql.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
height: 20px;
}

tr.data-row:hover {
tr.data-row:hover, tr.data-row.highlight {
background-color: rgb(51, 51, 68);
color: white;
cursor: pointer;
Expand Down Expand Up @@ -108,10 +108,14 @@
<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... -->
<tr class="data-row" onclick="window.location=' \
<tr
class="data-row"
data-duplicate-id="{{ sql_query.duplicate_id }}"
onclick="window.location=' \
{% if profile and silk_request %}\
{% url "silk:request_and_profile_sql_detail" silk_request.id profile.id sql_query.id %}\
{% elif profile %}\
Expand All @@ -124,6 +128,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_duplicates }}</td>
</tr>
{% endfor %}

Expand Down Expand Up @@ -151,8 +156,20 @@
</div>
</div>



<script>
const rows = document.querySelectorAll('tr.data-row').forEach(function (row) {
row.addEventListener('mouseenter', function () {
document.querySelectorAll('tr[data-duplicate-id="' + row.dataset.duplicateId + '"]').forEach(function (e) {
e.classList.add('highlight');
});
});
row.addEventListener('mouseleave', function () {
document.querySelectorAll('tr[data-duplicate-id="' + row.dataset.duplicateId + '"]').forEach(function (e) {
e.classList.remove('highlight');
});
});
});
</script>


{% endblock %}
8 changes: 8 additions & 0 deletions silk/views/sql.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections import defaultdict

from django.shortcuts import render
from django.utils.decorators import method_decorator
from django.views.generic import View
Expand All @@ -20,10 +22,16 @@ def get(self, request, *_, **kwargs):
'request': request,
}
if request_id:
query_duplicates = 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())
for q in query_set:
q.num_duplicates = query_duplicates[q.query_structure]
q.duplicate_id = structures.index(q.query_structure)
page = _page(request, query_set)
context['silk_request'] = silk_request
if profile_id:
Expand Down

0 comments on commit 9214ddc

Please sign in to comment.