From 80f1a273285ea497124687d4f073365d189f869d Mon Sep 17 00:00:00 2001 From: rup-narayan-rajbanshi Date: Thu, 11 Jul 2024 16:44:13 +0545 Subject: [PATCH] Change logic to save status for Surge Alerts. --- .../commands/update_surge_alert_status.py | 14 +++-- notifications/models.py | 16 +++--- notifications/tests.py | 51 ++++++++++++++++--- 3 files changed, 56 insertions(+), 25 deletions(-) diff --git a/notifications/management/commands/update_surge_alert_status.py b/notifications/management/commands/update_surge_alert_status.py index 0fe04dbcb1..6c64c8b5c2 100644 --- a/notifications/management/commands/update_surge_alert_status.py +++ b/notifications/management/commands/update_surge_alert_status.py @@ -2,7 +2,6 @@ from django.core.management.base import BaseCommand from django.db import models -from django.utils import timezone from sentry_sdk.crons import monitor from api.models import CronJob, CronJobStatus @@ -15,23 +14,22 @@ class Command(BaseCommand): """ Updating the Surge Alert Status according: - If the alert status is marked as stood_down, then the status is Stood Down. - If the closing timestamp (closes) is earlier than the current date, the status is displayed as Closed. - Otherwise, it is displayed as Open. + If molnix_status is active, status should Open, + If molnix_status is archived, status should be Closed, + If molnix_status is unfilled, status should be Stood Down, """ help = "Update surge alert status" @monitor(monitor_slug=SentryMonitor.UPDATE_SURGE_ALERT_STATUS) def handle(self, *args, **options): - now = timezone.now() try: logger.info("Updating Surge alerts status") SurgeAlert.objects.update( status=models.Case( - models.When(is_stood_down=True, then=models.Value(SurgeAlertStatus.STOOD_DOWN)), - models.When(closes__lt=now, then=models.Value(SurgeAlertStatus.CLOSED)), - models.When(closes__gte=now, then=models.Value(SurgeAlertStatus.OPEN)), + models.When(molnix_status="active", then=models.Value(SurgeAlertStatus.OPEN)), + models.When(molnix_status="archived", then=models.Value(SurgeAlertStatus.CLOSED)), + models.When(molnix_status="unfilled", then=models.Value(SurgeAlertStatus.STOOD_DOWN)), default=models.F("status"), output_field=models.IntegerField(), ) diff --git a/notifications/models.py b/notifications/models.py index dcd82950ed..4418a9cf62 100644 --- a/notifications/models.py +++ b/notifications/models.py @@ -74,20 +74,18 @@ class Meta: def save(self, *args, **kwargs): """ - If the alert status is marked as stood_down, then the status is Stood Down. - If the closing timestamp (closes) is earlier than the current date, the status is displayed as Closed. - Otherwise, it is displayed as Open. + A molnix_status of active should be shown as Open + A molnix_status of archived should be shown as Closed + A molnix_status of unfilled should be shown as Stood Down """ - # On save, if `created` is not set, make it the current time if (not self.id and not self.created_at) or (self.created_at > timezone.now()): self.created_at = timezone.now() - self.is_stood_down = self.molnix_status == "unfilled" - if self.is_stood_down: + if self.molnix_status == "active": + self.status = SurgeAlertStatus.OPEN + elif self.molnix_status == "unfilled": self.status = SurgeAlertStatus.STOOD_DOWN - elif self.closes and self.closes < timezone.now(): - self.status = SurgeAlertStatus.CLOSED else: - self.status = SurgeAlertStatus.OPEN + self.status = SurgeAlertStatus.CLOSED return super(SurgeAlert, self).save(*args, **kwargs) def __str__(self): diff --git a/notifications/tests.py b/notifications/tests.py index 167a7fb489..196fcd988b 100644 --- a/notifications/tests.py +++ b/notifications/tests.py @@ -162,12 +162,14 @@ def test_surge_alert_status(self): message="CEA Coordinator, Floods, Atlantis", country=country_1, molnix_tags=[molnix_tag_1, molnix_tag_2], + molnix_status="active", opens=timezone.now() - timedelta(days=2), closes=timezone.now() + timedelta(days=5), ) alert2 = SurgeAlertFactory.create( message="WASH Coordinator, Earthquake, Neptunus", country=country_2, + molnix_status="archived", molnix_tags=[molnix_tag_1, molnix_tag_3], opens=timezone.now() - timedelta(days=2), closes=timezone.now() - timedelta(days=1), @@ -175,8 +177,8 @@ def test_surge_alert_status(self): alert3 = SurgeAlertFactory.create( message="New One", country=country_2, - molnix_tags=[molnix_tag_1, molnix_tag_3], molnix_status="unfilled", + molnix_tags=[molnix_tag_1, molnix_tag_3], ) self.assertEqual(alert1.status, SurgeAlertStatus.OPEN) @@ -210,29 +212,62 @@ def test_update_alert_status_command(self): molnix_tag_2 = MolnixTagFactory.create(name="L-FRA") molnix_tag_3 = MolnixTagFactory.create(name="AMER") + # Override the original save method to create dummy data without restriction + original_save = SurgeAlert.save + + def mocked_save(self, *args, **kwargs): + original_save(self, *args, **kwargs) + + SurgeAlert.save = mocked_save + alert1 = SurgeAlertFactory.create( message="CEA Coordinator, Floods, Atlantis", country=country_1, + molnix_status="unfilled", molnix_tags=[molnix_tag_1, molnix_tag_2], opens=timezone.now() - timedelta(days=2), closes=timezone.now() + timedelta(seconds=5), + status=SurgeAlertStatus.CLOSED, ) + alert2 = SurgeAlertFactory.create( message="WASH Coordinator, Earthquake, Neptunus", country=country_2, + molnix_status="unfilled", molnix_tags=[molnix_tag_1, molnix_tag_3], opens=timezone.now() - timedelta(days=2), closes=timezone.now() - timedelta(days=1), + status=SurgeAlertStatus.STOOD_DOWN, ) - self.assertEqual(alert1.status, SurgeAlertStatus.OPEN) - self.assertEqual(alert2.status, SurgeAlertStatus.CLOSED) + + alert3 = SurgeAlertFactory.create( + message="WASH Coordinator, Earthquake, Neptunus", + country=country_2, + molnix_status="archived", + molnix_tags=[molnix_tag_1, molnix_tag_2], + opens=timezone.now() - timedelta(days=4), + closes=timezone.now() - timedelta(days=1), + status=SurgeAlertStatus.OPEN, + ) + + alert4 = SurgeAlertFactory.create( + message="WASH Coordinator, Earthquake, Neptunus", + country=country_2, + molnix_status="active", + molnix_tags=[molnix_tag_1, molnix_tag_2, molnix_tag_3], + opens=timezone.now() - timedelta(days=3), + closes=timezone.now() - timedelta(days=1), + status=SurgeAlertStatus.OPEN, + ) + + # Restore the original save method after the test + SurgeAlert.save = original_save with patch("django.utils.timezone.now") as mock_now: mock_now.return_value = datetime.now() + timedelta(days=1) call_command("update_surge_alert_status") - alert1.refresh_from_db() - alert2.refresh_from_db() - - self.assertEqual(alert1.status, SurgeAlertStatus.CLOSED) - self.assertEqual(alert2.status, SurgeAlertStatus.CLOSED) + self.assertEqual(alert1.status, SurgeAlertStatus.STOOD_DOWN) + self.assertEqual(alert2.status, SurgeAlertStatus.STOOD_DOWN) + self.assertEqual(alert3.status, SurgeAlertStatus.CLOSED) + self.assertEqual(alert4.status, SurgeAlertStatus.OPEN)