Skip to content

Commit dcba7c7

Browse files
peekjef72amotl
authored andcommitted
add health and datasource proxy
1 parent 36d0a50 commit dcba7c7

File tree

6 files changed

+82
-0
lines changed

6 files changed

+82
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,5 @@ venv.bak/
112112
# mypy
113113
.mypy_cache/
114114

115+
# visual code launch...
116+
.vscode/launch.json

grafana_client/api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Dashboard,
66
Datasource,
77
Folder,
8+
Health,
89
Notifications,
910
Organization,
1011
Organizations,
@@ -40,6 +41,7 @@ def __init__(
4041
self.dashboard = Dashboard(self.client)
4142
self.datasource = Datasource(self.client)
4243
self.folder = Folder(self.client)
44+
self.health = Health(self.client)
4345
self.organization = Organization(self.client)
4446
self.organizations = Organizations(self.client)
4547
self.search = Search(self.client)

grafana_client/elements/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .base import Base
44
from .dashboard import Dashboard
55
from .datasource import Datasource
6+
from .health import Health
67
from .folder import Folder
78
from .notifications import Notifications
89
from .organization import Organization, Organizations

grafana_client/elements/datasource.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,31 @@ def delete_datasource_by_name(self, datasource_name):
9595
delete_datasource = "/datasources/name/%s" % datasource_name
9696
r = self.client.DELETE(delete_datasource)
9797
return r
98+
99+
def get_datasource_proxy_data(self, datasource_id
100+
, query_type='query'
101+
, version='v1'
102+
, expr=None
103+
, time=None
104+
, start=None
105+
, end=None
106+
, step=None
107+
):
108+
"""
109+
110+
:param datasource_id:
111+
:param version: api_version currently v1
112+
:param query_type: query_range |query
113+
:param expr: expr to query
114+
115+
:return: r (dict)
116+
"""
117+
get_datasource_path = "/datasources/proxy/{}" \
118+
'/api/{}/{}?query={}'.format( datasource_id, version, query_type, expr)
119+
if query_type == 'query_range':
120+
get_datasource_path = get_datasource_path + '&start={}&end={}&step={}'.format(
121+
start, end, step)
122+
else:
123+
get_datasource_path = get_datasource_path + '&time={}'.format(time)
124+
r = self.api.GET(get_datasource_path)
125+
return r

grafana_client/elements/health.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from .base import Base
2+
3+
4+
class Health(Base):
5+
def __init__(self, client):
6+
super(Health, self).__init__(client)
7+
self.client = client
8+
9+
def check(self):
10+
"""
11+
12+
:return:
13+
"""
14+
path = "/health"
15+
r = self.client.GET(path)
16+
return r

test/elements/test_health.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import unittest
2+
3+
import requests_mock
4+
5+
from grafana_client import GrafanaApi
6+
from grafana_client.client import (
7+
GrafanaBadInputError,
8+
GrafanaClientError,
9+
GrafanaServerError,
10+
GrafanaUnauthorizedError,
11+
)
12+
13+
14+
class HealthTestCase(unittest.TestCase):
15+
def setUp(self):
16+
self.grafana = GrafanaApi(("admin", "admin"), host="localhost", url_path_prefix="", protocol="http")
17+
18+
@requests_mock.Mocker()
19+
def test_search_dashboards(self, m):
20+
m.get(
21+
"http://localhost/api/health",
22+
json=[
23+
{
24+
"commit": "6f8c1d9fe4",
25+
"database": "ok",
26+
"version": "7.5.11"
27+
}
28+
],
29+
)
30+
31+
result = self.grafana.health.check()
32+
self.assertEqual(result[0]["database"], 'ok')
33+
self.assertEqual(len(result), 1)

0 commit comments

Comments
 (0)