Skip to content

Commit 96817af

Browse files
twou12031travilyu
authored andcommitted
[feature] deepflow querier datasource, add panel repeat by variables support
**Phenomenon and reproduction steps** none **Root cause and solution** none **Impactions** none **Test method** - deepflow agent dashboards - queue row - click setting icon - select 'Repeat for' 'module' - panels generation automatic - one for one variable 'module' value **Affected branch(es)** - main **Checklist** - [ ] Dependencies update required - [ ] Common bug (similar problem in other repo)
1 parent 83b73a7 commit 96817af

File tree

3 files changed

+51
-44
lines changed

3 files changed

+51
-44
lines changed

deepflow-querier-datasource/src/QueryEditor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export class QueryEditor extends PureComponent<Props> {
175175
}
176176

177177
get sqlContent() {
178-
const content = this.props.data?.request?.requestId ? _.get(SQL_CACHE, this.props.data?.request?.requestId) : ''
178+
const content = this.props.data?.request?.requestId ? _.get(SQL_CACHE, this.props.data?.request?.requestId, '') : ''
179179
const sqlString = sqlFormatter(content, {
180180
tabWidth: 2,
181181
keywordCase: 'upper',

deepflow-querier-datasource/src/datasource.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
152152
window.useTimeLogs && console.timeEnd('[Time Log][Querier: Format flame data]')
153153
return [frame]
154154
}
155-
const parsedQueryData = parseQueryStr(queryData)
155+
const parsedQueryData = parseQueryStr(queryData, options.scopedVars)
156156
let querierJsResult
157157
try {
158158
// @ts-ignore

deepflow-querier-datasource/src/utils/parseQueryStr.ts

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DataQueryRequest } from '@grafana/data'
1+
import { DataQueryRequest, ScopedVars } from '@grafana/data'
22
import { getTemplateSrv } from '@grafana/runtime'
33
import { BasicData } from 'components/QueryEditorFormRow'
44
import _ from 'lodash'
@@ -224,49 +224,56 @@ function selectFormat(data: any): {
224224
}
225225
}
226226

227-
function getValueByVariablesName(val: LabelItem, variables: any[], op: string) {
227+
function getValueByVariablesName(val: LabelItem, variables: any[], op: string, scopedVars: ScopedVars) {
228228
const isLikeOp = op.toUpperCase().includes('LIKE')
229229
const specVariables = ['__disabled', '__any']
230230
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+
}
251255

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+
}
264269
}
270+
} catch (error) {
271+
console.log(error)
265272
}
266273
return val.value
267274
}
268275

269-
function whereFormat(data: any, variables: any[]) {
276+
function whereFormat(data: any, variables: any[], scopedVars: ScopedVars) {
270277
const { db, from, where, having } = data
271278
const fullData = where.concat(having)
272279
const validKeys = ['type', 'key', 'func', 'op', 'val', 'params', 'subFuncs', 'whereOnly'] as const
@@ -283,12 +290,12 @@ function whereFormat(data: any, variables: any[]) {
283290
}
284291
if (key === 'val') {
285292
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)
287294
}
288295
if (Array.isArray(item[key])) {
289296
result[key] = (item[key] as LabelItem[])
290297
.map((e: LabelItem) => {
291-
return getValueByVariablesName(e, variables, item.op)
298+
return getValueByVariablesName(e, variables, item.op, scopedVars)
292299
})
293300
.flat(Infinity)
294301
}
@@ -471,7 +478,7 @@ function orderByFormat(orderBy: BasicData[]) {
471478
})
472479
}
473480

474-
function queryTextFormat(queryData: any) {
481+
function queryTextFormat(queryData: any, scopedVars: ScopedVars) {
475482
const keys = [
476483
'db',
477484
'from',
@@ -491,7 +498,7 @@ function queryTextFormat(queryData: any) {
491498
}
492499
const data = queryData as Data
493500
const templateSrv = getTemplateSrv()
494-
const variables = templateSrv.getVariables() as any[]
501+
const variables = _.cloneDeep(templateSrv.getVariables() as any[])
495502
return {
496503
format: 'sql',
497504
db: data.db,
@@ -502,7 +509,7 @@ function queryTextFormat(queryData: any) {
502509
{
503510
id: '0',
504511
isForbidden: false,
505-
condition: whereFormat(data, variables)
512+
condition: whereFormat(data, variables, scopedVars)
506513
}
507514
]
508515
},
@@ -513,8 +520,8 @@ function queryTextFormat(queryData: any) {
513520
}
514521
}
515522

516-
const parse = (str: string) => {
517-
return queryTextFormat(str)
523+
const parse = (str: string, scopedVars: ScopedVars) => {
524+
return queryTextFormat(str, scopedVars)
518525
}
519526

520527
export default parse

0 commit comments

Comments
 (0)