diff --git a/hc/api/models.py b/hc/api/models.py index a2d94912..75505c1f 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -153,6 +153,9 @@ class Check(models.Model): def tags_list(self): return [t.strip() for t in self.tags.split(" ") if t.strip()] + def matches_tag_set(self, tag_set): + return tag_set.issubset(self.tags_list()) + def to_dict(self): update_rel_url = reverse("hc-api-update", args=[self.code]) pause_rel_url = reverse("hc-api-pause", args=[self.code]) diff --git a/hc/api/views.py b/hc/api/views.py index 1f7aa27c..f577361c 100644 --- a/hc/api/views.py +++ b/hc/api/views.py @@ -104,24 +104,20 @@ 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": []} + tags = set(request.GET.getlist("tag")) + for tag in tags: + # approximate filtering by tags + q = q.filter(tags__contains=tag) - if len(tags) > 0: - for tag in tags: - q = q.filter(tags__contains=tag.strip()) + checks = [] + for check in q: + # precise, final filtering + if not tags or check.matches_tag_set(tags): + checks.append(check.to_dict()) - 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) + return JsonResponse({"checks": checks}) elif request.method == "POST": created = False diff --git a/templates/front/docs_api.html b/templates/front/docs_api.html index af5275e6..be0c5717 100644 --- a/templates/front/docs_api.html +++ b/templates/front/docs_api.html @@ -60,22 +60,33 @@ The response may contain a JSON document with additional data.
GET {{ SITE_ROOT }}/api/v1/checks/
-

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

+

Returns a list of checks belonging to the user, optionally filtered by +one or more tags.

+ +

Query String Parameters

+ + + + + +
tag=<value> +

+ Filters the checks, and returns only the checks that + are tagged with the specified value. +

+

+ This parameter can be repeated multiple times. +

+

Example:

+
{{ SITE_ROOT }}/api/v1/checks/?tag=foo&tag=bar
+
+

Example Request

{% include "front/snippets/list_checks_request.html" %}

Example Response

{% include "front/snippets/list_checks_response.html" %} -

Example Request To Filter Checks By Their Tags

-{% include "front/snippets/list_checks_request_filtered.html" %} - -

Example Response Of Filtered Checks

-{% include "front/snippets/list_checks_response_filtered.html" %} - diff --git a/templates/front/snippets/list_checks_request_filtered.html b/templates/front/snippets/list_checks_request_filtered.html deleted file mode 100644 index a604acf6..00000000 --- a/templates/front/snippets/list_checks_request_filtered.html +++ /dev/null @@ -1,2 +0,0 @@ -
curl --header "X-Api-Key: your-api-key" {{ SITE_ROOT }}/api/v1/checks/?tag=bar&tag=baz
-
diff --git a/templates/front/snippets/list_checks_request_filtered.txt b/templates/front/snippets/list_checks_request_filtered.txt deleted file mode 100644 index b393688d..00000000 --- a/templates/front/snippets/list_checks_request_filtered.txt +++ /dev/null @@ -1 +0,0 @@ -curl --header "X-Api-Key: your-api-key" SITE_ROOT/api/v1/checks/?tag=bar&tag=baz \ No newline at end of file diff --git a/templates/front/snippets/list_checks_response_filtered.html b/templates/front/snippets/list_checks_response_filtered.html deleted file mode 100644 index 530c1c7e..00000000 --- a/templates/front/snippets/list_checks_response_filtered.html +++ /dev/null @@ -1,19 +0,0 @@ -
{
-  "checks": [
-    {
-      "last_ping": null,
-      "ping_url": "{{ PING_ENDPOINT }}9d17c61f-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"
-    }
-  ]
-}
-
diff --git a/templates/front/snippets/list_checks_response_filtered.txt b/templates/front/snippets/list_checks_response_filtered.txt deleted file mode 100644 index 870cfc1f..00000000 --- a/templates/front/snippets/list_checks_response_filtered.txt +++ /dev/null @@ -1,18 +0,0 @@ -{ - "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" - } - ] -} \ No newline at end of file