Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve smartquery and query_factory beyond data source health checks #112

Merged
merged 21 commits into from
Sep 15, 2023

Conversation

amotl
Copy link
Contributor

@amotl amotl commented Sep 15, 2023

Hi there,

this patch by @peekjef72 (thanks!) will improve the datasource query interface, which is an experimental subsystem of grafana-client, aiming to provide a unified interface for querying data sources "behind" Grafana.

Dear @peekjef72, I am handing in your patch from GH-38 separately, to have something to compare against in the case something went wrong with the rebasing, and for your and future references.

I will continue here to add corresponding adjustments to the test suite, in order to integrate this patch.

With kind regards,
Andreas.

@amotl amotl changed the title Datasource smartquery improvements Improve smartquery and query_factory beyond data source health checks Sep 15, 2023
@codecov
Copy link

codecov bot commented Sep 15, 2023

Codecov Report

Merging #112 (5782066) into main (a275d9f) will decrease coverage by 0.73%.
The diff coverage is 86.90%.

@@            Coverage Diff             @@
##             main     #112      +/-   ##
==========================================
- Coverage   95.88%   95.15%   -0.73%     
==========================================
  Files          25       25              
  Lines        1531     1590      +59     
==========================================
+ Hits         1468     1513      +45     
- Misses         63       77      +14     
Files Changed Coverage Δ
grafana_client/elements/datasource.py 97.83% <86.11%> (-2.17%) ⬇️
grafana_client/knowledge.py 95.77% <87.50%> (-4.23%) ⬇️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@amotl amotl force-pushed the datasource-smartquery-improvements branch from ce855a9 to 878ca42 Compare September 15, 2023 17:39
@amotl amotl force-pushed the datasource-smartquery-improvements branch from 878ca42 to 5782066 Compare September 15, 2023 17:40
@amotl amotl marked this pull request as ready for review September 15, 2023 17:40
Comment on lines 492 to 503
elif datasource_type == "loki":
if "status" in response and response["status"] == "success":
if self.api.version and VERSION_7 <= LooseVersion(self.api.version) < VERSION_8:
if "status" in response and response["status"] == "success":
message = "Success"
success = True
else:
message = response.get("message", "Unknown error")
elif "results" in response and "test" in response["results"]:
message = "Success"
success = True
else:
elif "message" in response:
message = response["message"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've adjusted this a bit, compared to your version, @peekjef72. When anything is wrong, please send fixes. 🌻

@@ -403,6 +440,7 @@ def health_check(self, datasource: Union[DatasourceIdentifier, Dict]) -> Datasou
expression = get_healthcheck_expression(datasource_type, datasource_dialect)

start = time.time()
message = "Unknown error"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I discovered cases where this was needed. Apparently, not all code paths have been covering this before.

Comment on lines +378 to +397
elif datasource_type in ("prometheus", "loki") and LooseVersion(self.api.version) <= VERSION_7:
if (
"queries" in request["data"]
and len(request["data"]["queries"]) > 0
and "instant" in request["data"]["queries"][0]
and request["data"]["queries"][0]["instant"]
):
return self.query(
datasource.get("id"),
request["expr"],
request["data"]["to"],
)
else:
return self.query_range(
datasource.get("id"),
request["expr"],
request["data"]["from"],
request["data"]["to"],
request["data"]["step"],
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have tests for those yet. Do you think you can add some?

Comment on lines +154 to +159
if "time_from" in model:
query["from"] = model["time_from"]
if "time_to" in model:
query["until"] = model["time_to"]

request["data"] = query
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This spot also lacks testing.

@amotl amotl merged commit d99549d into main Sep 15, 2023
7 checks passed
@amotl amotl deleted the datasource-smartquery-improvements branch September 15, 2023 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants