1
- import { DataQueryRequest } from '@grafana/data'
1
+ import { DataQueryRequest , ScopedVars } from '@grafana/data'
2
2
import { getTemplateSrv } from '@grafana/runtime'
3
3
import { BasicData } from 'components/QueryEditorFormRow'
4
4
import _ from 'lodash'
@@ -224,49 +224,56 @@ function selectFormat(data: any): {
224
224
}
225
225
}
226
226
227
- function getValueByVariablesName ( val : LabelItem , variables : any [ ] , op : string ) {
227
+ function getValueByVariablesName ( val : LabelItem , variables : any [ ] , op : string , scopedVars : ScopedVars ) {
228
228
const isLikeOp = op . toUpperCase ( ) . includes ( 'LIKE' )
229
229
const specVariables = [ '__disabled' , '__any' ]
230
230
const isVariable = val ?. isVariable
231
- if ( isVariable ) {
232
- const currentVariable = variables . find ( ( variable : any ) => {
233
- return variable . name === val ?. value
234
- } )
235
- const currentValue = _ . get ( currentVariable , [ 'current' , 'value' ] , '' )
236
- if ( currentVariable ?. type === undefined ) {
237
- return val . value
238
- }
239
- if ( [ 'textbox' , 'constant' ] . includes ( currentVariable . type ) ) {
240
- return currentValue
241
- }
242
- const targetField = isLikeOp ? 'text' : 'value'
243
- if ( currentValue . includes ( '$__all' ) ) {
244
- return currentVariable . options
245
- . filter ( ( e : any ) => e . value !== '$__all' && ! specVariables . includes ( e . value ) )
246
- . map ( ( e : any ) => _ . get ( e , [ targetField ] ) )
247
- }
248
- if ( currentValue . includes ( '__disabled' ) ) {
249
- return '__disabled'
250
- }
231
+ try {
232
+ if ( isVariable ) {
233
+ const currentVariable = variables . find ( ( variable : any ) => {
234
+ return variable . name === val ?. value
235
+ } )
236
+ if ( currentVariable && scopedVars [ val ?. value ] ) {
237
+ currentVariable . current = scopedVars [ val ?. value ]
238
+ }
239
+ const currentValue = _ . get ( currentVariable , [ 'current' , 'value' ] , '' )
240
+ if ( currentVariable ?. type === undefined ) {
241
+ return val . value
242
+ }
243
+ if ( [ 'textbox' , 'constant' ] . includes ( currentVariable . type ) ) {
244
+ return currentValue
245
+ }
246
+ const targetField = isLikeOp ? 'text' : 'value'
247
+ if ( currentValue . includes ( '$__all' ) ) {
248
+ return currentVariable . options
249
+ . filter ( ( e : any ) => e . value !== '$__all' && ! specVariables . includes ( e . value ) )
250
+ . map ( ( e : any ) => _ . get ( e , [ targetField ] ) )
251
+ }
252
+ if ( currentValue . includes ( '__disabled' ) ) {
253
+ return '__disabled'
254
+ }
251
255
252
- if (
253
- currentValue === '__any' ||
254
- ( Array . isArray ( currentValue ) && currentValue . filter ( ( e : string ) => e !== '__any' ) . length <= 0 )
255
- ) {
256
- return '__any'
257
- } else {
258
- const result = _ . get ( currentVariable , [ 'current' , targetField ] )
259
- return typeof result === 'string'
260
- ? result
261
- : result . filter ( ( e : string ) => {
262
- return e !== '__any' && e !== 'Any'
263
- } )
256
+ if (
257
+ currentValue === '__any' ||
258
+ ( Array . isArray ( currentValue ) && currentValue . filter ( ( e : string ) => e !== '__any' ) . length <= 0 )
259
+ ) {
260
+ return '__any'
261
+ } else {
262
+ const result = _ . get ( currentVariable , [ 'current' , targetField ] )
263
+ return typeof result === 'string'
264
+ ? result
265
+ : result . filter ( ( e : string ) => {
266
+ return e !== '__any' && e !== 'Any'
267
+ } )
268
+ }
264
269
}
270
+ } catch ( error ) {
271
+ console . log ( error )
265
272
}
266
273
return val . value
267
274
}
268
275
269
- function whereFormat ( data : any , variables : any [ ] ) {
276
+ function whereFormat ( data : any , variables : any [ ] , scopedVars : ScopedVars ) {
270
277
const { db, from, where, having } = data
271
278
const fullData = where . concat ( having )
272
279
const validKeys = [ 'type' , 'key' , 'func' , 'op' , 'val' , 'params' , 'subFuncs' , 'whereOnly' ] as const
@@ -283,12 +290,12 @@ function whereFormat(data: any, variables: any[]) {
283
290
}
284
291
if ( key === 'val' ) {
285
292
if ( item [ key ] instanceof Object ) {
286
- result [ key ] = getValueByVariablesName ( item [ key ] as LabelItem , variables , item . op )
293
+ result [ key ] = getValueByVariablesName ( item [ key ] as LabelItem , variables , item . op , scopedVars )
287
294
}
288
295
if ( Array . isArray ( item [ key ] ) ) {
289
296
result [ key ] = ( item [ key ] as LabelItem [ ] )
290
297
. map ( ( e : LabelItem ) => {
291
- return getValueByVariablesName ( e , variables , item . op )
298
+ return getValueByVariablesName ( e , variables , item . op , scopedVars )
292
299
} )
293
300
. flat ( Infinity )
294
301
}
@@ -471,7 +478,7 @@ function orderByFormat(orderBy: BasicData[]) {
471
478
} )
472
479
}
473
480
474
- function queryTextFormat ( queryData : any ) {
481
+ function queryTextFormat ( queryData : any , scopedVars : ScopedVars ) {
475
482
const keys = [
476
483
'db' ,
477
484
'from' ,
@@ -491,7 +498,7 @@ function queryTextFormat(queryData: any) {
491
498
}
492
499
const data = queryData as Data
493
500
const templateSrv = getTemplateSrv ( )
494
- const variables = templateSrv . getVariables ( ) as any [ ]
501
+ const variables = _ . cloneDeep ( templateSrv . getVariables ( ) as any [ ] )
495
502
return {
496
503
format : 'sql' ,
497
504
db : data . db ,
@@ -502,7 +509,7 @@ function queryTextFormat(queryData: any) {
502
509
{
503
510
id : '0' ,
504
511
isForbidden : false ,
505
- condition : whereFormat ( data , variables )
512
+ condition : whereFormat ( data , variables , scopedVars )
506
513
}
507
514
]
508
515
} ,
@@ -513,8 +520,8 @@ function queryTextFormat(queryData: any) {
513
520
}
514
521
}
515
522
516
- const parse = ( str : string ) => {
517
- return queryTextFormat ( str )
523
+ const parse = ( str : string , scopedVars : ScopedVars ) => {
524
+ return queryTextFormat ( str , scopedVars )
518
525
}
519
526
520
527
export default parse
0 commit comments