Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Annotation API updated as per https://grafana.com/docs/grafana/latest… #60

Merged
merged 3 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 35 additions & 36 deletions grafana_api/api/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@ def get_annotation(
alert_id=None,
dashboard_id=None,
panel_id=None,
user_id=None,
ann_type=None,
tags=None,
limit=None,
):

"""
https://grafana.com/docs/grafana/latest/http_api/annotations/#find-annotations

:param time_from:
:param time_to:
:param alert_id:
:param dashboard_id:
:param panel_id:
:param user_id:
:param ann_type: Annotation type. On of alert|annotation
:param tags:
:param limit:
:return:
Expand All @@ -45,6 +51,12 @@ def get_annotation(
if panel_id:
params.append("panelId=%s" % panel_id)

if user_id:
params.append("userId=%s", user_id)

if ann_type:
params.append("type=%s", ann_type)

if tags:
for tag in tags:
params.append("tags=%s" % tag)
Expand All @@ -61,29 +73,34 @@ def get_annotation(

def add_annotation(
self,
dashboard_id=None,
panel_id=None,
time_from=None,
time_to=None,
is_region=True,
tags=[],
text=None,
):

"""
https://grafana.com/docs/grafana/latest/http_api/annotations/#create-annotation

:param dashboard_id:
:param panel_id
:param time_from:
:param time_to:
:param is_region:
:param tags:
:param text:
:return:
"""

annotations_path = "/annotations"
payload = {
"dashboardId": dashboard_id,
"panelId": panel_id,
"time": time_from,
"timeEnd": time_to,
"isRegion": bool(is_region),
"tags": tags,
"text": text

"text": text,
}

r = self.api.POST(annotations_path, json=payload)
Expand All @@ -98,6 +115,8 @@ def add_annotation_graphite(
data=None,
):
"""
https://grafana.com/docs/grafana/latest/http_api/annotations/#create-annotation-in-graphite-format

:param what:
:param tags:
:param when:
Expand All @@ -111,7 +130,6 @@ def add_annotation_graphite(
"tags": tags,
"when": when,
"data": data

}

r = self.api.POST(annotations_path, json=payload)
Expand All @@ -123,15 +141,14 @@ def update_annotation(
annotations_id,
time_from=None,
time_to=None,
is_region=True,
tags=[],
text=None,
):
"""
https://grafana.com/docs/grafana/latest/http_api/annotations/#update-annotation

:param time_from:
:param time_to:
:param is_region:
:param tags:
:param text:
:return:
Expand All @@ -140,10 +157,8 @@ def update_annotation(
payload = {
"time": time_from,
"timeEnd": time_to,
"isRegion": bool(is_region),
"tags": tags,
"text": text

}

r = self.api.PUT(annotations_path, json=payload)
Expand All @@ -155,48 +170,30 @@ def partial_update_annotation(
annotations_id,
time_from=None,
time_to=None,
is_region=None,
tags=[],
text=None,
):
"""
https://grafana.com/docs/grafana/latest/http_api/annotations/#patch-annotation

:param annotations_id:
:param time_from:
:param time_to:
:param is_region:
:param tags:
:param text:
:return:
"""
annotations_path = "/annotations/{}".format(annotations_id)
payload = {}
if time_from:
payload['time'] = time_from
if time_to:
payload['timeEnd'] = time_to
if is_region:
payload['isRegion'] = bool(is_region)
if tags:
payload['tags'] = tags
if text:
payload['text'] = text

r = self.api.PATCH(annotations_path, json=payload)

return r

def delete_annotations_by_region_id(
self,
region_id=None
):
payload = {
"time": time_from,
"timeEnd": time_to,
"tags": tags,
"text": text
}

"""
:param region_id:
:return:
"""
annotations_path = "/annotations/region/{}".format(region_id)
r = self.api.DELETE(annotations_path)
r = self.api.PATCH(annotations_path, json=payload)

return r

Expand All @@ -206,6 +203,8 @@ def delete_annotations_by_id(
):

"""
https://grafana.com/docs/grafana/latest/http_api/annotations/#delete-annotation-by-id

:param annotations_id:
:return:
"""
Expand Down
16 changes: 4 additions & 12 deletions test/api/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def test_annotations(self, m):
"updated": 1563280160455,
"time": 1563156456006,
"text": "Annotation Description",
"regionId": 79,
"tags": [
"tags-test"
],
Expand Down Expand Up @@ -72,7 +71,6 @@ def test_annotations_with_out_param(self, m):
"updated": 1563280160455,
"time": 1563156456006,
"text": "Annotation Description",
"regionId": 79,
"tags": [
"tags-test"
],
Expand All @@ -86,12 +84,6 @@ def test_annotations_with_out_param(self, m):
annotations = self.cli.annotations.get_annotation()
self.assertEqual(len(annotations), 1)

@requests_mock.Mocker()
def test_delete_annotations_by_region_id(self, m):
m.delete("http://localhost/api/annotations/region/99", json={"message": "Annotation region deleted"})
response = self.cli.annotations.delete_annotations_by_region_id(99)
self.assertEqual(response['message'], "Annotation region deleted")

@requests_mock.Mocker()
def test_delete_annotations_by_id(self, m):
m.delete('http://localhost/api/annotations/99', json={"message": "Annotation deleted"})
Expand Down Expand Up @@ -132,8 +124,8 @@ def test_add_annotation(self, m):
"http://localhost/api/annotations",
json={"endId": 80, "id": 79, "message": "Annotation added"},
)
annotation = self.cli.annotations.add_annotation(time_from=1563183710618, time_to=1563185212275
, is_region=True, tags=["tags-test"], text="Test")
annotation = self.cli.annotations.add_annotation(time_from=1563183710618, time_to=1563185212275,
tags=["tags-test"], text="Test")
self.assertEqual(annotation["endId"], 80)
self.assertEqual(annotation["id"], 79)
self.assertEqual(annotation["message"], "Annotation added")
Expand All @@ -144,8 +136,8 @@ def test_update_annotation(self, m):
"http://localhost/api/annotations/79",
json={"endId": 80, "id": 79, "message": "Annotation updated"},
)
annotation = self.cli.annotations.update_annotation(annotations_id=79, time_from=1563183710618, time_to=1563185212275
, is_region=True, tags=["tags-test"], text="Test")
annotation = self.cli.annotations.update_annotation(annotations_id=79, time_from=1563183710618, time_to=1563185212275,
tags=["tags-test"], text="Test")
self.assertEqual(annotation["endId"], 80)
self.assertEqual(annotation["id"], 79)
self.assertEqual(annotation["message"], "Annotation updated")
Expand Down