Browse Source

More data in API responses.

pull/72/head
Pēteris Caune 8 years ago
parent
commit
a97f23190e
9 changed files with 88 additions and 38 deletions
  1. +19
    -0
      hc/api/models.py
  2. +7
    -1
      hc/api/tests/test_create_check.py
  3. +10
    -0
      hc/api/tests/test_list_checks.py
  4. +7
    -18
      hc/api/views.py
  5. +1
    -1
      hc/front/views.py
  6. +8
    -1
      templates/front/snippets/create_check_response.html
  7. +8
    -1
      templates/front/snippets/create_check_response.txt
  8. +14
    -8
      templates/front/snippets/list_checks_response.html
  9. +14
    -8
      templates/front/snippets/list_checks_response.txt

+ 19
- 0
hc/api/models.py View File

@ -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)


+ 7
- 1
hc/api/tests/test_create_check.py View File

@ -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()


+ 10
- 0
hc/api/tests/test_list_checks.py View File

@ -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)


+ 7
- 18
hc/api/views.py View File

@ -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


+ 1
- 1
hc/front/views.py View File

@ -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)


+ 8
- 1
templates/front/snippets/create_check_response.html View File

@ -1,4 +1,11 @@
<div class="highlight"><pre><span></span><span class="p">{</span>
<span class="nt">&quot;ping_url&quot;</span><span class="p">:</span> <span class="s2">&quot;{{ PING_ENDPOINT }}20f2d3d0-efe4-4cc1-a114-a186a225de50&quot;</span>
<span class="nt">&quot;grace&quot;</span><span class="p">:</span> <span class="mi">60</span><span class="p">,</span>
<span class="nt">&quot;last_ping&quot;</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
<span class="nt">&quot;n_pings&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
<span class="nt">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;Backups&quot;</span><span class="p">,</span>
<span class="nt">&quot;next_ping&quot;</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
<span class="nt">&quot;ping_url&quot;</span><span class="p">:</span> <span class="s2">&quot;{{ PING_ENDPOINT }}0c8983c9-9d73-446f-adb5-0641fdacc9d4&quot;</span><span class="p">,</span>
<span class="nt">&quot;tags&quot;</span><span class="p">:</span> <span class="s2">&quot;prod www&quot;</span><span class="p">,</span>
<span class="nt">&quot;timeout&quot;</span><span class="p">:</span> <span class="mi">3600</span>
<span class="p">}</span>
</pre></div>

+ 8
- 1
templates/front/snippets/create_check_response.txt View File

@ -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
}

+ 14
- 8
templates/front/snippets/list_checks_response.html View File

@ -1,18 +1,24 @@
<div class="highlight"><pre><span></span><span class="p">{</span>
<span class="nt">&quot;checks&quot;</span><span class="p">:</span> <span class="p">[</span>
<span class="p">{</span>
<span class="nt">&quot;ping_url&quot;</span><span class="p">:</span> <span class="s2">&quot;{{ PING_ENDPOINT }}848f3002-266b-482a-89ad-9d66a11aa2fb&quot;</span><span class="p">,</span>
<span class="nt">&quot;grace&quot;</span><span class="p">:</span> <span class="mi">900</span><span class="p">,</span>
<span class="nt">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;API test 1&quot;</span><span class="p">,</span>
<span class="nt">&quot;timeout&quot;</span><span class="p">:</span> <span class="mi">3600</span><span class="p">,</span>
<span class="nt">&quot;tags&quot;</span><span class="p">:</span> <span class="s2">&quot;foo&quot;</span>
<span class="nt">&quot;last_ping&quot;</span><span class="p">:</span> <span class="s2">&quot;2016-07-09T13:58:43.366568+00:00&quot;</span><span class="p">,</span>
<span class="nt">&quot;n_pings&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
<span class="nt">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;Api test 1&quot;</span><span class="p">,</span>
<span class="nt">&quot;next_ping&quot;</span><span class="p">:</span> <span class="s2">&quot;2016-07-09T14:58:43.366568+00:00&quot;</span><span class="p">,</span>
<span class="nt">&quot;ping_url&quot;</span><span class="p">:</span> <span class="s2">&quot;{{ PING_ENDPOINT }}25c55e7c-8092-4d21-ad06-7dacfbb6fc10&quot;</span><span class="p">,</span>
<span class="nt">&quot;tags&quot;</span><span class="p">:</span> <span class="s2">&quot;foo&quot;</span><span class="p">,</span>
<span class="nt">&quot;timeout&quot;</span><span class="p">:</span> <span class="mi">3600</span>
<span class="p">},</span>
<span class="p">{</span>
<span class="nt">&quot;ping_url&quot;</span><span class="p">:</span> <span class="s2">&quot;{{ PING_ENDPOINT }}20324f81-5966-4e75-9734-8440df52ed75&quot;</span><span class="p">,</span>
<span class="nt">&quot;grace&quot;</span><span class="p">:</span> <span class="mi">60</span><span class="p">,</span>
<span class="nt">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;API test 2&quot;</span><span class="p">,</span>
<span class="nt">&quot;timeout&quot;</span><span class="p">:</span> <span class="mi">60</span><span class="p">,</span>
<span class="nt">&quot;tags&quot;</span><span class="p">:</span> <span class="s2">&quot;bar,baz&quot;</span>
<span class="nt">&quot;last_ping&quot;</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
<span class="nt">&quot;n_pings&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
<span class="nt">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;Api test 2&quot;</span><span class="p">,</span>
<span class="nt">&quot;next_ping&quot;</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
<span class="nt">&quot;ping_url&quot;</span><span class="p">:</span> <span class="s2">&quot;{{ PING_ENDPOINT }}7e1b6e61-b16f-4671-bae3-e3233edd1b5e&quot;</span><span class="p">,</span>
<span class="nt">&quot;tags&quot;</span><span class="p">:</span> <span class="s2">&quot;bar baz&quot;</span><span class="p">,</span>
<span class="nt">&quot;timeout&quot;</span><span class="p">:</span> <span class="mi">60</span>
<span class="p">}</span>
<span class="p">]</span>
<span class="p">}</span>


+ 14
- 8
templates/front/snippets/list_checks_response.txt View File

@ -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
}
]
}

Loading…
Cancel
Save