44Knowledgebase module for filling the missing gaps to access the Grafana API
55more efficiently.
66"""
7- from typing import Dict , Optional , Union
87from datetime import datetime
8+ from typing import Dict , Optional , Union
99
1010from grafana_client .model import DatasourceModel
1111
@@ -143,23 +143,16 @@ def query_factory(datasource, model: Optional[dict]) -> Union[Dict, str]:
143143 query = expression
144144 elif datasource_type == "grafana-simple-json-datasource" :
145145 query = expression
146- #************************************************************************
146+
147147 elif datasource_type == "graphite" :
148- query = {
149- "target" : expression ,
150- "from" : "-5m" ,
151- "until" : "now" ,
152- "format" : "json" ,
153- "maxDataPoints" : 300
154- }
155- if 'time_from' in model :
156- query ['from' ]= model ['time_from' ]
157- if 'time_to' in model :
158- query ['until' ]= model ['time_to' ]
148+ query = {"target" : expression , "from" : "-5m" , "until" : "now" , "format" : "json" , "maxDataPoints" : 300 }
149+ if "time_from" in model :
150+ query ["from" ] = model ["time_from" ]
151+ if "time_to" in model :
152+ query ["until" ] = model ["time_to" ]
159153
160154 request ["data" ] = query
161155
162- #************************************************************************
163156 elif datasource_type == "influxdb" :
164157 dialect = datasource ["jsonData" ].get ("version" , "InfluxQL" )
165158 query = {
@@ -177,13 +170,12 @@ def query_factory(datasource, model: Optional[dict]) -> Union[Dict, str]:
177170 }
178171 )
179172 # this drive the how timestamp are rendered in result (string by default, or in milliseconds ms)
180- request ["params" ] = { "epoch" : "ms" }
181- if ' database' in datasource :
182- request ["params" ].update ({ "db" : datasource [' database' ] })
173+ request ["params" ] = { "epoch" : "ms" }
174+ if " database" in datasource :
175+ request ["params" ].update ({"db" : datasource [" database" ] })
183176 request ["data" ] = query
184177
185178 elif dialect == "Flux" :
186-
187179 query = {
188180 "datasource" : {
189181 "type" : datasource ["type" ],
@@ -195,17 +187,25 @@ def query_factory(datasource, model: Optional[dict]) -> Union[Dict, str]:
195187 }
196188
197189 attrs = [
198- { "name" : "intervalMs" , "default" : 30000 , },
199- { "name" : "maxDataPoints" , "default" : 1441 , },
200- { "name" : "refId" , "default" : "test" , },
190+ {
191+ "name" : "intervalMs" ,
192+ "default" : 30000 ,
193+ },
194+ {
195+ "name" : "maxDataPoints" ,
196+ "default" : 1441 ,
197+ },
198+ {
199+ "name" : "refId" ,
200+ "default" : "test" ,
201+ },
201202 ]
202203 else :
203204 raise KeyError (f"InfluxDB dialect '{ dialect } ' unknown" )
204205
205206 elif datasource_type == "jaeger" :
206207 query = {}
207208
208- #************************************************************************
209209 elif datasource_type == "loki" :
210210 query = {
211211 "datasource" : {
@@ -218,20 +218,40 @@ def query_factory(datasource, model: Optional[dict]) -> Union[Dict, str]:
218218 }
219219
220220 attrs = [
221- { "name" : "intervalMs" , "default" : 60000 , },
222- { "name" : "legendFormat" , "default" : "" , },
223- { "name" : "maxLines" , "default" : 1000 , },
224- { "name" : "maxDataPoints" , "default" : 1441 , },
225- { "name" : "queryType" , "default" : "range" , },
226- { "name" : "refId" , "default" : "test" , },
227- { "name" : "resolution" , "default" : 1 , },
221+ {
222+ "name" : "intervalMs" ,
223+ "default" : 60000 ,
224+ },
225+ {
226+ "name" : "legendFormat" ,
227+ "default" : "" ,
228+ },
229+ {
230+ "name" : "maxLines" ,
231+ "default" : 1000 ,
232+ },
233+ {
234+ "name" : "maxDataPoints" ,
235+ "default" : 1441 ,
236+ },
237+ {
238+ "name" : "queryType" ,
239+ "default" : "range" ,
240+ },
241+ {
242+ "name" : "refId" ,
243+ "default" : "test" ,
244+ },
245+ {
246+ "name" : "resolution" ,
247+ "default" : 1 ,
248+ },
228249 ]
229-
250+
230251 elif datasource_type == "opentsdb" :
231252 query = {}
232253
233- #************************************************************************
234- elif datasource_type in ( "postgres" , "mssql" , "mysql" ):
254+ elif datasource_type in ("postgres" , "mssql" , "mysql" ):
235255 query = {
236256 "datasource" : {
237257 "type" : datasource ["type" ],
@@ -241,16 +261,26 @@ def query_factory(datasource, model: Optional[dict]) -> Union[Dict, str]:
241261 "rawSql" : expression ,
242262 }
243263 attrs = [
244- { "name" : "format" , "default" : "time_series" ,
245- "choices" : [ "time_series" , "table" ],
264+ {
265+ "name" : "format" ,
266+ "default" : "time_series" ,
267+ "choices" : ["time_series" , "table" ],
246268 # "version": "8.0.0"
247269 },
248- { "name" : "intervalMs" , "default" : 30000 , },
249- { "name" : "maxDataPoints" , "default" : 1441 , },
250- { "name" : "refId" , "default" : None , },
270+ {
271+ "name" : "intervalMs" ,
272+ "default" : 30000 ,
273+ },
274+ {
275+ "name" : "maxDataPoints" ,
276+ "default" : 1441 ,
277+ },
278+ {
279+ "name" : "refId" ,
280+ "default" : None ,
281+ },
251282 ]
252283
253- #************************************************************************
254284 elif datasource_type == "prometheus" :
255285 query = {
256286 "datasource" : {
@@ -263,21 +293,56 @@ def query_factory(datasource, model: Optional[dict]) -> Union[Dict, str]:
263293 }
264294
265295 attrs = [
266- { "name" : "format" , "default" : "time_series" ,
267- "choices" : [ "time_series" , "table" , "heatmap" ],
296+ {
297+ "name" : "format" ,
298+ "default" : "time_series" ,
299+ "choices" : ["time_series" , "table" , "heatmap" ],
268300 # "version": "8.0.0"
269301 },
270- { "name" : "instant" , "default" : False , },
271- { "name" : "interval" , "default" : "" , },
272- { "name" : "intervalFactor" , "default" : None , },
273- { "name" : "intervalMs" , "default" : 30000 , },
274- { "name" : "legendFormat" , "default" : "" , },
275- { "name" : "maxDataPoints" , "default" : None , },
276- { "name" : "queryType" , "default" : "timeSeriesQuery" , },
277- { "name" : "refId" , "default" : "test" , },
278- { "name" : "requestId" , "default" : "0test" , },
279- { "name" : "step" , "default" : 300 , },
280- { "name" : "utcOffsetSec" , "default" : 0 , },
302+ {
303+ "name" : "instant" ,
304+ "default" : False ,
305+ },
306+ {
307+ "name" : "interval" ,
308+ "default" : "" ,
309+ },
310+ {
311+ "name" : "intervalFactor" ,
312+ "default" : None ,
313+ },
314+ {
315+ "name" : "intervalMs" ,
316+ "default" : 30000 ,
317+ },
318+ {
319+ "name" : "legendFormat" ,
320+ "default" : "" ,
321+ },
322+ {
323+ "name" : "maxDataPoints" ,
324+ "default" : None ,
325+ },
326+ {
327+ "name" : "queryType" ,
328+ "default" : "timeSeriesQuery" ,
329+ },
330+ {
331+ "name" : "refId" ,
332+ "default" : "test" ,
333+ },
334+ {
335+ "name" : "requestId" ,
336+ "default" : "0test" ,
337+ },
338+ {
339+ "name" : "step" ,
340+ "default" : 300 ,
341+ },
342+ {
343+ "name" : "utcOffsetSec" ,
344+ "default" : 0 ,
345+ },
281346 ]
282347
283348 elif datasource_type == "simpod-json-datasource" :
@@ -304,20 +369,20 @@ def query_factory(datasource, model: Optional[dict]) -> Union[Dict, str]:
304369 if value is not None :
305370 query [attr ["name" ]] = value
306371
307- if ' time_from' not in model or ' time_to' not in model :
372+ if " time_from" not in model or " time_to" not in model :
308373 now = datetime .now ()
309- if ' time_from' not in model :
310- model [' time_from' ] = int ( now .timestamp () ) - 5 * 60
311- if ' time_to' not in model :
312- model [' time_to' ] = int ( now .timestamp () )
374+ if " time_from" not in model :
375+ model [" time_from" ] = int (now .timestamp ()) - 5 * 60
376+ if " time_to" not in model :
377+ model [" time_to" ] = int (now .timestamp ())
313378
314- if ' instant' in query and query [' instant' ]:
315- model [' time_from' ] = model [' time_to' ]
379+ if " instant" in query and query [" instant" ]:
380+ model [" time_from" ] = model [" time_to" ]
316381
317382 payload = {
318383 "queries" : [query ],
319- "from" : str (model [' time_from' ] * 1000 ),
320- "to" : str (model [' time_to' ] * 1000 ),
384+ "from" : str (model [" time_from" ] * 1000 ),
385+ "to" : str (model [" time_to" ] * 1000 ),
321386 }
322387 request ["data" ] = payload
323388
@@ -330,12 +395,13 @@ def query_factory(datasource, model: Optional[dict]) -> Union[Dict, str]:
330395 "elasticsearch" : "url:///datasources/proxy/{datasource_id}/{database_name}/_mapping" ,
331396 "fetzerch-sunandmoon-datasource" : "url:///datasources/uid/{datasource_uid}" ,
332397 "grafana-simple-json-datasource" : "url:///datasources/proxy/{datasource_id}" ,
333- "graphite" : "random-walk.count;dc=asia-1;app=collector;server=000" , # from play.grafana.org explore Grafite datasource.
398+ # From play.grafana.org, to explore Graphite datasource.
399+ "graphite" : "random-walk.count;dc=asia-1;app=collector;server=000" ,
334400 "influxdb" : "SHOW RETENTION POLICIES on _internal" ,
335401 "influxdb+influxql" : "SHOW RETENTION POLICIES on _internal" ,
336402 "influxdb+flux" : "buckets()" ,
337403 "jaeger" : "url:///datasources/proxy/{datasource_id}/api/services" ,
338- "loki" : " count_over_time({job=~\ " .+\ " } [5m])" ,
404+ "loki" : ' count_over_time({job=~".+"} [5m])' ,
339405 "mssql" : "SELECT 1" ,
340406 "mysql" : "SELECT 1" ,
341407 "opentsdb" : "url:///datasources/proxy/{datasource_id}/api/suggest?type=metrics&q=cpu&max=100" ,
0 commit comments