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:
+1
-2
@@ -312,8 +312,7 @@ exports[`better eslint`] = {
|
|||||||
],
|
],
|
||||||
"packages/grafana-data/src/types/ScopedVars.ts:5381": [
|
"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.", "0"],
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "1"],
|
[0, 0, 0, "Unexpected any. Specify a different type.", "1"]
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "2"]
|
|
||||||
],
|
],
|
||||||
"packages/grafana-data/src/types/annotations.ts:5381": [
|
"packages/grafana-data/src/types/annotations.ts:5381": [
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
|
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
|
||||||
|
|||||||
@@ -413,7 +413,7 @@ export const getLinksSupplier =
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const variables = {
|
const variables: ScopedVars = {
|
||||||
...fieldScopedVars,
|
...fieldScopedVars,
|
||||||
__value: {
|
__value: {
|
||||||
text: 'Value',
|
text: 'Value',
|
||||||
@@ -423,10 +423,12 @@ export const getLinksSupplier =
|
|||||||
[DataLinkBuiltInVars.keepTime]: {
|
[DataLinkBuiltInVars.keepTime]: {
|
||||||
text: timeRangeUrl,
|
text: timeRangeUrl,
|
||||||
value: timeRangeUrl,
|
value: timeRangeUrl,
|
||||||
|
skipFormat: true,
|
||||||
},
|
},
|
||||||
[DataLinkBuiltInVars.includeVars]: {
|
[DataLinkBuiltInVars.includeVars]: {
|
||||||
text: variablesQuery,
|
text: variablesQuery,
|
||||||
value: variablesQuery,
|
value: variablesQuery,
|
||||||
|
skipFormat: true,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
export interface ScopedVar<T = any> {
|
export interface ScopedVar<T = any> {
|
||||||
text?: any;
|
text?: any;
|
||||||
value: T;
|
value: T;
|
||||||
[key: string]: any;
|
skipUrlSync?: boolean;
|
||||||
|
skipFormat?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ScopedVars extends Record<string, ScopedVar> {}
|
export interface ScopedVars extends Record<string, ScopedVar> {}
|
||||||
|
|||||||
@@ -647,11 +647,13 @@ export class PanelModel implements DataConfigSource, IPanelModel {
|
|||||||
vars[DataLinkBuiltInVars.keepTime] = {
|
vars[DataLinkBuiltInVars.keepTime] = {
|
||||||
text: timeRangeUrl,
|
text: timeRangeUrl,
|
||||||
value: timeRangeUrl,
|
value: timeRangeUrl,
|
||||||
|
skipFormat: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
vars[DataLinkBuiltInVars.includeVars] = {
|
vars[DataLinkBuiltInVars.includeVars] = {
|
||||||
text: variablesQuery,
|
text: variablesQuery,
|
||||||
value: variablesQuery,
|
value: variablesQuery,
|
||||||
|
skipFormat: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
return getTemplateSrv().replace(value, vars, format);
|
return getTemplateSrv().replace(value, vars, format);
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import {
|
|||||||
DataSourceInstanceSettings,
|
DataSourceInstanceSettings,
|
||||||
DataSourcePlugin,
|
DataSourcePlugin,
|
||||||
DataSourcePluginMeta,
|
DataSourcePluginMeta,
|
||||||
ScopedVar,
|
ScopedVars,
|
||||||
} from '@grafana/data';
|
} from '@grafana/data';
|
||||||
import { ExpressionDatasourceRef } from '@grafana/runtime/src/utils/DataSourceWithBackend';
|
import { ExpressionDatasourceRef } from '@grafana/runtime/src/utils/DataSourceWithBackend';
|
||||||
import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
|
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) {
|
if (scopedVars && scopedVars.datasource) {
|
||||||
return v.replace('${datasource}', scopedVars.datasource.value);
|
return v.replace('${datasource}', scopedVars.datasource.value);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -821,6 +821,15 @@ describe('templateSrv', () => {
|
|||||||
const target = _templateSrv.replace('${adhoc}', { adhoc: { value: 'value2', text: 'value2' } }, 'queryparam');
|
const target = _templateSrv.replace('${adhoc}', { adhoc: { value: 'value2', text: 'value2' } }, 'queryparam');
|
||||||
expect(target).toBe('var-adhoc=value2');
|
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', () => {
|
describe('scenes compatibility', () => {
|
||||||
|
|||||||
@@ -295,13 +295,17 @@ export class TemplateSrv implements BaseTemplateSrv {
|
|||||||
return target.replace(this.regex, (match, var1, var2, fmt2, var3, fieldPath, fmt3) => {
|
return target.replace(this.regex, (match, var1, var2, fmt2, var3, fieldPath, fmt3) => {
|
||||||
const variableName = var1 || var2 || var3;
|
const variableName = var1 || var2 || var3;
|
||||||
const variable = this.getVariableAtIndex(variableName);
|
const variable = this.getVariableAtIndex(variableName);
|
||||||
const fmt = fmt2 || fmt3 || format;
|
let fmt = fmt2 || fmt3 || format;
|
||||||
|
|
||||||
if (scopedVars) {
|
if (scopedVars) {
|
||||||
const value = this.getVariableValue(variableName, fieldPath, scopedVars);
|
const value = this.getVariableValue(variableName, fieldPath, scopedVars);
|
||||||
const text = this.getVariableText(variableName, value, scopedVars);
|
const text = this.getVariableText(variableName, value, scopedVars);
|
||||||
|
|
||||||
if (value !== null && value !== undefined) {
|
if (value !== null && value !== undefined) {
|
||||||
|
if (scopedVars[variableName].skipFormat) {
|
||||||
|
fmt = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
return this.formatValue(value, fmt, variable, text);
|
return this.formatValue(value, fmt, variable, text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ describe('getAllVariableValuesForUrl', () => {
|
|||||||
|
|
||||||
it('should not set scoped value as url params', () => {
|
it('should not set scoped value as url params', () => {
|
||||||
const params = getVariablesUrlParams({
|
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);
|
expect(params['var-test']).toBe(undefined);
|
||||||
});
|
});
|
||||||
|
|||||||
+3
-3
@@ -513,8 +513,8 @@ describe('CloudWatchMetricsQueryRunner', () => {
|
|||||||
runner.handleMetricQueries(queries, {
|
runner.handleMetricQueries(queries, {
|
||||||
...request,
|
...request,
|
||||||
scopedVars: {
|
scopedVars: {
|
||||||
var1: { selected: true, value: 'var1-foo', text: '' },
|
var1: { value: 'var1-foo', text: '' },
|
||||||
var2: { selected: true, value: 'var2-foo', text: '' },
|
var2: { value: 'var2-foo', text: '' },
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
).toEmitValuesWith(() => {
|
).toEmitValuesWith(() => {
|
||||||
@@ -579,7 +579,7 @@ describe('CloudWatchMetricsQueryRunner', () => {
|
|||||||
runner.handleMetricQueries(queries, {
|
runner.handleMetricQueries(queries, {
|
||||||
...request,
|
...request,
|
||||||
scopedVars: {
|
scopedVars: {
|
||||||
var1: { selected: true, value: 'var1-foo', text: '' },
|
var1: { value: 'var1-foo', text: '' },
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
).toEmitValuesWith(() => {
|
).toEmitValuesWith(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user