|
17 | 17 | IntegerField,
|
18 | 18 | ManyToManyField,
|
19 | 19 | OneToOneField,
|
| 20 | + Sum, |
20 | 21 | TextField,
|
21 | 22 | )
|
22 | 23 | from django.utils import timezone
|
@@ -124,16 +125,13 @@ def profile_table(self):
|
124 | 125 |
|
125 | 126 | @property
|
126 | 127 | def time_spent_on_sql_queries(self):
|
| 128 | + """" |
| 129 | + Calculate the total time spent in milli seconds on SQL queries using Django aggregates. |
127 | 130 | """
|
128 |
| - TODO: Perhaps there is a nicer way to do this with Django aggregates? |
129 |
| - My initial thought was to perform: |
130 |
| - SQLQuery.objects.filter.aggregate(Sum(F('end_time')) - Sum(F('start_time'))) |
131 |
| - However this feature isnt available yet, however there has been talk |
132 |
| - for use of F objects within aggregates for four years |
133 |
| - here: https://code.djangoproject.com/ticket/14030. It looks |
134 |
| - like this will go in soon at which point this should be changed. |
135 |
| - """ |
136 |
| - return sum(x.time_taken for x in SQLQuery.objects.filter(request=self)) |
| 131 | + result = SQLQuery.objects.filter(request=self).aggregate( |
| 132 | + total_time=Sum('time_taken', output_field=FloatField()) |
| 133 | + ) |
| 134 | + return result['total_time'] or 0.0 |
137 | 135 |
|
138 | 136 | @property
|
139 | 137 | def headers(self):
|
@@ -376,4 +374,10 @@ def is_context_profile(self):
|
376 | 374 |
|
377 | 375 | @property
|
378 | 376 | def time_spent_on_sql_queries(self):
|
379 |
| - return sum(x.time_taken for x in self.queries.all()) |
| 377 | + """ |
| 378 | + Calculate the total time spent in milliseconds on SQL queries using Django aggregates. |
| 379 | + """ |
| 380 | + result = self.queries.aggregate( |
| 381 | + total_time=Sum('time_taken', output_field=FloatField()) |
| 382 | + ) |
| 383 | + return result['total_time'] or 0.0 |
0 commit comments