From 72392a3f500f5f1cc575d3f8f11549a092c3e328 Mon Sep 17 00:00:00 2001 From: Martin Whitehouse Date: Mon, 25 Jul 2022 04:44:46 +0200 Subject: [PATCH] Add `update_summary_tables` helper function Reduces complexity of 'run_report' and handles updating summary table in own function --- helpdesk/views/staff.py | 86 +++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 42 deletions(-) diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py index 30dc88a1..f31b89f6 100644 --- a/helpdesk/views/staff.py +++ b/helpdesk/views/staff.py @@ -1578,6 +1578,49 @@ def get_report_table_and_totals(header1, summarytable, possible_options): return table, totals +def update_summary_tables(report_queryset, report, summarytable, summarytable2): + metric3 = False + for ticket in report_queryset: + if report == 'userpriority': + metric1 = u'%s' % ticket.get_assigned_to + metric2 = u'%s' % ticket.get_priority_display() + + elif report == 'userqueue': + metric1 = u'%s' % ticket.get_assigned_to + metric2 = u'%s' % ticket.queue.title + + elif report == 'userstatus': + metric1 = u'%s' % ticket.get_assigned_to + metric2 = u'%s' % ticket.get_status_display() + + elif report == 'usermonth': + metric1 = u'%s' % ticket.get_assigned_to + metric2 = u'%s-%s' % (ticket.created.year, ticket.created.month) + + elif report == 'queuepriority': + metric1 = u'%s' % ticket.queue.title + metric2 = u'%s' % ticket.get_priority_display() + + elif report == 'queuestatus': + metric1 = u'%s' % ticket.queue.title + metric2 = u'%s' % ticket.get_status_display() + + elif report == 'queuemonth': + metric1 = u'%s' % ticket.queue.title + metric2 = u'%s-%s' % (ticket.created.year, ticket.created.month) + + elif report == 'daysuntilticketclosedbymonth': + metric1 = u'%s' % ticket.queue.title + metric2 = u'%s-%s' % (ticket.created.year, ticket.created.month) + metric3 = ticket.modified - ticket.created + metric3 = metric3.days + + summarytable[metric1, metric2] += 1 + if metric3: + if report == 'daysuntilticketclosedbymonth': + summarytable2[metric1, metric2] += metric3 + + @helpdesk_staff_member_required def run_report(request, report): @@ -1663,48 +1706,7 @@ def run_report(request, report): col1heading = _('Queue') possible_options = periods charttype = 'date' - - metric3 = False - for ticket in report_queryset: - if report == 'userpriority': - metric1 = u'%s' % ticket.get_assigned_to - metric2 = u'%s' % ticket.get_priority_display() - - elif report == 'userqueue': - metric1 = u'%s' % ticket.get_assigned_to - metric2 = u'%s' % ticket.queue.title - - elif report == 'userstatus': - metric1 = u'%s' % ticket.get_assigned_to - metric2 = u'%s' % ticket.get_status_display() - - elif report == 'usermonth': - metric1 = u'%s' % ticket.get_assigned_to - metric2 = u'%s-%s' % (ticket.created.year, ticket.created.month) - - elif report == 'queuepriority': - metric1 = u'%s' % ticket.queue.title - metric2 = u'%s' % ticket.get_priority_display() - - elif report == 'queuestatus': - metric1 = u'%s' % ticket.queue.title - metric2 = u'%s' % ticket.get_status_display() - - elif report == 'queuemonth': - metric1 = u'%s' % ticket.queue.title - metric2 = u'%s-%s' % (ticket.created.year, ticket.created.month) - - elif report == 'daysuntilticketclosedbymonth': - metric1 = u'%s' % ticket.queue.title - metric2 = u'%s-%s' % (ticket.created.year, ticket.created.month) - metric3 = ticket.modified - ticket.created - metric3 = metric3.days - - summarytable[metric1, metric2] += 1 - if metric3: - if report == 'daysuntilticketclosedbymonth': - summarytable2[metric1, metric2] += metric3 - + update_summary_tables(report_queryset, report, summarytable, summarytable2) if report == 'daysuntilticketclosedbymonth': for key in summarytable2.keys(): summarytable[key] = summarytable2[key] / summarytable[key]