triggers: fix adding new panel

This commit is contained in:
Alexander Zobnin
2018-11-16 14:09:40 +03:00
parent faf66a60e6
commit f5cb445b7a
2 changed files with 22 additions and 1 deletions

View File

@@ -6,6 +6,7 @@ export const CURRENT_SCHEMA_VERSION = 5;
export function migratePanelSchema(panel) {
if (isEmptyPanel(panel)) {
delete panel.targets;
return panel;
}
@@ -47,5 +48,9 @@ function getSchemaVersion(panel) {
}
function isEmptyPanel(panel) {
return !panel.datasource && !panel.datasources && !panel.triggers && !panel.targets;
return !panel.datasource && !panel.datasources && !panel.triggers && isEmptyTargets(panel.targets);
}
function isEmptyTargets(targets) {
return !targets || (_.isArray(targets) && (targets.length === 0 || targets.length === 1 && _.isEmpty(targets[0])));
}

View File

@@ -74,4 +74,20 @@ describe('Triggers Panel schema migration', () => {
}, PANEL_DEFAULTS);
expect(updatedPanelCtrl.panel).toEqual(expected);
});
it('should set default targets for new panel with empty targets', () => {
ctx.scope.panel = {
targets: [{}]
};
let updatedPanelCtrl = new TriggerPanelCtrl(ctx.scope, {}, timeoutMock, datasourceSrvMock, {}, {}, {});
let expected = _.defaultsDeep({
datasources: ['zabbix_default'],
targets: {
'zabbix_default': DEFAULT_TARGET
},
}, PANEL_DEFAULTS);
expect(updatedPanelCtrl.panel).toEqual(expected);
});
});