From a97f23190effeef70b82e456a85460190f3b92a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Sat, 9 Jul 2016 17:06:00 +0300 Subject: [PATCH] More data in API responses. --- hc/api/models.py | 19 ++++++++++++++ hc/api/tests/test_create_check.py | 8 +++++- hc/api/tests/test_list_checks.py | 10 ++++++++ hc/api/views.py | 25 ++++++------------- hc/front/views.py | 2 +- .../front/snippets/create_check_response.html | 9 ++++++- .../front/snippets/create_check_response.txt | 9 ++++++- .../front/snippets/list_checks_response.html | 22 ++++++++++------ .../front/snippets/list_checks_response.txt | 22 ++++++++++------ 9 files changed, 88 insertions(+), 38 deletions(-) diff --git a/hc/api/models.py b/hc/api/models.py index 2dee4d82..d41fb3a4 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -107,6 +107,25 @@ class Check(models.Model): def tags_list(self): return [t.strip() for t in self.tags.split(" ") if t.strip()] + def to_dict(self): + result = { + "name": self.name, + "ping_url": self.url(), + "tags": self.tags, + "timeout": int(self.timeout.total_seconds()), + "grace": int(self.grace.total_seconds()), + "n_pings": self.n_pings + } + + if self.last_ping: + result["last_ping"] = self.last_ping.isoformat() + result["next_ping"] = (self.last_ping + self.timeout).isoformat() + else: + result["last_ping"] = None + result["next_ping"] = None + + return result + class Ping(models.Model): n = models.IntegerField(null=True) diff --git a/hc/api/tests/test_create_check.py b/hc/api/tests/test_create_check.py index 860db1d3..fcc815a7 100644 --- a/hc/api/tests/test_create_check.py +++ b/hc/api/tests/test_create_check.py @@ -30,7 +30,13 @@ class CreateCheckTestCase(BaseTestCase): }) self.assertEqual(r.status_code, 201) - self.assertTrue("ping_url" in r.json()) + + doc = r.json() + assert "ping_url" in doc + self.assertEqual(doc["name"], "Foo") + self.assertEqual(doc["tags"], "bar,baz") + self.assertEqual(doc["last_ping"], None) + self.assertEqual(doc["n_pings"], 0) self.assertEqual(Check.objects.count(), 1) check = Check.objects.get() diff --git a/hc/api/tests/test_list_checks.py b/hc/api/tests/test_list_checks.py index b1e9d162..41414f9c 100644 --- a/hc/api/tests/test_list_checks.py +++ b/hc/api/tests/test_list_checks.py @@ -1,5 +1,6 @@ import json from datetime import timedelta as td +from django.utils.timezone import now from hc.api.models import Check from hc.test import BaseTestCase @@ -10,9 +11,13 @@ class ListChecksTestCase(BaseTestCase): def setUp(self): super(ListChecksTestCase, self).setUp() + self.now = now() + self.a1 = Check(user=self.alice, name="Alice 1") self.a1.timeout = td(seconds=3600) self.a1.grace = td(seconds=900) + self.a1.last_ping = self.now + self.a1.n_pings = 1 self.a1.save() self.a2 = Check(user=self.alice, name="Alice 2") @@ -36,6 +41,11 @@ class ListChecksTestCase(BaseTestCase): self.assertEqual(checks["Alice 1"]["timeout"], 3600) self.assertEqual(checks["Alice 1"]["grace"], 900) self.assertEqual(checks["Alice 1"]["ping_url"], self.a1.url()) + self.assertEqual(checks["Alice 1"]["last_ping"], self.now.isoformat()) + self.assertEqual(checks["Alice 1"]["n_pings"], 1) + + next_ping = self.now + td(seconds=3600) + self.assertEqual(checks["Alice 1"]["next_ping"], next_ping.isoformat()) self.assertEqual(checks["Alice 2"]["timeout"], 86400) self.assertEqual(checks["Alice 2"]["grace"], 3600) diff --git a/hc/api/views.py b/hc/api/views.py index 0058815c..8f0fd354 100644 --- a/hc/api/views.py +++ b/hc/api/views.py @@ -50,17 +50,10 @@ def ping(request, code): @validate_json(schemas.check) def checks(request): if request.method == "GET": - code = 200 - response = { - "checks": [{ - "name": check.name, - "ping_url": check.url(), - "tags": check.tags, - "timeout": int(check.timeout.total_seconds()), - "grace": int(check.grace.total_seconds()), - # "channels": check.channels, - } for check in Check.objects.filter(user=request.user)] - } + q = Check.objects.filter(user=request.user) + doc = {"checks": [check.to_dict() for check in q]} + return JsonResponse(doc) + elif request.method == "POST": check = Check(user=request.user) check.name = str(request.json.get("name", "")) @@ -77,14 +70,10 @@ def checks(request): if request.json.get("channels") == "*": check.assign_all_channels() - code = 201 - response = { - "ping_url": check.url() - } - else: - return HttpResponse(status=405) + return JsonResponse(check.to_dict(), status=201) - return JsonResponse(response, status=code) + # If request is neither GET nor POST, return "405 Method not allowed" + return HttpResponse(status=405) @never_cache diff --git a/hc/front/views.py b/hc/front/views.py index 5f88622a..b06249da 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -400,7 +400,7 @@ def add_slack_btn(request): channel.value = result.text channel.save() channel.assign_all_checks() - messages.info(request, "The Slack integration has been added!") + messages.success(request, "The Slack integration has been added!") else: s = doc.get("error") messages.warning(request, "Error message from slack: %s" % s) diff --git a/templates/front/snippets/create_check_response.html b/templates/front/snippets/create_check_response.html index 0d76255d..0b752ec5 100644 --- a/templates/front/snippets/create_check_response.html +++ b/templates/front/snippets/create_check_response.html @@ -1,4 +1,11 @@
{
-  "ping_url": "{{ PING_ENDPOINT }}20f2d3d0-efe4-4cc1-a114-a186a225de50"
+  "grace": 60,
+  "last_ping": null,
+  "n_pings": 0,
+  "name": "Backups",
+  "next_ping": null,
+  "ping_url": "{{ PING_ENDPOINT }}0c8983c9-9d73-446f-adb5-0641fdacc9d4",
+  "tags": "prod www",
+  "timeout": 3600
 }
 
diff --git a/templates/front/snippets/create_check_response.txt b/templates/front/snippets/create_check_response.txt index 2dda1808..dbde03a3 100644 --- a/templates/front/snippets/create_check_response.txt +++ b/templates/front/snippets/create_check_response.txt @@ -1,3 +1,10 @@ { - "ping_url": "PING_ENDPOINT20f2d3d0-efe4-4cc1-a114-a186a225de50" + "grace": 60, + "last_ping": null, + "n_pings": 0, + "name": "Backups", + "next_ping": null, + "ping_url": "PING_ENDPOINT0c8983c9-9d73-446f-adb5-0641fdacc9d4", + "tags": "prod www", + "timeout": 3600 } \ No newline at end of file diff --git a/templates/front/snippets/list_checks_response.html b/templates/front/snippets/list_checks_response.html index d9b0da8c..91ccfdfa 100644 --- a/templates/front/snippets/list_checks_response.html +++ b/templates/front/snippets/list_checks_response.html @@ -1,18 +1,24 @@
{
   "checks": [
     {
-      "ping_url": "{{ PING_ENDPOINT }}848f3002-266b-482a-89ad-9d66a11aa2fb",
       "grace": 900,
-      "name": "API test 1",
-      "timeout": 3600,
-      "tags": "foo"
+      "last_ping": "2016-07-09T13:58:43.366568+00:00",
+      "n_pings": 1,
+      "name": "Api test 1",
+      "next_ping": "2016-07-09T14:58:43.366568+00:00",
+      "ping_url": "{{ PING_ENDPOINT }}25c55e7c-8092-4d21-ad06-7dacfbb6fc10",
+      "tags": "foo",
+      "timeout": 3600
     },
     {
-      "ping_url": "{{ PING_ENDPOINT }}20324f81-5966-4e75-9734-8440df52ed75",
       "grace": 60,
-      "name": "API test 2",
-      "timeout": 60,
-      "tags": "bar,baz"
+      "last_ping": null,
+      "n_pings": 0,
+      "name": "Api test 2",
+      "next_ping": null,
+      "ping_url": "{{ PING_ENDPOINT }}7e1b6e61-b16f-4671-bae3-e3233edd1b5e",
+      "tags": "bar baz",
+      "timeout": 60
     }
   ]
 }
diff --git a/templates/front/snippets/list_checks_response.txt b/templates/front/snippets/list_checks_response.txt
index 08f62fdc..c7464c77 100644
--- a/templates/front/snippets/list_checks_response.txt
+++ b/templates/front/snippets/list_checks_response.txt
@@ -1,18 +1,24 @@
 {
   "checks": [
     {
-      "ping_url": "PING_ENDPOINT848f3002-266b-482a-89ad-9d66a11aa2fb",
       "grace": 900,
-      "name": "API test 1",
-      "timeout": 3600,
-      "tags": "foo"
+      "last_ping": "2016-07-09T13:58:43.366568+00:00",
+      "n_pings": 1,
+      "name": "Api test 1",
+      "next_ping": "2016-07-09T14:58:43.366568+00:00",
+      "ping_url": "PING_ENDPOINT25c55e7c-8092-4d21-ad06-7dacfbb6fc10",
+      "tags": "foo",
+      "timeout": 3600
     },
     {
-      "ping_url": "PING_ENDPOINT20324f81-5966-4e75-9734-8440df52ed75",
       "grace": 60,
-      "name": "API test 2",
-      "timeout": 60,
-      "tags": "bar,baz"
+      "last_ping": null,
+      "n_pings": 0,
+      "name": "Api test 2",
+      "next_ping": null,
+      "ping_url": "PING_ENDPOINT7e1b6e61-b16f-4671-bae3-e3233edd1b5e",
+      "tags": "bar baz",
+      "timeout": 60
     }
   ]
 }
\ No newline at end of file