From df46695bb1fd47374cd849bb238cf6b76b6f22dd Mon Sep 17 00:00:00 2001 From: Sam Splunks <72095718+samsplunks@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:54:17 +0000 Subject: [PATCH] Moving python time_spent calculation to database aggregation --- helpdesk/models.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/helpdesk/models.py b/helpdesk/models.py index eacdfe6e..a9fca8ad 100644 --- a/helpdesk/models.py +++ b/helpdesk/models.py @@ -362,11 +362,8 @@ class Queue(models.Model): """Return back total time spent on the ticket. This is calculated value based on total sum from all FollowUps """ - total = datetime.timedelta(0) - for val in self.ticket_set.all(): - if val.time_spent: - total = total + val.time_spent - return total + res = FollowUp.objects.filter(ticket__queue=self).aggregate(models.Sum('time_spent')) + return res.get('time_spent__sum', datetime.timedelta(0)) @property def time_spent_formated(self): @@ -582,11 +579,8 @@ class Ticket(models.Model): """Return back total time spent on the ticket. This is calculated value based on total sum from all FollowUps """ - total = datetime.timedelta(0) - for val in self.followup_set.all(): - if val.time_spent: - total = total + val.time_spent - return total + res = FollowUp.objects.filter(ticket=self).aggregate(models.Sum('time_spent')) + return res.get('time_spent__sum', datetime.timedelta(0)) @property def time_spent_formated(self):