@@ -308,78 +308,6 @@ def series(self, datasource_id, match, start, end, access="proxy"):
308308 r = self .client .POST (post_series_path , data = {"match[]" : match , "start" : start , "end" : end })
309309 return r
310310
311- def smartquery_old (self , datasource : Union [DatasourceIdentifier , Dict ], expression : str , store : Optional [str ] = None ):
312- """
313- Send a query to the designated data source and return its response.
314-
315- TODO: This is by far not complete. The `query_factory` function has to
316- be made more elaborate in order to query different data source
317- types.
318- """
319-
320- if isinstance (datasource , DatasourceIdentifier ):
321- datasource = self .get (datasource )
322-
323- datasource_id = datasource ["id" ]
324- datasource_type = datasource ["type" ]
325- datasource_dialect = datasource .get ("jsonData" , {}).get ("version" , "InfluxQL" )
326- access_type = datasource ["access" ]
327-
328- # Sanity checks.
329- if not expression :
330- raise ValueError ("Expression must be given" )
331-
332- # Build the query payload. Each data source has different query attributes.
333- query = query_factory (datasource , expression , store )
334-
335- logger .info (f"Submitting query: { query } " )
336-
337- # Compute request method, body, and endpoint.
338- send_request = self .client .POST
339-
340- # Certain data sources like InfluxDB 1.x, still use the `/datasources/proxy` route.
341- if datasource_type == "influxdb" and datasource_dialect == "InfluxQL" :
342- url = f"/datasources/proxy/{ datasource_id } /query"
343- if store is not None :
344- url += f"?db={ store } "
345- payload = {"q" : query ["q" ]}
346- request_kwargs = {"data" : payload }
347-
348- elif datasource_type == "graphite" :
349- url = f"/datasources/proxy/{ datasource_id } /render"
350- request_kwargs = {"data" : query }
351-
352- # This case is very special. It is used for Elasticsearch and Testdata.
353- elif expression .startswith ("url://" ):
354- url = expression .replace ("url://" , "" )
355- url = url .format (
356- datasource_id = datasource .get ("id" ),
357- datasource_uid = datasource .get ("uid" ),
358- database_name = datasource .get ("database" ),
359- )
360- request_kwargs = {}
361- send_request = self .client .GET
362-
363- # For all others, use the generic data source communication endpoint.
364- elif access_type in ["server" , "proxy" ]:
365- url = "/ds/query"
366- payload = {"queries" : [query ]}
367- request_kwargs = {"json" : payload }
368-
369- else :
370- raise NotImplementedError (f"Unable to submit query to data source with access type '{ access_type } '" )
371-
372- # Submit query.
373- try :
374- r = send_request (url , ** request_kwargs )
375- # logger.debug(f"Response from generic data source query: {r}")
376- return r
377- except (GrafanaClientError , GrafanaServerError ) as ex :
378- logger .error (
379- f"Querying data source failed. id={ datasource_id } , type={ datasource_type } . "
380- f"Reason: { ex } . Response: { ex .response or '<empty>' } "
381- )
382- raise
383311#**********************************************************************************
384312 def smartquery (self , datasource : Union [DatasourceIdentifier , Dict ], expression : str , attrs : Optional [dict ] = None , request : Optional [dict ] = None ):
385313 """
@@ -431,8 +359,8 @@ def smartquery(self, datasource: Union[DatasourceIdentifier, Dict], expression:
431359 request_kwargs = {"data" : request ["data" ]}
432360
433361 # This case is very special. It is used for Elasticsearch and Testdata.
434- elif expression .startswith ("url://" ):
435- url = expression .replace ("url://" , "" )
362+ elif 'url' in request and model [ 'url' ] .startswith ("url://" ):
363+ url = model [ 'url' ] .replace ("url://" , "" )
436364 url = url .format (
437365 datasource_id = datasource .get ("id" ),
438366 datasource_uid = datasource .get ("uid" ),
@@ -441,6 +369,21 @@ def smartquery(self, datasource: Union[DatasourceIdentifier, Dict], expression:
441369 request_kwargs = {}
442370 send_request = self .client .GET
443371
372+ elif datasource_type in ("prometheus" , "loki" ) and LooseVersion (self .api .version ) <= VERSION_7 :
373+ if request ["data" ]["format" ] != "table" :
374+ return self .query (
375+ datasource .get ("id" ),
376+ request ["expr" ],
377+ request ["data" ]["to" ],
378+ )
379+ else :
380+ return self .query_range (
381+ datasource .get ("id" ),
382+ request ["expr" ],
383+ request ["data" ]["from" ],
384+ request ["data" ]["to" ],
385+ request ["data" ]["step" ],
386+ )
444387 # For all others, use the generic data source communication endpoint.
445388 elif access_type in ["server" , "proxy" ]:
446389 url = "/ds/query"
0 commit comments