diff --git a/hc/api/models.py b/hc/api/models.py index 60a000bd..310cc7f4 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -80,7 +80,7 @@ class Check(models.Model): return errors def get_status(self): - if self.status in ("new", "paused"): + if self.status == "new": return self.status now = timezone.now() @@ -88,11 +88,13 @@ class Check(models.Model): if self.last_ping + self.timeout > now: return "up" - if self.last_ping + self.timeout + self.grace > now: - return "grace" - return "down" + def in_grace_period(self): + up_ends = self.last_ping + self.timeout + grace_ends = up_ends + self.grace + return up_ends < timezone.now() < grace_ends + def assign_all_channels(self): if self.user: channels = Channel.objects.filter(user=self.user) diff --git a/hc/api/tests/test_sendalerts.py b/hc/api/tests/test_sendalerts.py index f21ca11e..f1d83159 100644 --- a/hc/api/tests/test_sendalerts.py +++ b/hc/api/tests/test_sendalerts.py @@ -29,3 +29,12 @@ class SendAlertsTestCase(BaseTestCase): handled_names.append(args[0].name) assert set(names) == set(handled_names) + + def test_it_handles_grace_period(self): + check = Check(user=self.alice, status="up") + # 1 day 30 minutes after ping the check is in grace period: + check.last_ping = timezone.now() - timedelta(days=1, minutes=30) + check.save() + + # Expect no exceptions-- + Command().handle_one(check) diff --git a/hc/front/tests/test_my_checks.py b/hc/front/tests/test_my_checks.py index 5e3e704b..e622c216 100644 --- a/hc/front/tests/test_my_checks.py +++ b/hc/front/tests/test_my_checks.py @@ -1,5 +1,7 @@ from hc.api.models import Check from hc.test import BaseTestCase +from datetime import timedelta as td +from django.utils import timezone class MyChecksTestCase(BaseTestCase): @@ -14,3 +16,45 @@ class MyChecksTestCase(BaseTestCase): self.client.login(username=email, password="password") r = self.client.get("/checks/") self.assertContains(r, "Alice Was Here", status_code=200) + + def test_it_shows_green_check(self): + self.check.last_ping = timezone.now() + self.check.status = "up" + self.check.save() + + self.client.login(username="alice@example.org", password="password") + r = self.client.get("/checks/") + + # Desktop + self.assertContains(r, "glyphicon-ok-sign") + + # Mobile + self.assertContains(r, "label-success") + + def test_it_shows_red_check(self): + self.check.last_ping = timezone.now() - td(days=3) + self.check.status = "up" + self.check.save() + + self.client.login(username="alice@example.org", password="password") + r = self.client.get("/checks/") + + # Desktop + self.assertContains(r, "glyphicon-exclamation-sign") + + # Mobile + self.assertContains(r, "label-danger") + + def test_it_shows_amber_check(self): + self.check.last_ping = timezone.now() - td(days=1, minutes=30) + self.check.status = "up" + self.check.save() + + self.client.login(username="alice@example.org", password="password") + r = self.client.get("/checks/") + + # Desktop + self.assertContains(r, "glyphicon-exclamation-sign grace") + + # Mobile + self.assertContains(r, "label-warning") diff --git a/hc/front/views.py b/hc/front/views.py index 8145da46..2e1d17aa 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -42,7 +42,7 @@ def my_checks(request): if status == "down": down_tags.add(tag) - elif status == "grace": + elif check.in_grace_period(): grace_tags.add(tag) ctx = { diff --git a/templates/emails/alert-body-html.html b/templates/emails/alert-body-html.html index d4ed9f2d..b6a8f1d9 100644 --- a/templates/emails/alert-body-html.html +++ b/templates/emails/alert-body-html.html @@ -19,7 +19,6 @@ } .new { background: #AAA; } - .paused { background: #AAA; } .up { background: #5cb85c; } .grace { background: #f0ad4e; } .down { background: #d9534f; } @@ -52,12 +51,10 @@ NEW {% elif check.get_status == "up" %} UP - {% elif check.get_status == "grace" %} + {% elif check.in_grace_period %} LATE {% elif check.get_status == "down" %} DOWN - {% elif check.get_status == "paused" %} - PAUSED {% endif %}