Skip to content

Commit 849c839

Browse files
peekjef72amotl
authored andcommitted
dsquery: New args; new examples; new db
1 parent 401410b commit 849c839

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

examples/datasource-smartquery.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,42 @@
44
55
Example program for submitting a query to a data source.
66
7-
While the ``--query`` option is obligatory, the ``--store`` option is not.
7+
While the ``--query`` option is obligatory, the ``--attr`` option is not.
88
It will be used to designate the ``database`` name, with e.g. InfluxDB,
9-
or the ``metric`` name, with e.g. Prometheus.
9+
or the ``queryType`` name, with e.g. Loki.
1010
1111
Synopsis
1212
========
1313
::
14-
1514
# Query the InfluxDB 1.x data source on `play.grafana.org`.
1615
python examples/datasource-smartquery.py --uid=000000002 --query="SHOW RETENTION POLICIES on _internal"
17-
python examples/datasource-smartquery.py --uid=000000002 --store=site --query="SHOW FIELD KEYS"
16+
python examples/datasource-smartquery.py --uid=000000002 --attr "database:site" --query="SHOW FIELD KEYS"
17+
python examples/datasource-smartquery.py --uid 000000002 --query "SELECT sum(\"value\") FROM \"logins.count\" WHERE time >= now() - 5m and time <= now() GROUP BY time(10s) fill(null)"
18+
19+
#Query the InfluxDB+flux on https://play.grafana.org
20+
# fix me : query invalid !
21+
python examples/datasource-smartquery.py \
22+
--uid M3k6ZPrnz \
23+
--query "from(bucket: \"example-bucket\")\\n|> range(start: -1h)|> filter(fn: (r) => r._measurement == \"example-measurement\" and r._field == \"example-field\")"
24+
25+
// Query the Grafite
26+
python examples/datasource-smartquery.py --uid 000000001 --query "aliasByNode(apps.fakesite.web_server_02.counters.requests.count,2)"
1827
1928
# Query the Prometheus data source on `play.grafana.org`.
20-
# FIXME: Does not work yet.
21-
python \
22-
examples/datasource-smartquery.py --uid=000000008 --store=node_boot_time \
29+
python examples/datasource-smartquery.py \
30+
--uid=000000008 \
2331
--query='time() - node_boot_time_seconds{job="node", instance=~"demo.do.prometheus.io:.*"}'
2432
33+
# Query the Loki data source on `play.grafana.org`.
34+
python examples/datasource-smartquery.py \
35+
--uid=NX9d1VH7k \
36+
--query "bytes_over_time({filename=~\".+/json_access.+\", job=~\".*\", instance=~\".*\"}[5m])" \
37+
--attr "queryType:instant"
38+
python examples/datasource-smartquery.py \
39+
--uid=NX9d1VH7k \
40+
--query "sum by (status) (count_over_time({filename=~\".+/json_access.+\", job=~\".*\", instance=~\".*\"} | json | __error__=\"\" [5m]))" \
41+
--attr "legendFormat:HTTP Status {{status}}"
42+
2543
"""
2644
import json
2745
import logging
@@ -39,17 +57,28 @@
3957
def run(grafana: GrafanaApi):
4058
parser = OptionParser()
4159
parser.add_option("--uid", dest="uid", help="Data source UID")
42-
parser.add_option("--store", dest="store", help="Database or metric to be addressed")
60+
# replace by --attr "database:<store>" or --attr "metric:<metric>"
61+
# parser.add_option("--store", dest="store", help="Database or metric to be addressed")
4362
parser.add_option("--query", dest="query", help="Query expression")
63+
parser.add_option("--attr", dest="attrs", action="append", help="key:value pair attribute to send into query")
4464
(options, args) = parser.parse_args()
4565
if not options.uid:
4666
parser.error("Option --uid required")
4767
if not options.query:
4868
parser.error("Option --query required")
4969

70+
attributes = {
71+
}
72+
if options.attrs is not None and len(options.attrs) > 0 :
73+
for attr in options.attrs:
74+
(key, val) = attr.split(":")
75+
attributes[key] = val
76+
5077
# Query the data source by UID.
5178
response = grafana.datasource.smartquery(
52-
DatasourceIdentifier(uid=options.uid), expression=options.query, store=options.store
79+
DatasourceIdentifier(uid=options.uid),
80+
options.query,
81+
attrs=attributes,
5382
)
5483

5584
# Display the outcome in JSON format.

0 commit comments

Comments
 (0)