forked from GithubBackups/healthchecks
Tag labels in "My Checks" page and SVG badges should ignore the "started" state.
This commit is contained in:
parent
5f9ebb178c
commit
93405cc286
@ -166,13 +166,13 @@ class Check(models.Model):
|
||||
|
||||
return timezone.now() >= down_after
|
||||
|
||||
def get_status(self, now=None):
|
||||
def get_status(self, now=None, with_started=True):
|
||||
""" Return current status for display. """
|
||||
|
||||
if now is None:
|
||||
now = timezone.now()
|
||||
|
||||
if self.last_start:
|
||||
if self.last_start and with_started:
|
||||
if now >= self.last_start + self.grace:
|
||||
return "down"
|
||||
else:
|
||||
|
@ -1,5 +1,8 @@
|
||||
from datetime import timedelta as td
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.signing import base64_hmac
|
||||
from django.utils.timezone import now
|
||||
|
||||
from hc.api.models import Check
|
||||
from hc.test import BaseTestCase
|
||||
@ -11,16 +14,17 @@ class BadgeTestCase(BaseTestCase):
|
||||
super(BadgeTestCase, self).setUp()
|
||||
self.check = Check.objects.create(user=self.alice, tags="foo bar")
|
||||
|
||||
sig = base64_hmac(str(self.alice.username), "foo", settings.SECRET_KEY)
|
||||
sig = sig[:8]
|
||||
self.svg_url = "/badge/%s/%s/foo.svg" % (self.alice.username, sig)
|
||||
self.json_url = "/badge/%s/%s/foo.json" % (self.alice.username, sig)
|
||||
|
||||
def test_it_rejects_bad_signature(self):
|
||||
r = self.client.get("/badge/%s/12345678/foo.svg" % self.alice.username)
|
||||
assert r.status_code == 404
|
||||
|
||||
def test_it_returns_svg(self):
|
||||
sig = base64_hmac(str(self.alice.username), "foo", settings.SECRET_KEY)
|
||||
sig = sig[:8]
|
||||
url = "/badge/%s/%s/foo.svg" % (self.alice.username, sig)
|
||||
|
||||
r = self.client.get(url)
|
||||
r = self.client.get(self.svg_url)
|
||||
self.assertEqual(r["Access-Control-Allow-Origin"], "*")
|
||||
self.assertContains(r, "#4c1")
|
||||
|
||||
@ -32,3 +36,31 @@ class BadgeTestCase(BaseTestCase):
|
||||
r = self.client.options(url)
|
||||
self.assertEqual(r.status_code, 204)
|
||||
self.assertEqual(r["Access-Control-Allow-Origin"], "*")
|
||||
|
||||
def test_it_handles_started_but_down(self):
|
||||
self.check.last_start = now()
|
||||
self.check.tags = "foo"
|
||||
self.check.status = "down"
|
||||
self.check.save()
|
||||
|
||||
r = self.client.get(self.json_url)
|
||||
self.assertContains(r, "down")
|
||||
|
||||
def test_it_shows_grace_badge(self):
|
||||
self.check.last_ping = now() - td(days=1, minutes=10)
|
||||
self.check.tags = "foo"
|
||||
self.check.status = "up"
|
||||
self.check.save()
|
||||
|
||||
r = self.client.get(self.json_url)
|
||||
self.assertContains(r, "late")
|
||||
|
||||
def test_it_shows_started_but_grace_badge(self):
|
||||
self.check.last_start = now()
|
||||
self.check.last_ping = now() - td(days=1, minutes=10)
|
||||
self.check.tags = "foo"
|
||||
self.check.status = "up"
|
||||
self.check.save()
|
||||
|
||||
r = self.client.get(self.json_url)
|
||||
self.assertContains(r, "late")
|
||||
|
@ -211,7 +211,7 @@ def badge(request, username, signature, tag, format="svg"):
|
||||
if tag != "*" and tag not in check.tags_list():
|
||||
continue
|
||||
|
||||
check_status = check.get_status()
|
||||
check_status = check.get_status(with_started=False)
|
||||
if status == "up" and check_status == "grace":
|
||||
status = "late"
|
||||
|
||||
|
@ -65,3 +65,34 @@ class MyChecksTestCase(BaseTestCase):
|
||||
|
||||
self.profile.refresh_from_db()
|
||||
self.assertEqual(self.profile.sort, "created")
|
||||
|
||||
def test_it_shows_started_but_down_badge(self):
|
||||
self.check.last_start = timezone.now()
|
||||
self.check.tags = "foo"
|
||||
self.check.status = "down"
|
||||
self.check.save()
|
||||
|
||||
self.client.login(username="alice@example.org", password="password")
|
||||
r = self.client.get("/checks/")
|
||||
self.assertContains(r, """<div class="btn btn-xs down ">foo</div>""")
|
||||
|
||||
def test_it_shows_grace_badge(self):
|
||||
self.check.last_ping = timezone.now() - td(days=1, minutes=10)
|
||||
self.check.tags = "foo"
|
||||
self.check.status = "up"
|
||||
self.check.save()
|
||||
|
||||
self.client.login(username="alice@example.org", password="password")
|
||||
r = self.client.get("/checks/")
|
||||
self.assertContains(r, """<div class="btn btn-xs grace ">foo</div>""")
|
||||
|
||||
def test_it_shows_grace_started_badge(self):
|
||||
self.check.last_start = timezone.now()
|
||||
self.check.last_ping = timezone.now() - td(days=1, minutes=10)
|
||||
self.check.tags = "foo"
|
||||
self.check.status = "up"
|
||||
self.check.save()
|
||||
|
||||
self.client.login(username="alice@example.org", password="password")
|
||||
r = self.client.get("/checks/")
|
||||
self.assertContains(r, """<div class="btn btn-xs grace ">foo</div>""")
|
||||
|
@ -42,7 +42,7 @@ EVENTS_TMPL = get_template("front/details_events.html")
|
||||
def _tags_statuses(checks):
|
||||
tags, down, grace, num_down = {}, {}, {}, 0
|
||||
for check in checks:
|
||||
status = check.get_status()
|
||||
status = check.get_status(with_started=False)
|
||||
|
||||
if status == "down":
|
||||
num_down += 1
|
||||
|
Loading…
x
Reference in New Issue
Block a user