Browse Source

Merge pull request #134 from umitakkaya/master

Tags querystring param to filter checks by tags
pull/140/head
Pēteris Caune 7 years ago
committed by GitHub
parent
commit
94556f2893
7 changed files with 108 additions and 3 deletions
  1. +44
    -0
      hc/api/tests/test_list_checks.py
  2. +16
    -1
      hc/api/views.py
  3. +8
    -2
      templates/front/docs_api.html
  4. +2
    -0
      templates/front/snippets/list_checks_request_filtered.html
  5. +1
    -0
      templates/front/snippets/list_checks_request_filtered.txt
  6. +19
    -0
      templates/front/snippets/list_checks_response_filtered.html
  7. +18
    -0
      templates/front/snippets/list_checks_response_filtered.txt

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

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

+ 16
- 1
hc/api/views.py View File

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


+ 8
- 2
templates/front/docs_api.html View File

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


+ 2
- 0
templates/front/snippets/list_checks_request_filtered.html View File

@ -0,0 +1,2 @@
<div class="highlight"><pre><span></span>curl --header <span class="s2">&quot;X-Api-Key: your-api-key&quot;</span> {{ SITE_ROOT }}/api/v1/checks/?tag=bar&tag=baz
</pre></div>

+ 1
- 0
templates/front/snippets/list_checks_request_filtered.txt View File

@ -0,0 +1 @@
curl --header "X-Api-Key: your-api-key" SITE_ROOT/api/v1/checks/?tag=bar&tag=baz

+ 19
- 0
templates/front/snippets/list_checks_response_filtered.html View File

@ -0,0 +1,19 @@
<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;last_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 }}9d17c61f-5c4f-4cab-b517-11e6b2679ced&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;grace&quot;</span><span class="p">:</span> <span class="mi">3600</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;n_pings&quot;</span><span class="p">:</span> <span class="mi">0</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;pause_url&quot;</span><span class="p">:</span> <span class="s2">&quot;{{ SITE_ROOT }}/api/v1/checks/9d17c61f-5c4f-4cab-b517-11e6b2679ced/pause&quot;</span><span class="p">,</span>
<span class="nt">&quot;tz&quot;</span><span class="p">:</span> <span class="s2">&quot;UTC&quot;</span><span class="p">,</span>
<span class="nt">&quot;schedule&quot;</span><span class="p">:</span> <span class="s2">&quot;0/10 * * * *&quot;</span><span class="p">,</span>
<span class="nt">&quot;status&quot;</span><span class="p">:</span> <span class="s2">&quot;new&quot;</span><span class="p">,</span>
<span class="nt">&quot;update_url&quot;</span><span class="p">:</span> <span class="s2">&quot;{{ SITE_ROOT }}/api/v1/checks/9d17c61f-5c4f-4cab-b517-11e6b2679ced&quot;</span>
<span class="p">}</span>
<span class="p">]</span>
<span class="p">}</span>
</pre></div>

+ 18
- 0
templates/front/snippets/list_checks_response_filtered.txt View 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…
Cancel
Save