mirror of https://github.com/grafana/grafana
DashboardScene: Support auto migration for angular panels (#76100)
* DashboardScene: Support auto migration for angular panels * minor tweak * Update scenes * Review fix * Updatepull/76261/head
parent
ea741dda6b
commit
42218fbdbb
@ -0,0 +1,35 @@ |
||||
import { getPanelPlugin } from '@grafana/data/test/__mocks__/pluginMocks'; |
||||
import { PanelModel } from 'app/features/dashboard/state'; |
||||
|
||||
import { getAngularPanelMigrationHandler } from './angularMigration'; |
||||
|
||||
describe('getAngularPanelMigrationHandler', () => { |
||||
describe('Given an old angular panel', () => { |
||||
it('Should call migration handler', () => { |
||||
const onPanelTypeChanged = (panel: PanelModel, prevPluginId: string, prevOptions: Record<string, any>) => { |
||||
panel.fieldConfig = { defaults: { unit: 'bytes' }, overrides: [] }; |
||||
return { name: prevOptions.angular.oldOptionProp }; |
||||
}; |
||||
|
||||
const reactPlugin = getPanelPlugin({ id: 'timeseries' }).setPanelChangeHandler(onPanelTypeChanged as any); |
||||
|
||||
const oldModel = new PanelModel({ |
||||
autoMigrateFrom: 'graph', |
||||
oldOptionProp: 'old name', |
||||
type: 'timeseries', |
||||
}); |
||||
|
||||
const mutatedModel = { |
||||
id: 1, |
||||
type: 'timeseries', |
||||
options: {}, |
||||
fieldConfig: { defaults: {}, overrides: [] }, |
||||
}; |
||||
|
||||
getAngularPanelMigrationHandler(oldModel)(mutatedModel, reactPlugin); |
||||
|
||||
expect(mutatedModel.options).toEqual({ name: 'old name' }); |
||||
expect(mutatedModel.fieldConfig).toEqual({ defaults: { unit: 'bytes' }, overrides: [] }); |
||||
}); |
||||
}); |
||||
}); |
||||
@ -0,0 +1,22 @@ |
||||
import { PanelModel as PanelModelFromData, PanelPlugin } from '@grafana/data'; |
||||
import { autoMigrateAngular, PanelModel } from 'app/features/dashboard/state/PanelModel'; |
||||
|
||||
export function getAngularPanelMigrationHandler(oldModel: PanelModel) { |
||||
return function handleAngularPanelMigrations(panel: PanelModelFromData, plugin: PanelPlugin) { |
||||
if (plugin.angularPanelCtrl) { |
||||
panel.options = { angularOptions: oldModel.getOptionsToRemember() }; |
||||
return; |
||||
} |
||||
|
||||
if (oldModel.autoMigrateFrom) { |
||||
const wasAngular = autoMigrateAngular[oldModel.autoMigrateFrom] != null; |
||||
const oldOptions = oldModel.getOptionsToRemember(); |
||||
const prevPluginId = oldModel.autoMigrateFrom; |
||||
|
||||
if (plugin.onPanelTypeChanged) { |
||||
const prevOptions = wasAngular ? { angular: oldOptions } : oldOptions.options; |
||||
Object.assign(panel.options, plugin.onPanelTypeChanged(panel, prevPluginId, prevOptions, panel.fieldConfig)); |
||||
} |
||||
} |
||||
}; |
||||
} |
||||
Loading…
Reference in new issue