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 #719

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
e3b1d2b
"Refactor SQL query time calculation and reorder imports
beltagymohamed Jul 9, 2024
1e01bd9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 9, 2024
2ce501a
fix : bug use datetime and convert to milliseconds
beltagymohamed Jul 9, 2024
42587a4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 9, 2024
f68af2f
test: increase verboisity to debug tox
beltagymohamed Jul 9, 2024
69cc9a4
test: only enable one test to debug tox
beltagymohamed Jul 9, 2024
0ecaa92
comment: update comment
beltagymohamed Jul 9, 2024
a33dda7
test: trigger tox
beltagymohamed Jul 9, 2024
691df21
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 9, 2024
b7fdfcf
run all tests for tox
beltagymohamed Jul 9, 2024
d1234dd
trigger tests
beltagymohamed Jul 9, 2024
82ee6e0
trigger all tests
beltagymohamed Jul 9, 2024
4f41ca6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 9, 2024
93b26c2
revert changes to models.py
beltagymohamed Jul 9, 2024
2c96408
Merge branch 'fix/aggregate-sql-query-time' of https://github.com/bel…
beltagymohamed Jul 9, 2024
f446d91
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 9, 2024
d660d53
fix aggregation
beltagymohamed Jul 9, 2024
0ff26f0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 9, 2024
bba41ce
use db aggregation
beltagymohamed Jul 9, 2024
7c9db9f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 9, 2024
e4d2e03
remove extra logging
beltagymohamed Jul 9, 2024
bc39840
Merge branch 'fix/aggregate-sql-query-time' of https://github.com/bel…
beltagymohamed Jul 9, 2024
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.
"""
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