add defaults to alert list migration handler (#36547)

This commit is contained in:
Domas 2021-07-19 12:25:53 +03:00 committed by GitHub
parent f308ba91e3
commit 27b7c35ccc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 9 deletions

View File

@ -107,4 +107,30 @@ describe('AlertList Panel Migration', () => {
expect(panel).not.toHaveProperty('dashboardTags');
expect(panel).not.toHaveProperty('stateFilter');
});
it('should handle config with no options or stateFilter', () => {
const panel: Omit<PanelModel, 'fieldConfig'> & Record<string, any> = {
id: 7,
links: [],
pluginVersion: '7.4.0',
targets: [],
title: 'Usage',
type: 'alertlist',
onlyAlertsOnDashboard: false,
options: {},
};
const newOptions = alertListPanelMigrationHandler(panel as PanelModel);
expect(newOptions).toMatchObject({
showOptions: ShowOption.Current,
maxItems: 10,
sortOrder: SortOrder.AlphaAsc,
dashboardAlerts: false,
alertName: '',
dashboardTitle: '',
tags: [],
stateFilter: {},
folderId: undefined,
});
});
});

View File

@ -1,21 +1,22 @@
import { PanelModel } from '@grafana/data';
import { AlertListOptions } from './types';
import { AlertListOptions, ShowOption, SortOrder } from './types';
export const alertListPanelMigrationHandler = (
panel: PanelModel<AlertListOptions> & Record<string, any>
): Partial<AlertListOptions> => {
const newOptions: AlertListOptions = {
showOptions: panel.options.showOptions ?? panel.show,
maxItems: panel.options.maxItems ?? panel.limit,
sortOrder: panel.options.sortOrder ?? panel.sortOrder,
dashboardAlerts: panel.options.dashboardAlerts ?? panel.onlyAlertsOnDashboard,
alertName: panel.options.alertName ?? panel.nameFilter,
dashboardTitle: panel.options.dashboardTitle ?? panel.dashboardFilter,
showOptions: panel.options.showOptions ?? panel.show ?? ShowOption.Current,
maxItems: panel.options.maxItems ?? panel.limit ?? 10,
sortOrder: panel.options.sortOrder ?? panel.sortOrder ?? SortOrder.AlphaAsc,
dashboardAlerts: panel.options.dashboardAlerts ?? panel.onlyAlertsOnDashboard ?? false,
alertName: panel.options.alertName ?? panel.nameFilter ?? '',
dashboardTitle: panel.options.dashboardTitle ?? panel.dashboardFilter ?? '',
folderId: panel.options.folderId ?? panel.folderId,
tags: panel.options.tags ?? panel.dashboardTags,
tags: panel.options.tags ?? panel.dashboardTags ?? [],
stateFilter:
panel.options.stateFilter ??
panel.stateFilter.reduce((filterObj: any, curFilter: any) => ({ ...filterObj, [curFilter]: true }), {}),
panel.stateFilter?.reduce((filterObj: any, curFilter: any) => ({ ...filterObj, [curFilter]: true }), {}) ??
{},
};
const previousVersion = parseFloat(panel.pluginVersion || '7.4');