From 56bbfd43d1f590f9d38e68b970f4048b3f2de29c Mon Sep 17 00:00:00 2001 From: s-aga-r Date: Sat, 5 Oct 2024 21:50:36 +0530 Subject: [PATCH] refactor: `Outbound Delay` report --- .../report/outbound_delay/outbound_delay.py | 66 +++++++------------ 1 file changed, 25 insertions(+), 41 deletions(-) diff --git a/mail/mail/report/outbound_delay/outbound_delay.py b/mail/mail/report/outbound_delay/outbound_delay.py index 511c8e3c..6cd3b26a 100644 --- a/mail/mail/report/outbound_delay/outbound_delay.py +++ b/mail/mail/report/outbound_delay/outbound_delay.py @@ -6,7 +6,7 @@ from typing import Tuple from frappe.query_builder import Order, Criterion from mail.utils.cache import get_user_owned_domains -from frappe.query_builder.functions import Date, IfNull +from frappe.query_builder.functions import Sum, Date, IfNull from mail.utils.user import has_role, is_system_manager, get_user_mailboxes @@ -36,19 +36,21 @@ def get_data(filters=None) -> list: OM.message_size, OM.via_api, OM.is_newsletter, - OM.submitted_after, - OM.transfer_completed_after.as_("transferred_after"), - MR.action_after, + OM.submitted_after.as_("submission_delay"), + Sum(OM.transfer_started_after, OM.transfer_completed_after).as_("transfer_delay"), + OM.action_after.as_("action_delay"), + Sum( + OM.submitted_after, + OM.transfer_started_after, + OM.transfer_completed_after, + OM.action_after, + ).as_("total_delay"), OM.agent, OM.domain_name, OM.ip_address, OM.sender, MR.email.as_("recipient"), OM.message_id, - OM.created_at, - OM.submitted_at, - OM.transfer_completed_at.as_("transferred_at"), - MR.action_at, ) .where((OM.docstatus == 1) & (IfNull(MR.status, "") != "")) .orderby(OM.creation, OM.created_at, order=Order.desc) @@ -111,7 +113,7 @@ def get_summary(data: list) -> list[dict]: status_count[status] += 1 total_message_size += row["message_size"] - total_transfer_delay += row["transferred_after"] + total_transfer_delay += row["transfer_delay"] return [ { @@ -193,22 +195,28 @@ def get_columns() -> list: "width": 100, }, { - "label": _("Created After"), - "fieldname": "submitted_after", + "label": _("Submission Delay"), + "fieldname": "submission_delay", "fieldtype": "Int", "width": 120, }, { - "label": _("Transferred After"), - "fieldname": "transferred_after", + "label": _("Transfer Delay"), + "fieldname": "transfer_delay", "fieldtype": "Int", - "width": 140, + "width": 120, + }, + { + "label": _("Action Delay"), + "fieldname": "action_delay", + "fieldtype": "Int", + "width": 120, }, { - "label": _("Action After"), - "fieldname": "action_after", + "label": _("Total Delay"), + "fieldname": "total_delay", "fieldtype": "Int", - "width": 110, + "width": 120, }, { "label": _("Agent"), @@ -249,28 +257,4 @@ def get_columns() -> list: "fieldtype": "Data", "width": 200, }, - { - "label": _("Created At"), - "fieldname": "created_at", - "fieldtype": "Datetime", - "width": 180, - }, - { - "label": _("Submitted At"), - "fieldname": "submitted_at", - "fieldtype": "Datetime", - "width": 180, - }, - { - "label": _("Transferred At"), - "fieldname": "transferred_at", - "fieldtype": "Datetime", - "width": 180, - }, - { - "label": _("Action At"), - "fieldname": "action_at", - "fieldtype": "Datetime", - "width": 180, - }, ]