From a8deee833536076f98d75ed91f1d85f42216fd94 Mon Sep 17 00:00:00 2001 From: Andretalik Date: Tue, 6 Jun 2017 18:28:10 +0300 Subject: [PATCH] [Feature #145028653] Alerts user only when job is run too often - Fix the bug where a job is flagged as running too often even on its very first ping. - Fix the previously altered tests to accommodate the "often" status. --- hc/api/models.py | 17 ++++++++++++----- hc/api/tests/test_list_checks.py | 2 +- hc/front/tests/test_my_checks.py | 4 ++-- templates/front/my_checks_desktop.html | 4 ++-- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/hc/api/models.py b/hc/api/models.py index 06757f5..7b4e9ad 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -17,7 +17,8 @@ ("up", "Up"), ("down", "Down"), ("new", "New"), - ("paused", "Paused") + ("paused", "Paused"), + ("often", "Often") ) DEFAULT_TIMEOUT = td(days=1) DEFAULT_GRACE = td(hours=1) @@ -85,9 +86,15 @@ def get_status(self): return self.status now = timezone.now() - - if self.last_ping + self.timeout/6 > now: - return "often" + if len(self.ping_set.all().order_by('-created')) > 2: + reversed_grace = (self.timeout/6) + pings_to_check = self.ping_set.all().order_by('-created') + previous_ping = pings_to_check[1].created + if self.last_ping + self.timeout + self.grace > now: + if (self.last_ping - previous_ping) - reversed_grace < self.timeout: + return "often" + else: + return "up" if self.last_ping + self.timeout + self.grace > now: return "up" @@ -95,7 +102,7 @@ def get_status(self): return "down" def in_grace_period(self): - if self.status in ("new", "paused", "often"): + if self.status in ("new", "paused"): return False up_ends = self.last_ping + self.timeout diff --git a/hc/api/tests/test_list_checks.py b/hc/api/tests/test_list_checks.py index 496711c..d36db21 100644 --- a/hc/api/tests/test_list_checks.py +++ b/hc/api/tests/test_list_checks.py @@ -57,7 +57,7 @@ def test_it_works(self): self.assertEqual(checks['Alice 2']['timeout'], 86400) self.assertEqual(checks['Alice 2']['grace'], 3600) self.assertEqual(checks['Alice 2']['ping_url'], self.a2.to_dict()['ping_url']) - self.assertEqual(checks['Alice 2']['status'], "often") + self.assertEqual(checks['Alice 2']['status'], "up") self.assertEqual(checks['Alice 2']['last_ping'], self.a2.to_dict()['last_ping']) self.assertEqual(checks['Alice 2']['n_pings'], self.a2.to_dict()['n_pings']) self.assertEqual(checks['Alice 2']['pause_url'], self.a2.to_dict()['pause_url']) diff --git a/hc/front/tests/test_my_checks.py b/hc/front/tests/test_my_checks.py index 205bff5..c759d1f 100644 --- a/hc/front/tests/test_my_checks.py +++ b/hc/front/tests/test_my_checks.py @@ -26,10 +26,10 @@ def test_it_shows_green_check(self): r = self.client.get("/checks/") # Desktop - self.assertContains(r, "icon-often") + self.assertContains(r, "icon-up") # Mobile - self.assertContains(r, "label-warning") + self.assertContains(r, "label-success") def test_it_shows_red_check(self): self.check.last_ping = timezone.now() - td(days=3) diff --git a/templates/front/my_checks_desktop.html b/templates/front/my_checks_desktop.html index 16c65da..4b9247a 100644 --- a/templates/front/my_checks_desktop.html +++ b/templates/front/my_checks_desktop.html @@ -20,11 +20,11 @@ {% elif check.get_status == "paused" %} + {% elif check.in_grace_period %} + {% elif check.get_status == "often" %} - {% elif check.in_grace_period %} - {% elif check.get_status == "up" %} {% elif check.get_status == "down" %}