Skip to content

Commit e040d50

Browse files
twou12031travilyu
authored andcommitted
[bugfix] deepflow datasource, not support use grafana variables in input
**Phenomenon and reproduction steps** none **Root cause and solution** none **Impactions** none **Test method** none **Affected branch(es)** - main **Checklist** - [ ] Dependencies update required - [ ] Common bug (similar problem in other repo)
1 parent 659ddec commit e040d50

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

deepflow-querier-datasource/src/QueryEditor.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import { TracingIdSelector } from 'components/TracingIdSelector'
4949
import { format as sqlFormatter } from 'sql-formatter'
5050
import './QueryEditor.css'
5151
import { getI18NLabelByName } from 'utils/i18n'
52-
import { genQueryParams } from 'utils/genQueryParams'
52+
import { genQueryParams, replaceIntervalAndVariables } from 'utils/genQueryParams'
5353

5454
type Props = QueryEditorProps<DataSource, MyQuery, MyDataSourceOptions>
5555

@@ -561,7 +561,8 @@ export class QueryEditor extends PureComponent<Props> {
561561
}
562562
let newQuery
563563
if (appType !== APPTYPE_APP_TRACING_FLAME) {
564-
const parsedQueryData = genQueryParams(addTimeToWhere(dataObj), {})
564+
const _queryText = JSON.stringify(addTimeToWhere(dataObj))
565+
const parsedQueryData = genQueryParams(JSON.parse(replaceIntervalAndVariables(_queryText)), {})
565566
// @ts-ignore
566567
const querierJsResult = querierJs.dfQuery(_.cloneDeep(parsedQueryData))
567568
const { returnTags, returnMetrics, sql } = querierJsResult.resource[0]

deepflow-querier-datasource/src/datasource.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
} from 'utils/tools'
2020
import _ from 'lodash'
2121
import * as querierJs from 'deepflow-sdk-js'
22-
import { genQueryParams, replaceInterval } from 'utils/genQueryParams'
22+
import { genQueryParams, replaceIntervalAndVariables } from 'utils/genQueryParams'
2323
import { DATA_SOURCE_SETTINGS, QUERY_DATA_CACHE, SQL_CACHE } from 'utils/cache'
2424
import { MyVariableQuery } from 'components/VariableQueryEditor'
2525
import { Observable, of, zip } from 'rxjs'
@@ -191,7 +191,7 @@ export class DataSource extends DataSourceWithBackend<MyQuery, MyDataSourceOptio
191191
}
192192

193193
applyTemplateVariables(query: MyQuery & { requestId: string }, scopedVars: ScopedVars): any {
194-
const _queryText = replaceInterval(query.queryText, scopedVars)
194+
const _queryText = replaceIntervalAndVariables(query.queryText, scopedVars)
195195
const queryData = JSON.parse(_queryText)
196196
const result = {} as MyQuery
197197
// set new params after replaced variables

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ export function genQueryParams(queryData: Record<any, any>, scopedVars: ScopedVa
497497
'offset',
498498
'appType'
499499
] as const
500-
type KeyTypes = typeof keys[number]
500+
type KeyTypes = (typeof keys)[number]
501501
type Data = {
502502
[K in KeyTypes]?: string | BasicData[]
503503
}
@@ -529,19 +529,18 @@ export function genQueryParams(queryData: Record<any, any>, scopedVars: ScopedVa
529529
return result
530530
}
531531

532-
export const replaceInterval = (queryText: string, scopedVars: ScopedVars) => {
532+
export const replaceIntervalAndVariables = (queryText: string, scopedVars?: ScopedVars) => {
533+
let _queryText = queryText
533534
if (typeof scopedVars?.__interval_ms?.value === 'number') {
534535
const intervalSecond = scopedVars.__interval_ms.value / 1000
535-
return (
536-
queryText
537-
// convert string to number
538-
.split(`"${VAR_INTERVAL_LABEL}"`)
539-
.join(intervalSecond + '')
540-
.split(VAR_INTERVAL_LABEL)
541-
.join(intervalSecond + '')
542-
)
536+
_queryText = queryText
537+
// convert string to number
538+
.split(`"${VAR_INTERVAL_LABEL}"`)
539+
.join(intervalSecond + '')
540+
.split(VAR_INTERVAL_LABEL)
541+
.join(intervalSecond + '')
543542
}
544-
return queryText
543+
return getTemplateSrv().replace(_queryText, scopedVars)
545544
}
546545

547546
export default genQueryParams

0 commit comments

Comments
 (0)