Skip to content

Commit

Permalink
fix: handles re-transfer correctly
Browse files Browse the repository at this point in the history
fix: handles re-transfer correctly
  • Loading branch information
s-aga-r authored Oct 6, 2024
2 parents e38f63f + 51118f2 commit 7ba2a93
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 71 deletions.
25 changes: 25 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
parserPreset: "conventional-changelog-conventionalcommits",
rules: {
"subject-empty": [2, "never"],
"type-case": [2, "always", "lower-case"],
"type-empty": [2, "never"],
"type-enum": [
2,
"always",
[
"build",
"chore",
"ci",
"docs",
"feat",
"fix",
"perf",
"refactor",
"revert",
"style",
"test",
],
],
},
};
4 changes: 2 additions & 2 deletions mail/mail/doctype/mail_recipient/mail_recipient.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
},
{
"depends_on": "eval: doc.status",
"description": "Action At - Transferred At",
"description": "Action At - Transfer Completed At",
"fieldname": "action_after",
"fieldtype": "Int",
"label": "Action After (Seconds)",
Expand Down Expand Up @@ -94,7 +94,7 @@
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2024-07-31 16:10:44.144199",
"modified": "2024-10-05 10:03:46.688970",
"modified_by": "Administrator",
"module": "Mail",
"name": "Mail Recipient",
Expand Down
59 changes: 39 additions & 20 deletions mail/mail/doctype/outgoing_mail/outgoing_mail.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@
"section_break_093p",
"created_at",
"submitted_at",
"transferred_at",
"transfer_started_at",
"transfer_completed_at",
"column_break_fvyv",
"submitted_after",
"transferred_after",
"transfer_started_after",
"transfer_completed_after",
"section_break_aafd",
"tracking_id",
"first_opened_at",
Expand Down Expand Up @@ -203,23 +205,6 @@
"read_only": 1,
"search_index": 1
},
{
"fieldname": "transferred_at",
"fieldtype": "Datetime",
"label": "Transferred At",
"no_copy": 1,
"read_only": 1
},
{
"depends_on": "eval: doc.transferred_at",
"description": "Transferred At - Submitted At",
"fieldname": "transferred_after",
"fieldtype": "Int",
"label": "Transferred After (Seconds)",
"no_copy": 1,
"non_negative": 1,
"read_only": 1
},
{
"depends_on": "eval: doc.message_size",
"fieldname": "message_size",
Expand Down Expand Up @@ -426,12 +411,46 @@
"label": "In Reply To (Message ID)",
"length": 255,
"read_only": 1
},
{
"fieldname": "transfer_completed_at",
"fieldtype": "Datetime",
"label": "Transfer Completed At",
"no_copy": 1,
"read_only": 1
},
{
"depends_on": "eval: doc.transfer_completed_at",
"description": "Transfer Completed At - Transfer Started At",
"fieldname": "transfer_completed_after",
"fieldtype": "Int",
"label": "Transfer Completed After (Seconds)",
"no_copy": 1,
"non_negative": 1,
"read_only": 1
},
{
"fieldname": "transfer_started_at",
"fieldtype": "Datetime",
"label": "Transfer Started At",
"no_copy": 1,
"read_only": 1
},
{
"depends_on": "eval: doc.transfer_started_at",
"description": "Transfer Started At - Submitted At",
"fieldname": "transfer_started_after",
"fieldtype": "Int",
"label": "Transfer Started After (Seconds)",
"no_copy": 1,
"non_negative": 1,
"read_only": 1
}
],
"index_web_pages_for_search": 1,
"is_submittable": 1,
"links": [],
"modified": "2024-08-27 09:37:40.451414",
"modified": "2024-10-05 12:34:39.544599",
"modified_by": "Administrator",
"module": "Mail",
"name": "Outgoing Mail",
Expand Down
28 changes: 19 additions & 9 deletions mail/mail/doctype/outgoing_mail/outgoing_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,14 @@ def transfer_now(self) -> None:
if not (self.docstatus == 1 and self.status == "Pending"):
return

self._db_set(status="Transferring", commit=True)
transfer_started_at = now()
transfer_started_after = time_diff_in_seconds(transfer_started_at, self.submitted_at)
self._db_set(
status="Transferring",
transfer_started_at=transfer_started_at,
transfer_started_after=transfer_started_after,
commit=True,
)

recipients = [formataddr((r.display_name, r.email)) for r in self.recipients]
data = {
Expand All @@ -694,12 +701,14 @@ def transfer_now(self) -> None:
rmq.declare_queue(constants.OUTGOING_MAIL_QUEUE, max_priority=3)
rmq.publish(constants.OUTGOING_MAIL_QUEUE, json.dumps(data), priority=3)

transferred_at = now()
transferred_after = time_diff_in_seconds(transferred_at, self.submitted_at)
transfer_completed_at = now()
transfer_completed_after = time_diff_in_seconds(
transfer_completed_at, transfer_started_at
)
self._db_set(
status="Transferred",
transferred_at=transferred_at,
transferred_after=transferred_after,
transfer_completed_at=transfer_completed_at,
transfer_completed_after=transfer_completed_after,
commit=True,
)
except Exception:
Expand Down Expand Up @@ -1019,6 +1028,7 @@ def update_outgoing_mails(
outgoing_mails,
current_status=current_status,
status="Transferring",
transfer_started_at=now(),
error_log=None,
commit=True,
)
Expand Down Expand Up @@ -1048,8 +1058,8 @@ def update_outgoing_mails(
SET
status = %s,
error_log = NULL,
transferred_at = %s,
transferred_after = TIMESTAMPDIFF(SECOND, `submitted_at`, `transferred_at`)
transfer_completed_at = %s,
transfer_completed_after = TIMESTAMPDIFF(SECOND, `transfer_started_at`, `transfer_completed_at`)
WHERE
docstatus = 1 AND
status = %s AND
Expand Down Expand Up @@ -1127,7 +1137,7 @@ def undelivered(data: dict) -> None:
recipient.retries = retries
recipient.action_at = action_at
recipient.action_after = time_diff_in_seconds(
recipient.action_at, doc.transferred_at
recipient.action_at, doc.transfer_completed_at
)
recipient.details = json.dumps(recipients[recipient.email], indent=4)
recipient.db_update()
Expand Down Expand Up @@ -1166,7 +1176,7 @@ def delivered(data: dict) -> None:
recipient.retries = retries
recipient.action_at = action_at
recipient.action_after = time_diff_in_seconds(
recipient.action_at, doc.transferred_at
recipient.action_at, doc.transfer_completed_at
)
recipient.details = json.dumps(
{
Expand Down
64 changes: 24 additions & 40 deletions mail/mail/report/outbound_delay/outbound_delay.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,21 @@ def get_data(filters=None) -> list:
OM.message_size,
OM.via_api,
OM.is_newsletter,
OM.submitted_after,
OM.transferred_after,
MR.action_after,
OM.submitted_after.as_("submission_delay"),
(OM.transfer_started_after + OM.transfer_completed_after).as_("transfer_delay"),
MR.action_after.as_("action_delay"),
(
OM.submitted_after
+ OM.transfer_started_after
+ OM.transfer_completed_after
+ MR.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.transferred_at,
MR.action_at,
)
.where((OM.docstatus == 1) & (IfNull(MR.status, "") != ""))
.orderby(OM.creation, OM.created_at, order=Order.desc)
Expand Down Expand Up @@ -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 [
{
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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,
},
]
2 changes: 2 additions & 0 deletions mail/patches.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
[post_model_sync]
mail.patches.v1_0.rename_field_dkim_bits
mail.patches.v1_0.create_dkim_key_doc
mail.patches.v1_0.rename_field_transferred_at
mail.patches.v1_0.rename_field_transferred_after
10 changes: 10 additions & 0 deletions mail/patches/v1_0/rename_field_transferred_after.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import frappe
from frappe.model.utils.rename_field import rename_field


def execute():
doctype = "Outgoing Mail"
old_fieldname, new_fieldname = "transferred_after", "transfer_completed_after"

if frappe.db.has_column(doctype, old_fieldname):
rename_field(doctype, old_fieldname, new_fieldname)
10 changes: 10 additions & 0 deletions mail/patches/v1_0/rename_field_transferred_at.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import frappe
from frappe.model.utils.rename_field import rename_field


def execute():
doctype = "Outgoing Mail"
old_fieldname, new_fieldname = "transferred_at", "transfer_completed_at"

if frappe.db.has_column(doctype, old_fieldname):
rename_field(doctype, old_fieldname, new_fieldname)

0 comments on commit 7ba2a93

Please sign in to comment.