|
| 1 | +import { PanelModel } from '@grafana/data'; |
| 2 | +import { DefaultSettings } from 'options/DefaultSettings'; |
| 3 | +import { PanelSettings } from 'types'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Checks if the given options are in the format of version < 4.0.0. |
| 7 | + * @param options The options object which should be checked. |
| 8 | + */ |
| 9 | +function isLegacyFormat(options: any) { |
| 10 | + return !('showDummyData' in options['dataMapping']); |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Migrates the legacy iconMapping format to the iconMapping format of version > 4.0.0. |
| 15 | + * @param iconMappings The iconMappings object to be migrated. |
| 16 | + */ |
| 17 | +function migrateIconMapping(iconMappings: any) { |
| 18 | + const migratedIconMapping = []; |
| 19 | + for (const iconMapping of iconMappings) { |
| 20 | + migratedIconMapping.push({ |
| 21 | + pattern: iconMapping.name, |
| 22 | + filename: iconMapping.filename, |
| 23 | + }); |
| 24 | + } |
| 25 | + return migratedIconMapping; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Migrates the legacy panel settings from version < 4.0.0 to the new format introduced in version 4.0.0 |
| 30 | + * The newly introduced variable aggregationType will be set to $aggregationTyoe in order to ensure functionality with |
| 31 | + * the legacy setup of the panel. |
| 32 | + * All other newly added options will be set to their respective default values. |
| 33 | + * @param panel The panel object which should be migrated. |
| 34 | + */ |
| 35 | +export const PanelMigrationHandler = (panel: PanelModel<Partial<PanelSettings>> | any) => { |
| 36 | + const { settings } = panel; |
| 37 | + if (isLegacyFormat(settings)) { |
| 38 | + return { |
| 39 | + animate: settings.animate, |
| 40 | + sumTimings: settings.sumTimings, |
| 41 | + filterEmptyConnections: settings.filterEmptyConnections, |
| 42 | + style: { |
| 43 | + healthyColor: settings.style.healthyColor, |
| 44 | + dangerColor: settings.style.dangerColor, |
| 45 | + noDataColor: settings.style.unknownColor, |
| 46 | + }, |
| 47 | + showDebugInformation: settings.showDebugInformation, |
| 48 | + showConnectionStats: settings.showConnectionStats, |
| 49 | + externalIcons: migrateIconMapping(settings.externalIcons), |
| 50 | + icons: settings.serviceIcons, |
| 51 | + dataMapping: { |
| 52 | + aggregationType: '$aggregationType', |
| 53 | + sourceColumn: settings.dataMapping.sourceComponentPrefix + '$aggregationType', |
| 54 | + targetColumn: settings.dataMapping.targetComponentPrefix + '$aggregationType', |
| 55 | + |
| 56 | + responseTimeColumn: settings.dataMapping.responseTimeColumn, |
| 57 | + requestRateColumn: settings.dataMapping.requestRateColumn, |
| 58 | + errorRateColumn: settings.dataMapping.errorRateColumn, |
| 59 | + responseTimeOutgoingColumn: settings.dataMapping.responseTimeOutgoingColumn, |
| 60 | + requestRateOutgoingColumn: settings.dataMapping.requestRateOutgoingColumn, |
| 61 | + errorRateOutgoingColumn: settings.dataMapping.errorRateOutgoingColumn, |
| 62 | + |
| 63 | + extOrigin: settings.dataMapping.extOrigin, |
| 64 | + extTarget: settings.dataMapping.extTarget, |
| 65 | + type: settings.dataMapping.type, |
| 66 | + showDummyData: settings.showDummyData, |
| 67 | + |
| 68 | + baselineRtUpper: settings.dataMapping.baselineRtUpper, |
| 69 | + }, |
| 70 | + drillDownLink: settings.drillDownLink, |
| 71 | + showBaselines: settings.showBaselines, |
| 72 | + timeFormat: DefaultSettings.timeFormat, |
| 73 | + }; |
| 74 | + } |
| 75 | + return settings; |
| 76 | +}; |
0 commit comments