Skip to content

Commit

Permalink
#2159 - Update Total Time For Twilio Delivery Status (#2168)
Browse files Browse the repository at this point in the history
  • Loading branch information
k-macmillan authored Dec 6, 2024
1 parent c746363 commit e15b799
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
11 changes: 10 additions & 1 deletion app/celery/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,19 @@ def log_notification_total_time(
"""Logs how long it took a notification to go from created to delivered"""
if status == NOTIFICATION_DELIVERED:
end_time = event_timestamp or datetime.now(timezone.utc).replace(tzinfo=None)
total_time = (end_time - start_time).total_seconds()

# Twilio RawDlrDoneDate can make this negative
# https://www.twilio.com/en-us/changelog/addition-of-rawdlrdonedate-to-delivered-and-undelivered-status-webhooks
corrected_total_time = (
total_time
if total_time > 0.0
else (datetime.now(timezone.utc).replace(tzinfo=None) - start_time).total_seconds()
)
current_app.logger.info(
'notification %s took %ss total time to reach %s status - %s',
notification_id,
(end_time - start_time).total_seconds(),
corrected_total_time,
status,
provider,
)
30 changes: 26 additions & 4 deletions tests/app/celery/test_common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import datetime
from datetime import datetime, timedelta, timezone
from uuid import uuid4

from freezegun import freeze_time
Expand All @@ -11,6 +11,7 @@
NOTIFICATION_PENDING,
NOTIFICATION_SENDING,
NOTIFICATION_SENT,
TWILIO_PROVIDER,
)


Expand All @@ -23,7 +24,7 @@ def test_ut_log_notification_total_time(
mock_logger = mocker.patch('app.celery.common.current_app.logger.info')
notification_id = uuid4()
with freeze_time('2024-04-04 17:16:43'):
created_at = datetime.datetime.fromisoformat('2024-04-04 17:16:41')
created_at = datetime.fromisoformat('2024-04-04 17:16:41')

log_notification_total_time(
notification_id=notification_id,
Expand All @@ -35,7 +36,7 @@ def test_ut_log_notification_total_time(
mock_logger.assert_called_once_with(
'notification %s took %ss total time to reach %s status - %s',
notification_id,
(datetime.datetime.now() - created_at).total_seconds(),
(datetime.now() - created_at).total_seconds(),
NOTIFICATION_DELIVERED,
provider,
)
Expand All @@ -60,9 +61,30 @@ def test_ut_skip_log_notification_total_time(
mock_logger = mocker.patch('app.celery.common.current_app.logger.info')
log_notification_total_time(
notification_id=uuid4(),
start_time=datetime.datetime.now(),
start_time=datetime.now(),
status=status,
provider=provider,
)

mock_logger.assert_not_called()


def test_log_total_time_negative_value(
mocker,
client,
):
mock_logger = mocker.patch('app.celery.common.current_app.logger.info')
start_time = datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(seconds=1)
event_timestamp = datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(seconds=3)

notification_id = str(uuid4())
log_notification_total_time(
notification_id,
start_time,
NOTIFICATION_DELIVERED,
TWILIO_PROVIDER,
event_timestamp,
)

# Test that the total time logged is > 0
assert float(mock_logger.call_args[0][2]) > 0

0 comments on commit e15b799

Please sign in to comment.