Graph: Preseve existing data links in field config when loading dashboard (#27295)

* Graph: Preseve existing data links in field config

* ts fix
This commit is contained in:
Torkel Ödegaard 2020-08-31 17:53:21 +02:00 committed by GitHub
parent 06323b8e52
commit 4b8f4df18d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 3 deletions

View File

@ -203,6 +203,7 @@ export class PanelModel implements DataConfigSource {
getOptions() {
return this.options;
}
getFieldConfig() {
return this.fieldConfig;
}
@ -309,6 +310,7 @@ export class PanelModel implements DataConfigSource {
if (plugin.angularConfigCtrl) {
return;
}
this.options = _.mergeWith({}, plugin.defaults, this.options || {}, (objValue: any, srcValue: any): any => {
if (_.isArray(srcValue)) {
return srcValue;

View File

@ -126,4 +126,25 @@ describe('Graph Panel Migrations', () => {
const link = fieldSource.defaults.links![0];
expect(link.url).toEqual('THE DRILLDOWN URL');
});
it('from 7.1 it should preserve existing fieldConfig', () => {
const panel = ({
id: 1,
fieldConfig: {
defaults: {
links: [
{
title: 'Details',
url: '/link',
},
],
},
overrides: [],
},
} as unknown) as PanelModel;
graphPanelMigrationHandler(panel as PanelModel);
const fieldConfig = (panel as any).fieldConfig as FieldConfigSource;
expect(fieldConfig.defaults.links).toHaveLength(1);
});
});

View File

@ -4,7 +4,7 @@ import { PanelModel, FieldConfigSource, DataLink } from '@grafana/data';
* Called when upgrading from a previously saved versoin
*/
export const graphPanelMigrationHandler = (panel: PanelModel<any>): Partial<any> => {
const fieldOptions: FieldConfigSource = {
const fieldConfig: FieldConfigSource = panel.fieldConfig ?? {
defaults: {},
overrides: [],
};
@ -13,12 +13,12 @@ export const graphPanelMigrationHandler = (panel: PanelModel<any>): Partial<any>
// Move <7.1 dataLinks to the field section
if (options.dataLinks) {
fieldOptions.defaults.links = options.dataLinks as DataLink[];
fieldConfig.defaults.links = options.dataLinks as DataLink[];
delete options.dataLinks;
}
// Mutate the original panel state (only necessary because it is angular)
panel.options = options;
panel.fieldConfig = fieldOptions;
panel.fieldConfig = fieldConfig;
return options;
};