From 1f70f568190e91c0a3cd8c56e3d04c6214a2d015 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Mon, 20 Jun 2016 20:35:02 +0300 Subject: [PATCH] Check model gets "in_grace_period" method. It replaces the transient "grace" status. Also, remove references of the obsolete "paused" status. --- hc/api/models.py | 10 +++--- hc/api/tests/test_sendalerts.py | 9 ++++++ hc/front/tests/test_my_checks.py | 44 ++++++++++++++++++++++++++ hc/front/views.py | 2 +- templates/emails/alert-body-html.html | 5 +-- templates/emails/report-body-html.html | 5 +-- templates/front/my_checks_desktop.html | 4 +-- templates/front/my_checks_mobile.html | 4 +-- 8 files changed, 64 insertions(+), 19 deletions(-) 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 %} diff --git a/templates/emails/report-body-html.html b/templates/emails/report-body-html.html index dcf780e1..92edcc7a 100644 --- a/templates/emails/report-body-html.html +++ b/templates/emails/report-body-html.html @@ -19,7 +19,6 @@ } .new { background: #AAA; } - .paused { background: #AAA; } .up { background: #5cb85c; } .grace { background: #f0ad4e; } .down { background: #d9534f; } @@ -49,12 +48,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 %} diff --git a/templates/front/my_checks_desktop.html b/templates/front/my_checks_desktop.html index 04236c9a..9161e892 100644 --- a/templates/front/my_checks_desktop.html +++ b/templates/front/my_checks_desktop.html @@ -18,12 +18,10 @@ {% elif check.get_status == "up" %} - {% elif check.get_status == "grace" %} + {% elif check.in_grace_period %} {% elif check.get_status == "down" %} - {% elif check.get_status == "paused" %} - {% endif %} diff --git a/templates/front/my_checks_mobile.html b/templates/front/my_checks_mobile.html index 909071cc..ebaa4db6 100644 --- a/templates/front/my_checks_mobile.html +++ b/templates/front/my_checks_mobile.html @@ -27,12 +27,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 %}