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

View File

@ -126,4 +126,25 @@ describe('Graph Panel Migrations', () => {
const link = fieldSource.defaults.links![0]; const link = fieldSource.defaults.links![0];
expect(link.url).toEqual('THE DRILLDOWN URL'); 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 * Called when upgrading from a previously saved versoin
*/ */
export const graphPanelMigrationHandler = (panel: PanelModel<any>): Partial<any> => { export const graphPanelMigrationHandler = (panel: PanelModel<any>): Partial<any> => {
const fieldOptions: FieldConfigSource = { const fieldConfig: FieldConfigSource = panel.fieldConfig ?? {
defaults: {}, defaults: {},
overrides: [], overrides: [],
}; };
@ -13,12 +13,12 @@ export const graphPanelMigrationHandler = (panel: PanelModel<any>): Partial<any>
// Move <7.1 dataLinks to the field section // Move <7.1 dataLinks to the field section
if (options.dataLinks) { if (options.dataLinks) {
fieldOptions.defaults.links = options.dataLinks as DataLink[]; fieldConfig.defaults.links = options.dataLinks as DataLink[];
delete options.dataLinks; delete options.dataLinks;
} }
// Mutate the original panel state (only necessary because it is angular) // Mutate the original panel state (only necessary because it is angular)
panel.options = options; panel.options = options;
panel.fieldConfig = fieldOptions; panel.fieldConfig = fieldConfig;
return options; return options;
}; };