Refactored time_spent calculation queue exclusions to reflect queue changes over time

This commit is contained in:
Sam Splunks 2024-04-09 13:25:35 +00:00
parent e372f4447b
commit f4ab7a5226

View File

@ -1007,34 +1007,59 @@ class FollowUp(models.Model):
def time_spent_calculation(self): def time_spent_calculation(self):
"Returns timedelta according to rules settings." "Returns timedelta according to rules settings."
# extract earliest from previous follow-up or ticket open_hours = helpdesk_settings.FOLLOWUP_TIME_SPENT_OPENING_HOURS
try: holidays = helpdesk_settings.FOLLOWUP_TIME_SPENT_EXCLUDE_HOLIDAYS
exclude_statuses = helpdesk_settings.FOLLOWUP_TIME_SPENT_EXCLUDE_STATUSES
exclude_queues = helpdesk_settings.FOLLOWUP_TIME_SPENT_EXCLUDE_QUEUES
# queryset for this ticket previous follow-ups
prev_fup_qs = self.ticket.followup_set.all() prev_fup_qs = self.ticket.followup_set.all()
if self.id: if self.id:
prev_fup_qs = prev_fup_qs.filter(id__lt=self.id) # if the follow-up exist in DB, only keep previous follow-ups
prev_fup = prev_fup_qs.latest("date") prev_fup_qs = prev_fup_qs.filter(date__lt=self.date)
earliest = prev_fup.date
except ObjectDoesNotExist:
earliest = self.ticket.created
# extract previous status from follow-up or ticket # handle exclusions
# extract previous status from follow-up or ticket for exclusion check
if exclude_statuses:
try: try:
prev_fup_qs = self.ticket.followup_set.exclude(new_status__isnull=True)
if self.id:
prev_fup_qs = prev_fup_qs.filter(id__lt=self.id)
prev_fup = prev_fup_qs.latest("date") prev_fup = prev_fup_qs.latest("date")
prev_status = prev_fup.new_status prev_status = prev_fup.new_status
except ObjectDoesNotExist: except ObjectDoesNotExist:
prev_status = self.ticket.status prev_status = self.ticket.status
# latest time is current follow-up date # don't calculate status exclusions
latest = self.date if prev_status in exclude_statuses:
return datetime.timedelta(seconds=0)
# find the previous queue for exclusion check
if exclude_queues:
try:
prev_fup_ids = prev_fup_qs.values_list('id', flat=True)
prev_queue_change = TicketChange.objects.filter(followup_id__in=prev_fup_ids,
field=_('Queue')).latest('id')
prev_queue = Queue.objects.get(pk=prev_queue_change.new_value)
prev_queue_slug = prev_queue.slug
except ObjectDoesNotExist:
prev_queue_slug = self.ticket.queue.slug
# don't calculate queue exclusions
if prev_queue_slug in exclude_queues:
return datetime.timedelta(seconds=0)
# no exclusion found
time_spent_seconds = 0 time_spent_seconds = 0
open_hours = helpdesk_settings.FOLLOWUP_TIME_SPENT_OPENING_HOURS
holidays = helpdesk_settings.FOLLOWUP_TIME_SPENT_EXCLUDE_HOLIDAYS # extract earliest from previous follow-up or ticket
exclude_statuses = helpdesk_settings.FOLLOWUP_TIME_SPENT_EXCLUDE_STATUSES try:
exclude_queues = helpdesk_settings.FOLLOWUP_TIME_SPENT_EXCLUDE_QUEUES prev_fup = prev_fup_qs.latest("date")
earliest = prev_fup.date
except ObjectDoesNotExist:
earliest = self.ticket.created
# latest time is current follow-up date
latest = self.date
# split time interval by days # split time interval by days
days = latest.toordinal() - earliest.toordinal() days = latest.toordinal() - earliest.toordinal()
@ -1054,9 +1079,7 @@ class FollowUp(models.Model):
start_day_time = middle_day_time.replace(hour=0, minute=0, second=0) start_day_time = middle_day_time.replace(hour=0, minute=0, second=0)
end_day_time = middle_day_time.replace(hour=23, minute=59, second=59, microsecond=999999) end_day_time = middle_day_time.replace(hour=23, minute=59, second=59, microsecond=999999)
if (start_day_time.strftime("%Y-%m-%d") not in holidays and if start_day_time.strftime("%Y-%m-%d") not in holidays:
prev_status not in exclude_statuses and
self.ticket.queue.slug not in exclude_queues):
time_spent_seconds += daily_time_spent_calculation(start_day_time, end_day_time, open_hours) time_spent_seconds += daily_time_spent_calculation(start_day_time, end_day_time, open_hours)
return datetime.timedelta(seconds=time_spent_seconds) return datetime.timedelta(seconds=time_spent_seconds)