Skip to content

Commit

Permalink
Data source health check: Support data source fetzerch-sunandmoon-...
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Jun 25, 2022
1 parent aab93b9 commit 077492a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
29 changes: 21 additions & 8 deletions grafana_client/elements/datasource.py
Expand Up @@ -287,14 +287,27 @@ def health_check(self, datasource: Union[DatasourceIdentifier, Dict]):
# This case is very special. It is used for Elasticsearch and Testdata.
if datasource_type == "elasticsearch":
database_name = datasource["database"]
assert response[database_name]["mappings"]["properties"], "Invalid response from Elasticsearch"
message = "Success"
success = True
elif datasource_type == "testdata":
assert response["id"], "Invalid response from Testdata. No attribute 'id' in response."
assert response["uid"], "Invalid response from Testdata. No attribute 'uid' in response."
message = "Success"
success = True
try:
assert response[database_name]["mappings"]["properties"], "Invalid response from Elasticsearch"
message = "Success"
success = True
except (AssertionError, KeyError) as ex:
message = f"{ex.__class__.__name__}: {ex}"
elif datasource_type in ["fetzerch-sunandmoon-datasource", "testdata"]:
try:
assert response["id"], "Invalid response. No attribute 'id' in response."
assert response["uid"], "Invalid response. No attribute 'uid' in response."
if datasource_type == "fetzerch-sunandmoon-datasource":
assert response["jsonData"][
"latitude"
], "Invalid response. No attribute 'jsonData.latitude' in response."
assert response["jsonData"][
"longitude"
], "Invalid response. No attribute 'jsonData.longitude' in response."
message = "Success"
success = True
except (AssertionError, KeyError) as ex:
message = f"{ex.__class__.__name__}: {ex}"

# Generic case, where the response has a top-level "results" key.
else:
Expand Down
3 changes: 3 additions & 0 deletions grafana_client/knowledge.py
Expand Up @@ -92,6 +92,8 @@ def query_factory(datasource, expression: str, store: Optional[str] = None) -> U
raise NotImplementedError("__NEVER__")
elif datasource_type == "elasticsearch":
query = expression
elif datasource_type == "fetzerch-sunandmoon-datasource":
query = expression
elif datasource_type == "influxdb":
dialect = datasource["jsonData"]["version"]
query = {
Expand Down Expand Up @@ -163,6 +165,7 @@ def query_factory(datasource, expression: str, store: Optional[str] = None) -> U
# TODO: Complete the list for all popular databases.
HEALTHCHECK_EXPRESSION_MAP = {
"elasticsearch": "url:///datasources/proxy/{datasource_id}/{database_name}/_mapping",
"fetzerch-sunandmoon-datasource": "url:///datasources/uid/{datasource_uid}",
"influxdb": "SHOW RETENTION POLICIES on _internal",
"influxdb+influxql": "SHOW RETENTION POLICIES on _internal",
"influxdb+flux": "buckets()",
Expand Down

0 comments on commit 077492a

Please sign in to comment.