Skip to content

Commit

Permalink
Change logic to save status for Surge Alerts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rup-Narayan-Rajbanshi committed Jul 12, 2024
1 parent 5063a01 commit 80f1a27
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 25 deletions.
14 changes: 6 additions & 8 deletions notifications/management/commands/update_surge_alert_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
)
Expand Down
16 changes: 7 additions & 9 deletions notifications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
51 changes: 43 additions & 8 deletions notifications/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,23 @@ 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),
)
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)
Expand Down Expand Up @@ -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)

0 comments on commit 80f1a27

Please sign in to comment.