Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor SQL query time calculation to use Django aggregation #720

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions project/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,15 @@ def test_time_spent_on_sql_queries_if_has_no_related_SQLQueries(self):

self.assertEqual(self.obj.time_spent_on_sql_queries, 0)

# FIXME probably a bug
def test_time_spent_on_sql_queries_if_has_related_SQLQueries_with_no_time_taken(self):

query = SQLQueryFactory()
self.obj.queries.add(query)

self.assertEqual(query.time_taken, None)

with self.assertRaises(TypeError):
self.obj.time_spent_on_sql_queries
# No exception should be raised, and the result should be 0.0
self.assertEqual(self.obj.time_spent_on_sql_queries, 0.0)

def test_time_spent_on_sql_queries_if_has_related_SQLQueries_and_time_taken(self):

Expand Down
24 changes: 14 additions & 10 deletions silk/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
IntegerField,
ManyToManyField,
OneToOneField,
Sum,
TextField,
)
from django.utils import timezone
Expand Down Expand Up @@ -124,16 +125,13 @@ def profile_table(self):

@property
def time_spent_on_sql_queries(self):
""""
Calculate the total time spent in milli seconds on SQL queries using Django aggregates.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - "milliseconds" instead of "milli seconds"

"""
TODO: Perhaps there is a nicer way to do this with Django aggregates?
My initial thought was to perform:
SQLQuery.objects.filter.aggregate(Sum(F('end_time')) - Sum(F('start_time')))
However this feature isnt available yet, however there has been talk
for use of F objects within aggregates for four years
here: https://code.djangoproject.com/ticket/14030. It looks
like this will go in soon at which point this should be changed.
"""
return sum(x.time_taken for x in SQLQuery.objects.filter(request=self))
result = SQLQuery.objects.filter(request=self).aggregate(
total_time=Sum('time_taken', output_field=FloatField())
)
return result['total_time'] or 0.0

@property
def headers(self):
Expand Down Expand Up @@ -376,4 +374,10 @@ def is_context_profile(self):

@property
def time_spent_on_sql_queries(self):
return sum(x.time_taken for x in self.queries.all())
"""
Calculate the total time spent in milliseconds on SQL queries using Django aggregates.
"""
result = self.queries.aggregate(
total_time=Sum('time_taken', output_field=FloatField())
)
return result['total_time'] or 0.0
Loading