1
- import { DataSourceInstanceSettings , CoreApp , ScopedVars , DataQueryRequest , DataQueryResponse } from "@grafana/data" ;
1
+ import { DataSourceInstanceSettings , CoreApp , ScopedVars , DataQueryRequest , DataQueryResponse , LegacyMetricFindQueryOptions , MetricFindValue , dateTime } from "@grafana/data" ;
2
2
import { DataSourceWithBackend , getTemplateSrv } from "@grafana/runtime" ;
3
- import { parseJsQuery , datetimeToJson , getBucketCount , parseJsQueryLegacy } from "./utils" ;
4
- import { MongoQuery , MongoDataSourceOptions , DEFAULT_QUERY , QueryLanguage } from "./types" ;
5
- import { Observable } from "rxjs" ;
3
+ import { parseJsQuery , datetimeToJson , getBucketCount , parseJsQueryLegacy , randomId , getMetricValues } from "./utils" ;
4
+ import { MongoQuery , MongoDataSourceOptions , DEFAULT_QUERY , QueryLanguage , VariableQuery } from "./types" ;
5
+ import { Observable , firstValueFrom } from "rxjs" ;
6
6
7
7
8
8
export class DataSource extends DataSourceWithBackend < MongoQuery , MongoDataSourceOptions > {
@@ -21,6 +21,40 @@ export class DataSource extends DataSourceWithBackend<MongoQuery, MongoDataSourc
21
21
} ;
22
22
}
23
23
24
+ async metricFindQuery ( query : VariableQuery , options ?: LegacyMetricFindQueryOptions ) : Promise < MetricFindValue [ ] > {
25
+ const request : DataQueryRequest < MongoQuery > = {
26
+ requestId : "variable-query-" + randomId ( 3 ) ,
27
+ targets : [ {
28
+ refId : "A" ,
29
+ queryLanguage : QueryLanguage . JSON ,
30
+ collection : query . collection ,
31
+ queryText : getTemplateSrv ( ) . replace ( query . queryText ) ,
32
+ queryType : "table"
33
+ } ] ,
34
+ scopedVars : options ?. scopedVars || { } ,
35
+ interval : "5s" ,
36
+ timezone : "browser" ,
37
+ intervalMs : 5000 ,
38
+ range : options ?. range || {
39
+ from : dateTime ( ) ,
40
+ to : dateTime ( ) ,
41
+ raw : {
42
+ from : "now" ,
43
+ to : "now"
44
+ }
45
+ } ,
46
+ app : "variable-query" ,
47
+ startTime : ( options ?. range ?. from || dateTime ( ) ) . toDate ( ) . getUTCMilliseconds ( )
48
+ } ;
49
+
50
+ const resp = await firstValueFrom ( this . query ( request ) ) ;
51
+ if ( resp . errors ?. length && resp . errors . length > 0 ) {
52
+ throw new Error ( resp . errors [ 0 ] . message || "Unknown error" ) ;
53
+ }
54
+
55
+ return getMetricValues ( resp ) ;
56
+ }
57
+
24
58
filterQuery ( query : MongoQuery ) : boolean {
25
59
return ! ! query . queryText && ! ! query . collection ;
26
60
}
0 commit comments