diff --git a/public/app/features/dashboard/state/DashboardMigrator.ts b/public/app/features/dashboard/state/DashboardMigrator.ts index 3d037d064c2..ed0d87a0457 100644 --- a/public/app/features/dashboard/state/DashboardMigrator.ts +++ b/public/app/features/dashboard/state/DashboardMigrator.ts @@ -40,6 +40,7 @@ import { getStandardFieldConfigs, getStandardOptionEditors } from '@grafana/ui'; standardEditorsRegistry.setInit(getStandardOptionEditors); standardFieldConfigEditorRegistry.setInit(getStandardFieldConfigs); +type PanelSchemeUpgradeHandler = (panel: PanelModel) => PanelModel; export class DashboardMigrator { dashboard: DashboardModel; @@ -50,7 +51,7 @@ export class DashboardMigrator { updateSchema(old: any) { let i, j, k, n; const oldVersion = this.dashboard.schemaVersion; - const panelUpgrades = []; + const panelUpgrades: PanelSchemeUpgradeHandler[] = []; this.dashboard.schemaVersion = 30; if (oldVersion === this.dashboard.schemaVersion) { @@ -71,8 +72,9 @@ export class DashboardMigrator { if (panel.type === 'graphite') { panel.type = 'graph'; } + if (panel.type !== 'graph') { - return; + return panel; } if (isBoolean(panel.legend)) { @@ -106,6 +108,8 @@ export class DashboardMigrator { panel.y_formats[1] = panel.y2_format; delete panel.y2_format; } + + return panel; }); } @@ -118,6 +122,8 @@ export class DashboardMigrator { panel.id = maxId; maxId += 1; } + + return panel; }); } @@ -126,12 +132,16 @@ export class DashboardMigrator { // move aliasYAxis changes panelUpgrades.push((panel: any) => { if (panel.type !== 'graph') { - return; + return panel; } + each(panel.aliasYAxis, (value, key) => { panel.seriesOverrides = [{ alias: key, yaxis: value }]; }); + delete panel.aliasYAxis; + + return panel; }); } @@ -175,6 +185,8 @@ export class DashboardMigrator { target.refId = panel.getNextQueryLetter && panel.getNextQueryLetter(); } }); + + return panel; }); } @@ -218,6 +230,8 @@ export class DashboardMigrator { } } }); + + return panel; }); } @@ -226,7 +240,7 @@ export class DashboardMigrator { // move aliasYAxis changes panelUpgrades.push((panel: any) => { if (panel.type !== 'singlestat' && panel.thresholds !== '') { - return; + return panel; } if (panel.thresholds) { @@ -237,6 +251,8 @@ export class DashboardMigrator { panel.thresholds = k.join(','); } } + + return panel; }); } @@ -245,7 +261,7 @@ export class DashboardMigrator { // move aliasYAxis changes panelUpgrades.push((panel: any) => { if (panel.type !== 'table') { - return; + return panel; } each(panel.styles, (style) => { @@ -255,6 +271,8 @@ export class DashboardMigrator { style.thresholds = k; } }); + + return panel; }); } @@ -279,10 +297,10 @@ export class DashboardMigrator { // update graph yaxes changes panelUpgrades.push((panel: any) => { if (panel.type !== 'graph') { - return; + return panel; } if (!panel.grid) { - return; + return panel; } if (!panel.yaxes) { @@ -321,6 +339,8 @@ export class DashboardMigrator { delete panel['y-axis']; delete panel['x-axis']; } + + return panel; }); } @@ -328,10 +348,10 @@ export class DashboardMigrator { // update graph yaxes changes panelUpgrades.push((panel: any) => { if (panel.type !== 'graph') { - return; + return panel; } if (!panel.grid) { - return; + return panel; } if (!panel.thresholds) { @@ -388,6 +408,8 @@ export class DashboardMigrator { delete panel.grid.threshold2; delete panel.grid.threshold2Color; delete panel.grid.thresholdLine; + + return panel; }); } @@ -413,7 +435,10 @@ export class DashboardMigrator { }) - 1 ]; } + delete panel.minSpan; + + return panel; }); } @@ -444,6 +469,8 @@ export class DashboardMigrator { delete panel.options.suffix; delete panel['options-gauge']; } + + return panel; }); } @@ -453,6 +480,8 @@ export class DashboardMigrator { if (panel.links && isArray(panel.links)) { panel.links = panel.links.map(upgradePanelLink); } + + return panel; }); } @@ -480,6 +509,8 @@ export class DashboardMigrator { ); } } + + return panel; }); } @@ -502,18 +533,22 @@ export class DashboardMigrator { panel.options.fieldOptions.defaults.links = panel.options.fieldOptions.defaults.links.map(updateLinks); } } + + return panel; }); } if (oldVersion < 22) { panelUpgrades.push((panel: any) => { if (panel.type !== 'table') { - return; + return panel; } each(panel.styles, (style) => { style.align = 'auto'; }); + + return panel; }); } @@ -533,13 +568,14 @@ export class DashboardMigrator { panelUpgrades.push((panel: any) => { const wasAngularTable = panel.type === 'table'; if (wasAngularTable && !panel.styles) { - return; // styles are missing so assumes default settings + return panel; // styles are missing so assumes default settings } const wasReactTable = panel.table === 'table2'; if (!wasAngularTable || wasReactTable) { - return; + return panel; } panel.type = wasAngularTable ? 'table-old' : 'table'; + return panel; }); } @@ -551,11 +587,12 @@ export class DashboardMigrator { panelUpgrades.push((panel: any) => { const wasReactText = panel.type === 'text2'; if (!wasReactText) { - return; + return panel; } panel.type = 'text'; delete panel.options.angular; + return panel; }); } @@ -577,8 +614,10 @@ export class DashboardMigrator { if (oldVersion < 28) { panelUpgrades.push((panel: PanelModel) => { if (panel.type === 'singlestat') { - migrateSinglestat(panel); + return migrateSinglestat(panel); } + + return panel; }); for (const variable of this.dashboard.templating.list) { @@ -627,10 +666,10 @@ export class DashboardMigrator { for (j = 0; j < this.dashboard.panels.length; j++) { for (k = 0; k < panelUpgrades.length; k++) { - panelUpgrades[k].call(this, this.dashboard.panels[j]); + this.dashboard.panels[j] = panelUpgrades[k].call(this, this.dashboard.panels[j]); if (this.dashboard.panels[j].panels) { for (n = 0; n < this.dashboard.panels[j].panels.length; n++) { - panelUpgrades[k].call(this, this.dashboard.panels[j].panels[n]); + this.dashboard.panels[j].panels[n] = panelUpgrades[k].call(this, this.dashboard.panels[j].panels[n]); } } } @@ -880,7 +919,14 @@ function migrateSinglestat(panel: PanelModel) { // If 'grafana-singlestat-panel' exists, move to that if (config.panels['grafana-singlestat-panel']) { panel.type = 'grafana-singlestat-panel'; - return; + return panel; + } + + let returnSaveModel = false; + + if (!panel.changePlugin) { + returnSaveModel = true; + panel = new PanelModel(panel); } // To make sure PanelModel.isAngularPlugin logic thinks the current panel is angular @@ -895,12 +941,18 @@ function migrateSinglestat(panel: PanelModel) { statPanelPlugin.meta = config.panels['stat']; panel.changePlugin(statPanelPlugin); } + + if (returnSaveModel) { + return panel.getSaveModel(); + } + + return panel; } function upgradeValueMappingsForPanel(panel: PanelModel) { const fieldConfig = panel.fieldConfig; if (!fieldConfig) { - return; + return panel; } fieldConfig.defaults.mappings = upgradeValueMappings(fieldConfig.defaults.mappings, fieldConfig.defaults.thresholds); @@ -912,6 +964,8 @@ function upgradeValueMappingsForPanel(panel: PanelModel) { } } } + + return panel; } function upgradeValueMappings(oldMappings: any, thresholds?: ThresholdsConfig): ValueMapping[] | undefined { @@ -982,4 +1036,6 @@ function migrateTooltipOptions(panel: PanelModel) { delete panel.options.tooltipOptions; } } + + return panel; }