From ec1ae002ea3b0227251c3425a1d414d3ef35bd47 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Thu, 7 Apr 2022 07:00:18 -0700 Subject: [PATCH] TestData: Interpolate variables in more fields (#47158) (#47450) * TestData: Interpolate variables in more fields * only interpolate variables if the field is set * Correctly type scenarioId which can be undefined (cherry picked from commit b857f8339ed58a20a7bb6f61c6a3e784164faa1c) Co-authored-by: Josh Hunt --- .../datasource/testdata/QueryEditor.tsx | 2 +- .../plugins/datasource/testdata/datasource.ts | 30 ++++++++++++++----- .../app/plugins/datasource/testdata/types.ts | 2 +- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/public/app/plugins/datasource/testdata/QueryEditor.tsx b/public/app/plugins/datasource/testdata/QueryEditor.tsx index 1c762c22730..f3590a6ed14 100644 --- a/public/app/plugins/datasource/testdata/QueryEditor.tsx +++ b/public/app/plugins/datasource/testdata/QueryEditor.tsx @@ -161,7 +161,7 @@ export const QueryEditor = ({ query, datasource, onChange, onRunQuery }: Props) .sort((a, b) => a.label.localeCompare(b.label)), [scenarioList] ); - const showLabels = useMemo(() => showLabelsFor.includes(query.scenarioId), [query]); + const showLabels = useMemo(() => showLabelsFor.includes(query.scenarioId ?? ''), [query]); if (loading) { return null; diff --git a/public/app/plugins/datasource/testdata/datasource.ts b/public/app/plugins/datasource/testdata/datasource.ts index 09b8cc133b8..7a16cf781a9 100644 --- a/public/app/plugins/datasource/testdata/datasource.ts +++ b/public/app/plugins/datasource/testdata/datasource.ts @@ -81,10 +81,6 @@ export class TestDataDataSource extends DataSourceWithBackend { } default: - if (target.alias) { - target.alias = this.templateSrv.replace(target.alias, options.scopedVars); - } - backendQueries.push(target); } } @@ -105,7 +101,24 @@ export class TestDataDataSource extends DataSourceWithBackend { } resolveTemplateVariables(query: TestDataQuery, scopedVars: ScopedVars) { - query.labels = this.templateSrv.replace(query.labels!, scopedVars); + if (query.labels) { + query.labels = this.templateSrv.replace(query.labels, scopedVars); + } + if (query.alias) { + query.alias = this.templateSrv.replace(query.alias, scopedVars); + } + if (query.scenarioId) { + query.scenarioId = this.templateSrv.replace(query.scenarioId, scopedVars); + } + if (query.stringInput) { + query.stringInput = this.templateSrv.replace(query.stringInput, scopedVars); + } + if (query.csvContent) { + query.csvContent = this.templateSrv.replace(query.csvContent, scopedVars); + } + if (query.rawFrameContent) { + query.rawFrameContent = this.templateSrv.replace(query.rawFrameContent, scopedVars); + } } annotationDataTopicTest(target: TestDataQuery, req: DataQueryRequest): Observable { @@ -139,10 +152,13 @@ export class TestDataDataSource extends DataSourceWithBackend { } getQueryDisplayText(query: TestDataQuery) { + const scenario = query.scenarioId ?? 'Default scenario'; + if (query.alias) { - return query.scenarioId + ' as ' + query.alias; + return scenario + ' as ' + query.alias; } - return query.scenarioId; + + return scenario; } testDatasource() { diff --git a/public/app/plugins/datasource/testdata/types.ts b/public/app/plugins/datasource/testdata/types.ts index f34442fa571..91b13e209c6 100644 --- a/public/app/plugins/datasource/testdata/types.ts +++ b/public/app/plugins/datasource/testdata/types.ts @@ -8,7 +8,7 @@ export interface Scenario { export interface TestDataQuery extends DataQuery { alias?: string; - scenarioId: string; + scenarioId?: string; stringInput?: string; stream?: StreamingQuery; pulseWave?: PulseWaveQuery;