Skip to content

Commit

Permalink
[Feature #145028653] Alerts user only when job is run too often
Browse files Browse the repository at this point in the history
 - 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.
  • Loading branch information
Andretalik committed Jun 6, 2017
1 parent e8a9621 commit a8deee8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
17 changes: 12 additions & 5 deletions hc/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -85,17 +86,23 @@ 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"

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
Expand Down
2 changes: 1 addition & 1 deletion hc/api/tests/test_list_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
4 changes: 2 additions & 2 deletions hc/front/tests/test_my_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions templates/front/my_checks_desktop.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
{% elif check.get_status == "paused" %}
<span class="status icon-paused"
data-toggle="tooltip" title="Monitoring paused. Ping to resume."></span>
{% elif check.in_grace_period %}
<span class="status icon-grace"></span>
{% elif check.get_status == "often" %}
<span class="status icon-often" data-toggle="tooltip"
title="Ping is up but running too often."></span>
{% elif check.in_grace_period %}
<span class="status icon-grace"></span>
{% elif check.get_status == "up" %}
<span class="status icon-up"></span>
{% elif check.get_status == "down" %}
Expand Down

0 comments on commit a8deee8

Please sign in to comment.