forked from GithubBackups/healthchecks
Merge pull request #134 from umitakkaya/master
Tags querystring param to filter checks by tags
This commit is contained in:
commit
94556f2893
@ -20,6 +20,7 @@ class ListChecksTestCase(BaseTestCase):
|
||||
self.a1.last_ping = self.now
|
||||
self.a1.n_pings = 1
|
||||
self.a1.status = "new"
|
||||
self.a1.tags = "a1-tag a1-additional-tag"
|
||||
self.a1.save()
|
||||
|
||||
self.a2 = Check(user=self.alice, name="Alice 2")
|
||||
@ -27,6 +28,7 @@ class ListChecksTestCase(BaseTestCase):
|
||||
self.a2.grace = td(seconds=3600)
|
||||
self.a2.last_ping = self.now
|
||||
self.a2.status = "up"
|
||||
self.a2.tags = "a2-tag"
|
||||
self.a2.save()
|
||||
|
||||
def get(self):
|
||||
@ -79,3 +81,45 @@ class ListChecksTestCase(BaseTestCase):
|
||||
|
||||
self.assertEqual(r.status_code, 200)
|
||||
self.assertContains(r, "Alice")
|
||||
|
||||
def test_it_works_with_tags_param(self):
|
||||
r = self.client.get("/api/v1/checks/?tag=a2-tag", HTTP_X_API_KEY="abc")
|
||||
self.assertEqual(r.status_code, 200)
|
||||
|
||||
doc = r.json()
|
||||
self.assertTrue("checks" in doc)
|
||||
self.assertEqual(len(doc["checks"]), 1)
|
||||
|
||||
check = doc["checks"][0]
|
||||
|
||||
self.assertEqual(check["name"], "Alice 2")
|
||||
self.assertEqual(check["tags"], "a2-tag")
|
||||
|
||||
def test_it_filters_with_multiple_tags_param(self):
|
||||
r = self.client.get("/api/v1/checks/?tag=a1-tag&tag=a1-additional-tag", HTTP_X_API_KEY="abc")
|
||||
self.assertEqual(r.status_code, 200)
|
||||
|
||||
doc = r.json()
|
||||
self.assertTrue("checks" in doc)
|
||||
self.assertEqual(len(doc["checks"]), 1)
|
||||
|
||||
check = doc["checks"][0]
|
||||
|
||||
self.assertEqual(check["name"], "Alice 1")
|
||||
self.assertEqual(check["tags"], "a1-tag a1-additional-tag")
|
||||
|
||||
def test_it_does_not_match_tag_partially(self):
|
||||
r = self.client.get("/api/v1/checks/?tag=tag", HTTP_X_API_KEY="abc")
|
||||
self.assertEqual(r.status_code, 200)
|
||||
|
||||
doc = r.json()
|
||||
self.assertTrue("checks" in doc)
|
||||
self.assertEqual(len(doc["checks"]), 0)
|
||||
|
||||
def test_non_existing_tags_filter_returns_empty_result(self):
|
||||
r = self.client.get("/api/v1/checks/?tag=non_existing_tag_with_no_checks", HTTP_X_API_KEY="abc")
|
||||
self.assertEqual(r.status_code, 200)
|
||||
|
||||
doc = r.json()
|
||||
self.assertTrue("checks" in doc)
|
||||
self.assertEqual(len(doc["checks"]), 0)
|
||||
|
@ -104,8 +104,23 @@ def _update(check, spec):
|
||||
@validate_json(schemas.check)
|
||||
def checks(request):
|
||||
if request.method == "GET":
|
||||
tags = request.GET.getlist('tag', [])
|
||||
q = Check.objects.filter(user=request.user)
|
||||
doc = {"checks": [check.to_dict() for check in q]}
|
||||
|
||||
doc = {"checks": []}
|
||||
|
||||
if len(tags) > 0:
|
||||
for tag in tags:
|
||||
q = q.filter(tags__contains=tag.strip())
|
||||
|
||||
tags_set = set(tags)
|
||||
|
||||
for check in q:
|
||||
if tags_set.issubset(set(check.tags_list())):
|
||||
doc["checks"].append(check.to_dict())
|
||||
else:
|
||||
doc["checks"] = [check.to_dict() for check in q]
|
||||
|
||||
return JsonResponse(doc)
|
||||
|
||||
elif request.method == "POST":
|
||||
|
@ -61,8 +61,8 @@ The response may contain a JSON document with additional data.
|
||||
<div class="api-path">GET {{ SITE_ROOT }}/api/v1/checks/</div>
|
||||
|
||||
<p>
|
||||
Returns a list of checks. This API call takes no parameters and returns
|
||||
a JSON document with all checks in user's account.
|
||||
Returns a list of checks. This API call takes only optional tag querystring parameter to filter checks by their tags.
|
||||
If no parameter provided it returns a JSON document with all checks in user's account.
|
||||
</p>
|
||||
<h3 class="api-section">Example Request</h3>
|
||||
{% include "front/snippets/list_checks_request.html" %}
|
||||
@ -70,6 +70,12 @@ The response may contain a JSON document with additional data.
|
||||
<h3 class="api-section">Example Response</h3>
|
||||
{% include "front/snippets/list_checks_response.html" %}
|
||||
|
||||
<h3 class="api-section">Example Request To Filter Checks By Their Tags</h3>
|
||||
{% include "front/snippets/list_checks_request_filtered.html" %}
|
||||
|
||||
<h3 class="api-section">Example Response Of Filtered Checks</h3>
|
||||
{% include "front/snippets/list_checks_response_filtered.html" %}
|
||||
|
||||
<!-- ********************************************************************** /-->
|
||||
|
||||
<a class="section" name="create-check">
|
||||
|
@ -0,0 +1,2 @@
|
||||
<div class="highlight"><pre><span></span>curl --header <span class="s2">"X-Api-Key: your-api-key"</span> {{ SITE_ROOT }}/api/v1/checks/?tag=bar&tag=baz
|
||||
</pre></div>
|
@ -0,0 +1 @@
|
||||
curl --header "X-Api-Key: your-api-key" SITE_ROOT/api/v1/checks/?tag=bar&tag=baz
|
19
templates/front/snippets/list_checks_response_filtered.html
Normal file
19
templates/front/snippets/list_checks_response_filtered.html
Normal file
@ -0,0 +1,19 @@
|
||||
<div class="highlight"><pre><span></span><span class="p">{</span>
|
||||
<span class="nt">"checks"</span><span class="p">:</span> <span class="p">[</span>
|
||||
<span class="p">{</span>
|
||||
<span class="nt">"last_ping"</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
|
||||
<span class="nt">"ping_url"</span><span class="p">:</span> <span class="s2">"{{ PING_ENDPOINT }}9d17c61f-5c4f-4cab-b517-11e6b2679ced"</span><span class="p">,</span>
|
||||
<span class="nt">"next_ping"</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
|
||||
<span class="nt">"grace"</span><span class="p">:</span> <span class="mi">3600</span><span class="p">,</span>
|
||||
<span class="nt">"name"</span><span class="p">:</span> <span class="s2">"Api test 2"</span><span class="p">,</span>
|
||||
<span class="nt">"n_pings"</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
|
||||
<span class="nt">"tags"</span><span class="p">:</span> <span class="s2">"bar baz"</span><span class="p">,</span>
|
||||
<span class="nt">"pause_url"</span><span class="p">:</span> <span class="s2">"{{ SITE_ROOT }}/api/v1/checks/9d17c61f-5c4f-4cab-b517-11e6b2679ced/pause"</span><span class="p">,</span>
|
||||
<span class="nt">"tz"</span><span class="p">:</span> <span class="s2">"UTC"</span><span class="p">,</span>
|
||||
<span class="nt">"schedule"</span><span class="p">:</span> <span class="s2">"0/10 * * * *"</span><span class="p">,</span>
|
||||
<span class="nt">"status"</span><span class="p">:</span> <span class="s2">"new"</span><span class="p">,</span>
|
||||
<span class="nt">"update_url"</span><span class="p">:</span> <span class="s2">"{{ SITE_ROOT }}/api/v1/checks/9d17c61f-5c4f-4cab-b517-11e6b2679ced"</span>
|
||||
<span class="p">}</span>
|
||||
<span class="p">]</span>
|
||||
<span class="p">}</span>
|
||||
</pre></div>
|
18
templates/front/snippets/list_checks_response_filtered.txt
Normal file
18
templates/front/snippets/list_checks_response_filtered.txt
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"checks": [
|
||||
{
|
||||
"last_ping": null,
|
||||
"ping_url": "PING_ENDPOINT9d17c61f-5c4f-4cab-b517-11e6b2679ced",
|
||||
"next_ping": null,
|
||||
"grace": 3600,
|
||||
"name": "Api test 2",
|
||||
"n_pings": 0,
|
||||
"tags": "bar baz",
|
||||
"pause_url": "SITE_ROOT/api/v1/checks/9d17c61f-5c4f-4cab-b517-11e6b2679ced/pause",
|
||||
"tz": "UTC",
|
||||
"schedule": "0/10 * * * *",
|
||||
"status": "new",
|
||||
"update_url": "SITE_ROOT/api/v1/checks/9d17c61f-5c4f-4cab-b517-11e6b2679ced"
|
||||
}
|
||||
]
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user