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

Update SqlTarget to support parsing queries from files #555

Merged
merged 3 commits into from
May 18, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Changelog
* Added Minimum option for Timeseries
* Added Maximum option for Timeseries
* Added Number of decimals displays option for Timeseries* Added Bar_Chart_ panel support
* Extended SqlTarget to support parsing queries from files

.. _Bar_Chart: basehttps://grafana.com/docs/grafana/latest/panels-visualizations/visualizations/bar-chart/

Expand Down
14 changes: 14 additions & 0 deletions grafanalib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,20 @@ class SqlTarget(Target):

rawSql = attr.ib(default="")
rawQuery = attr.ib(default=True)
srcFilePath = attr.ib(default="", validator=instance_of(str))
sqlParams = attr.ib(default={}, validator=instance_of(dict))

def __attrs_post_init__(self):
"""Override rawSql if a path to a source file is provided,
if it is a parameterized query, fill in the parameters.
srcFilePath: this will containt the path to the source file
sqlParams: this will contain the sql parameters to use in the read query
"""
if self.srcFilePath:
with open(self.srcFilePath, "r") as f:
self.rawSql = f.read()
if self.sqlParams is not None:
self.rawSql = self.rawSql.format(**self.sqlParams)

def to_json_data(self):
"""Override the Target to_json_data to add additional fields.
Expand Down
3 changes: 3 additions & 0 deletions grafanalib/tests/examples/sqltarget_example_files/example.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT example, count(id)
FROM test
GROUP BY example;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT example
FROM test
WHERE example='{example}' AND example_date BETWEEN '{starting_date}' AND '{ending_date}';
28 changes: 28 additions & 0 deletions grafanalib/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1135,3 +1135,31 @@ def test_sql_target():
)
assert t.to_json_data()["targets"][0].rawQuery is True
assert t.to_json_data()["targets"][0].rawSql == "SELECT * FROM example"


def test_sql_target_with_source_files():
t = G.Table(
dataSource="some data source",
targets=[
G.SqlTarget(srcFilePath="grafanalib/tests/examples/sqltarget_example_files/example.sql"),
],
title="table title",
)
assert t.to_json_data()["targets"][0].rawQuery is True
assert t.to_json_data()["targets"][0].rawSql == "SELECT example, count(id)\nFROM test\nGROUP BY example;\n"
print(t.to_json_data()["targets"][0])

t = G.Table(
dataSource="some data source",
targets=[
G.SqlTarget(srcFilePath="grafanalib/tests/examples/sqltarget_example_files/example_with_params.sql", sqlParams={
"example": "example",
"starting_date": "1970-01-01",
"ending_date": "1971-01-01",
},),
],
title="table title",
)
assert t.to_json_data()["targets"][0].rawQuery is True
assert t.to_json_data()["targets"][0].rawSql == "SELECT example\nFROM test\nWHERE example='example' AND example_date BETWEEN '1970-01-01' AND '1971-01-01';\n"
print(t.to_json_data()["targets"][0])