1
- import { DataSourceInstanceSettings , CoreApp , ScopedVars , DataQueryRequest , LiveChannelScope , DataQueryResponse , LoadingState , rangeUtil } from '@grafana/data' ;
1
+ import { DataSourceInstanceSettings , CoreApp , ScopedVars , DataQueryRequest , LiveChannelScope , DataQueryResponse , LoadingState } from '@grafana/data' ;
2
2
import { DataSourceWithBackend , getGrafanaLiveSrv , getTemplateSrv , TemplateSrv } from '@grafana/runtime' ;
3
- import { parseJsQuery , getBucketCount , parseJsQueryLegacy , base64UrlEncode , datetimeToJson , unixTsToMongoID } from './utils' ;
3
+ import { parseJsQuery , parseJsQueryLegacy , base64UrlEncode , 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,6 +17,7 @@ export class MongoDBDataSource extends DataSourceWithBackend<MongoDBQuery, Mongo
17
17
}
18
18
19
19
applyTemplateVariables ( query : MongoDBQuery , scopedVars : ScopedVars ) {
20
+ console . log ( "scopedVars" , scopedVars ) ;
20
21
const variables = { ...scopedVars } ;
21
22
22
23
let queryText = query . queryText ! ;
@@ -29,23 +30,42 @@ export class MongoDBDataSource extends DataSourceWithBackend<MongoDBQuery, Mongo
29
30
}
30
31
31
32
// Get time range
33
+ let from_ms : number | undefined = undefined ;
34
+ let to_ms : number | undefined = undefined ;
35
+
32
36
const from = this . templateSrv . replace ( '$__from' , variables ) ;
33
37
const to = this . templateSrv . replace ( '$__to' , variables ) ;
34
38
35
- if ( from && to ) {
36
- variables . __from_oid = { value : unixTsToMongoID ( from , '0' ) }
37
- variables . __to_oid = { value : unixTsToMongoID ( to , 'f' ) }
38
-
39
+ if ( from !== '$__from' ) {
40
+ from_ms = parseInt ( from , 10 ) ;
41
+ // $__from_oid
42
+ variables . __from_oid = { value : unixTsToMongoID ( from_ms , '0' ) }
43
+ // $from
39
44
queryText = queryText
40
- . replaceAll ( / " \$ f r o m " / g, datetimeToJson ( from ) )
41
- . replaceAll ( / " \$ t o " / g, datetimeToJson ( to ) ) ;
45
+ . replaceAll ( / " \$ f r o m " / g, JSON . stringify ( {
46
+ $date : {
47
+ $numberLong : from ,
48
+ } ,
49
+ } ) )
42
50
}
43
51
52
+ if ( to !== '$__to' ) {
53
+ to_ms = parseInt ( to , 10 ) ;
54
+ // $__to_oid
55
+ variables . __to_oid = { value : unixTsToMongoID ( to_ms , 'f' ) }
56
+ // $to
57
+ queryText = queryText
58
+ . replaceAll ( / " \$ t o " / g, JSON . stringify ( {
59
+ $date : {
60
+ $numberLong : to ,
61
+ } ,
62
+ } ) ) ;
63
+ }
44
64
45
- const interval = scopedVars [ '__interval_ms' ] ?. value ;
65
+ const interval_ms : number | undefined = scopedVars [ '__interval_ms' ] ?. value ;
46
66
// $dateBucketCount
47
- if ( interval && from && to ) {
48
- variables . dateBucketCount = { value : getBucketCount ( from , to , interval ) }
67
+ if ( interval_ms && from_ms && to_ms ) {
68
+ variables . dateBucketCount = { value : Math . ceil ( ( parseInt ( to , 10 ) - parseInt ( from , 10 ) ) / interval_ms ) }
49
69
}
50
70
51
71
const text = this . templateSrv . replace ( queryText , variables ) ;
0 commit comments