1
1
import { DataSourceInstanceSettings , CoreApp , ScopedVars , DataQueryRequest , LiveChannelScope , DataQueryResponse , LoadingState , rangeUtil } from '@grafana/data' ;
2
2
import { DataSourceWithBackend , getGrafanaLiveSrv , getTemplateSrv , TemplateSrv } from '@grafana/runtime' ;
3
- import { parseJsQuery , getBucketCount , parseJsQueryLegacy , datetimeToJson , base64UrlEncode , unixTsToMongoID } from './utils' ;
3
+ import { parseJsQuery , getBucketCount , parseJsQueryLegacy , base64UrlEncode , datetimeToJson , unixTsToMongoID } from './utils' ;
4
4
import { MongoDBQuery , MongoDataSourceOptions , DEFAULT_QUERY , QueryLanguage } from './types' ;
5
5
import { merge , Observable , of } from 'rxjs' ;
6
6
import { MongoDBVariableSupport } from 'variables'
@@ -17,17 +17,8 @@ export class MongoDBDataSource extends DataSourceWithBackend<MongoDBQuery, Mongo
17
17
}
18
18
19
19
applyTemplateVariables ( query : MongoDBQuery , scopedVars : ScopedVars ) {
20
- console . log ( "scopedVars" , scopedVars ) ;
21
- console . log ( "Vars" , this . templateSrv . getVariables ( ) ) ;
22
20
const variables = { ...scopedVars } ;
23
21
24
-
25
- const from = this . templateSrv . replace ( '$__from' , variables ) ;
26
- const to = this . templateSrv . replace ( '$__to' , variables ) ;
27
-
28
- variables . from = { value : datetimeToJson ( from ) }
29
- variables . to = { value : datetimeToJson ( to ) }
30
-
31
22
let queryText = query . queryText ! ;
32
23
if ( query . queryLanguage === QueryLanguage . JAVASCRIPT || query . queryLanguage === QueryLanguage . JAVASCRIPT_SHADOW ) {
33
24
const { jsonQuery } =
@@ -37,16 +28,24 @@ export class MongoDBDataSource extends DataSourceWithBackend<MongoDBQuery, Mongo
37
28
queryText = jsonQuery ! ;
38
29
}
39
30
31
+ // Get time range
32
+ const from = this . templateSrv . replace ( '$__from' , variables ) ;
33
+ const to = this . templateSrv . replace ( '$__to' , variables ) ;
40
34
41
- queryText = queryText
42
- . replaceAll ( / " \$ _ _ f r o m _ o i d " / g , `" ${ unixTsToMongoID ( from , '0' ) } "` )
43
- . replaceAll ( / " \$ _ _ t o _ o i d " / g , `" ${ unixTsToMongoID ( to , 'f' ) } "` ) ;
35
+ if ( from && to ) {
36
+ variables . __from_oid = { value : unixTsToMongoID ( from , '0' ) }
37
+ variables . __to_oid = { value : unixTsToMongoID ( to , 'f' ) }
44
38
45
- const interval = scopedVars [ '__interval_ms' ] ?. value ;
39
+ queryText = queryText
40
+ . replaceAll ( / " \$ f r o m " / g, datetimeToJson ( from ) )
41
+ . replaceAll ( / " \$ t o " / g, datetimeToJson ( to ) ) ;
42
+ }
46
43
47
- // Compatible with legacy plugin $dateBucketCount
44
+
45
+ const interval = scopedVars [ '__interval_ms' ] ?. value ;
46
+ // $dateBucketCount
48
47
if ( interval && from && to ) {
49
- queryText = queryText . replaceAll ( / " \$ d a t e B u c k e t C o u n t " / g , getBucketCount ( from , to , interval ) . toString ( ) ) ;
48
+ variables . dateBucketCount = { value : getBucketCount ( from , to , interval ) }
50
49
}
51
50
52
51
const text = this . templateSrv . replace ( queryText , variables ) ;
0 commit comments