Commit fcfd5cf0 by Torkel Ödegaard Committed by GitHub

DataLinks: Fixes issue with data links not interpolating values with correct field config (#27622)

* DataLinks: Fixes issue with data links not having access to other fields field config

* Fixed test
parent e3b79ac4
......@@ -19,6 +19,7 @@ import {
GrafanaTheme,
InterpolateFunction,
ThresholdsMode,
ScopedVars,
} from '../types';
import { locationUtil, Registry } from '../utils';
import { mockStandardProperties } from '../utils/tests/mockStandardProperties';
......@@ -64,6 +65,16 @@ export const customFieldRegistry: FieldConfigOptionsRegistry = new Registry<Fiel
return [property1, property2, property3, shouldApplyFalse, ...mockStandardProperties()];
});
locationUtil.initialize({
getConfig: () => {
return { appSubUrl: '/subUrl' } as any;
},
// @ts-ignore
buildParamsFromVariables: () => {},
// @ts-ignore
getTimeRangeForUrl: () => {},
});
describe('Global MinMax', () => {
it('find global min max', () => {
const f0 = new MutableDataFrame();
......@@ -93,6 +104,7 @@ describe('applyFieldOverrides', () => {
defaults: {
unit: 'xyz',
decimals: 2,
links: [{ title: 'link', url: '${__value.text}' }],
},
overrides: [
{
......@@ -244,6 +256,28 @@ describe('applyFieldOverrides', () => {
// Don't Automatically pick the min value
expect(config.min).toEqual(-20);
});
it('getLinks should use applied field config', () => {
const replaceVariablesCalls: any[] = [];
const data = applyFieldOverrides({
data: [f0], // the frame
fieldConfig: src as FieldConfigSource, // defaults + overrides
replaceVariables: ((value: string, variables: ScopedVars) => {
replaceVariablesCalls.push(variables);
return value;
}) as InterpolateFunction,
getDataSourceSettingsByUid: undefined as any,
theme: (undefined as any) as GrafanaTheme,
autoMinMax: true,
fieldConfigRegistry: customFieldRegistry,
})[0];
data.fields[1].getLinks!({ valueRowIndex: 0 });
expect(data.fields[1].config.decimals).toEqual(1);
expect(replaceVariablesCalls[0].__value.value.text).toEqual('100.0');
});
});
describe('setFieldConfigDefaults', () => {
......
......@@ -101,6 +101,9 @@ export function applyFieldOverrides(options: ApplyFieldOverrideOptions): DataFra
}
return options.data.map((frame, index) => {
// Need to define this new frame here as it's passed to the getLinkSupplier function inside the fields loop
const newFrame: DataFrame = { ...frame };
const scopedVars: ScopedVars = {
__series: { text: 'Series', value: { name: getFrameDisplayName(frame, index) } }, // might be missing
};
......@@ -206,7 +209,7 @@ export function applyFieldOverrides(options: ApplyFieldOverrideOptions): DataFra
// Attach data links supplier
f.getLinks = getLinksSupplier(
frame,
newFrame,
f,
fieldScopedVars,
context.replaceVariables,
......@@ -220,10 +223,8 @@ export function applyFieldOverrides(options: ApplyFieldOverrideOptions): DataFra
return f;
});
return {
...frame,
fields,
};
newFrame.fields = fields;
return newFrame;
});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment