Browse Source

Show check counts in JSON "badges". Fixes #251

pull/230/head
Pēteris Caune 6 years ago
parent
commit
1b948f4d5a
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
3 changed files with 41 additions and 8 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +23
    -3
      hc/api/tests/test_badge.py
  3. +17
    -5
      hc/api/views.py

+ 1
- 0
CHANGELOG.md View File

@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
### Improvements ### Improvements
- Add the `prunetokenbucket` management command - Add the `prunetokenbucket` management command
- Show check counts in JSON "badges" (#251)
### Bug Fixes ### Bug Fixes
- Fix badges for tags containing special characters (#240, #237) - Fix badges for tags containing special characters (#240, #237)


+ 23
- 3
hc/api/tests/test_badge.py View File

@ -33,6 +33,14 @@ class BadgeTestCase(BaseTestCase):
self.assertEqual(r.status_code, 204) self.assertEqual(r.status_code, 204)
self.assertEqual(r["Access-Control-Allow-Origin"], "*") self.assertEqual(r["Access-Control-Allow-Origin"], "*")
def test_it_handles_new(self):
r = self.client.get(self.json_url)
doc = r.json()
self.assertEqual(doc["status"], "up")
self.assertEqual(doc["total"], 1)
self.assertEqual(doc["grace"], 0)
self.assertEqual(doc["down"], 0)
def test_it_handles_started_but_down(self): def test_it_handles_started_but_down(self):
self.check.last_start = now() self.check.last_start = now()
self.check.tags = "foo" self.check.tags = "foo"
@ -40,7 +48,11 @@ class BadgeTestCase(BaseTestCase):
self.check.save() self.check.save()
r = self.client.get(self.json_url) r = self.client.get(self.json_url)
self.assertContains(r, "down")
doc = r.json()
self.assertEqual(doc["status"], "down")
self.assertEqual(doc["total"], 1)
self.assertEqual(doc["grace"], 0)
self.assertEqual(doc["down"], 1)
def test_it_shows_grace_badge(self): def test_it_shows_grace_badge(self):
self.check.last_ping = now() - td(days=1, minutes=10) self.check.last_ping = now() - td(days=1, minutes=10)
@ -49,7 +61,11 @@ class BadgeTestCase(BaseTestCase):
self.check.save() self.check.save()
r = self.client.get(self.json_url) r = self.client.get(self.json_url)
self.assertContains(r, "late")
doc = r.json()
self.assertEqual(doc["status"], "late")
self.assertEqual(doc["total"], 1)
self.assertEqual(doc["grace"], 1)
self.assertEqual(doc["down"], 0)
def test_it_shows_started_but_grace_badge(self): def test_it_shows_started_but_grace_badge(self):
self.check.last_start = now() self.check.last_start = now()
@ -59,7 +75,11 @@ class BadgeTestCase(BaseTestCase):
self.check.save() self.check.save()
r = self.client.get(self.json_url) r = self.client.get(self.json_url)
self.assertContains(r, "late")
doc = r.json()
self.assertEqual(doc["status"], "late")
self.assertEqual(doc["total"], 1)
self.assertEqual(doc["grace"], 1)
self.assertEqual(doc["down"], 0)
def test_it_handles_special_characters(self): def test_it_handles_special_characters(self):
self.check.tags = "db@dc1" self.check.tags = "db@dc1"


+ 17
- 5
hc/api/views.py View File

@ -200,7 +200,6 @@ def badge(request, badge_key, signature, tag, format="svg"):
if not check_signature(badge_key, tag, signature): if not check_signature(badge_key, tag, signature):
return HttpResponseNotFound() return HttpResponseNotFound()
status = "up"
q = Check.objects.filter(project__badge_key=badge_key) q = Check.objects.filter(project__badge_key=badge_key)
if tag != "*": if tag != "*":
q = q.filter(tags__contains=tag) q = q.filter(tags__contains=tag)
@ -208,20 +207,33 @@ def badge(request, badge_key, signature, tag, format="svg"):
else: else:
label = settings.MASTER_BADGE_LABEL label = settings.MASTER_BADGE_LABEL
status, total, grace, down = "up", 0, 0, 0
for check in q: for check in q:
if tag != "*" and tag not in check.tags_list(): if tag != "*" and tag not in check.tags_list():
continue continue
total += 1
check_status = check.get_status(with_started=False) check_status = check.get_status(with_started=False)
if status == "up" and check_status == "grace":
status = "late"
if check_status == "down": if check_status == "down":
down += 1
status = "down" status = "down"
break
if format == "svg":
# For SVG badges, we can leave the loop as soon as we
# find the first "down"
break
elif check_status == "grace":
grace += 1
if status == "up":
status = "late"
if format == "json": if format == "json":
return JsonResponse({"status": status})
return JsonResponse({
"status": status,
"total": total,
"grace": grace,
"down": down
})
svg = get_badge_svg(label, status) svg = get_badge_svg(label, status)
return HttpResponse(svg, content_type="image/svg+xml") return HttpResponse(svg, content_type="image/svg+xml")


Loading…
Cancel
Save