Skip to content

Commit

Permalink
Update SqlTarget to support parsing queries from files (#555)
Browse files Browse the repository at this point in the history
* Update SqlTarget to support parsing queries from files

* Update test files to be more general

---------

Co-authored-by: JamesGibo <[email protected]>
  • Loading branch information
kamka427 and JamesGibo authored May 18, 2023
1 parent 79450cf commit 2a30b44
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
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])

0 comments on commit 2a30b44

Please sign in to comment.