DataLinks: Fixes interpolation (formatting) of __all_variables and __url_time_range (#65162)

* DataLinks: Fixes interpolation (formatting) of __all_variables and __url_time_range

* simplify if statement
This commit is contained in:
Torkel Ödegaard 2023-03-22 18:56:18 +01:00 committed by GitHub
parent 0409cfd116
commit cd490ba0be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 28 additions and 11 deletions

View File

@ -312,8 +312,7 @@ exports[`better eslint`] = {
],
"packages/grafana-data/src/types/ScopedVars.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
[0, 0, 0, "Unexpected any. Specify a different type.", "1"],
[0, 0, 0, "Unexpected any. Specify a different type.", "2"]
[0, 0, 0, "Unexpected any. Specify a different type.", "1"]
],
"packages/grafana-data/src/types/annotations.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],

View File

@ -413,7 +413,7 @@ export const getLinksSupplier =
}
}
const variables = {
const variables: ScopedVars = {
...fieldScopedVars,
__value: {
text: 'Value',
@ -423,10 +423,12 @@ export const getLinksSupplier =
[DataLinkBuiltInVars.keepTime]: {
text: timeRangeUrl,
value: timeRangeUrl,
skipFormat: true,
},
[DataLinkBuiltInVars.includeVars]: {
text: variablesQuery,
value: variablesQuery,
skipFormat: true,
},
};

View File

@ -1,7 +1,8 @@
export interface ScopedVar<T = any> {
text?: any;
value: T;
[key: string]: any;
skipUrlSync?: boolean;
skipFormat?: boolean;
}
export interface ScopedVars extends Record<string, ScopedVar> {}

View File

@ -647,11 +647,13 @@ export class PanelModel implements DataConfigSource, IPanelModel {
vars[DataLinkBuiltInVars.keepTime] = {
text: timeRangeUrl,
value: timeRangeUrl,
skipFormat: true,
};
vars[DataLinkBuiltInVars.includeVars] = {
text: variablesQuery,
value: variablesQuery,
skipFormat: true,
};
return getTemplateSrv().replace(value, vars, format);

View File

@ -3,7 +3,7 @@ import {
DataSourceInstanceSettings,
DataSourcePlugin,
DataSourcePluginMeta,
ScopedVar,
ScopedVars,
} from '@grafana/data';
import { ExpressionDatasourceRef } from '@grafana/runtime/src/utils/DataSourceWithBackend';
import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
@ -26,7 +26,7 @@ const templateSrv: any = {
},
},
],
replace: (v: string, scopedVars: ScopedVar) => {
replace: (v: string, scopedVars: ScopedVars) => {
if (scopedVars && scopedVars.datasource) {
return v.replace('${datasource}', scopedVars.datasource.value);
}

View File

@ -821,6 +821,15 @@ describe('templateSrv', () => {
const target = _templateSrv.replace('${adhoc}', { adhoc: { value: 'value2', text: 'value2' } }, 'queryparam');
expect(target).toBe('var-adhoc=value2');
});
it('Variable named ${__all_variables} is already formatted so skip any formatting', () => {
const target = _templateSrv.replace(
'${__all_variables}',
{ __all_variables: { value: 'var-server=server+name+with+plus%2B', skipFormat: true } },
'percentencode'
);
expect(target).toBe('var-server=server+name+with+plus%2B');
});
});
describe('scenes compatibility', () => {

View File

@ -295,13 +295,17 @@ export class TemplateSrv implements BaseTemplateSrv {
return target.replace(this.regex, (match, var1, var2, fmt2, var3, fieldPath, fmt3) => {
const variableName = var1 || var2 || var3;
const variable = this.getVariableAtIndex(variableName);
const fmt = fmt2 || fmt3 || format;
let fmt = fmt2 || fmt3 || format;
if (scopedVars) {
const value = this.getVariableValue(variableName, fieldPath, scopedVars);
const text = this.getVariableText(variableName, value, scopedVars);
if (value !== null && value !== undefined) {
if (scopedVars[variableName].skipFormat) {
fmt = undefined;
}
return this.formatValue(value, fmt, variable, text);
}
}

View File

@ -107,7 +107,7 @@ describe('getAllVariableValuesForUrl', () => {
it('should not set scoped value as url params', () => {
const params = getVariablesUrlParams({
test: { name: 'test', value: 'val1', text: 'val1text', skipUrlSync: true },
test: { value: 'val1', text: 'val1text', skipUrlSync: true },
});
expect(params['var-test']).toBe(undefined);
});

View File

@ -513,8 +513,8 @@ describe('CloudWatchMetricsQueryRunner', () => {
runner.handleMetricQueries(queries, {
...request,
scopedVars: {
var1: { selected: true, value: 'var1-foo', text: '' },
var2: { selected: true, value: 'var2-foo', text: '' },
var1: { value: 'var1-foo', text: '' },
var2: { value: 'var2-foo', text: '' },
},
})
).toEmitValuesWith(() => {
@ -579,7 +579,7 @@ describe('CloudWatchMetricsQueryRunner', () => {
runner.handleMetricQueries(queries, {
...request,
scopedVars: {
var1: { selected: true, value: 'var1-foo', text: '' },
var1: { value: 'var1-foo', text: '' },
},
})
).toEmitValuesWith(() => {