import { i18n } from '@osd/i18n' ; import semver from 'semver' ; import uuid from 'uuid' ; import { GridData , SavedDashboardPanelTo60 , SavedDashboardPanel620 , SavedDashboardPanel630 , SavedDashboardPanel610 , } from './' ; import { RawSavedDashboardPanelTo60 , RawSavedDashboardPanel630 , RawSavedDashboardPanel640To720 , RawSavedDashboardPanel730ToLatest , RawSavedDashboardPanel610 , RawSavedDashboardPanel620 , } from './bwc/types' ; const PANEL_HEIGHT_SCALE_FACTOR = 5 ; const PANEL_HEIGHT_SCALE_FACTOR_WITH_MARGINS = 4 ; const PANEL_WIDTH_SCALE_FACTOR = 4 ; /**
 * Note!
 *
 * The 7.3.0 migrations reference versions that are quite old because for a long time all of this
 * migration logic was done ad hoc in the code itself, not on the indexed data (migrations didn't even
 * exist at the point most of that logic was put in place).
 *
 * So you could have a dashboard in 7.2.0 that was created in 6.3 and it will have data of a different
 * shape than some other dashboards that were created more recently.
 *
 * Moving forward migrations should be simpler since all 7.3.0+ dashboards should finally have the
 * same data.
 */ function isPre61Panel ( panel ) { return ( panel as RawSavedDashboardPanelTo60 ) . row !== undefined ; } function is61Panel ( panel ) { return semver . satisfies ( ( panel as RawSavedDashboardPanel610 ) . version , '6.1.x' ) ; } function is62Panel ( panel ) { return semver . satisfies ( ( panel as RawSavedDashboardPanel620 ) . version , '6.2.x' ) ; } function is63Panel ( panel ) { return semver . satisfies ( ( panel as RawSavedDashboardPanel630 ) . version , '6.3.x' ) ; } function is640To720Panel ( panel ) { return ( semver . satisfies ( ( panel as RawSavedDashboardPanel630 ) . version , '>6.3' ) && semver . satisfies ( ( panel as RawSavedDashboardPanel630 ) . version , '<7.3' ) ) ; } function migratePre61PanelToLatest ( panel , version , useMargins , uiState ? ) { if ( panel . col === undefined || panel . row === undefined ) { throw new Error ( i18n . translate ( 'dashboard.panel.unableToMigratePanelDataForSixOneZeroErrorMessage' , { defaultMessage : 'Unable to migrate panel data for "6.1.0" backwards compatibility, panel does not contain expected col and/or row fields' , } ) ) ; } const embeddableConfig = uiState ? uiState [ `P-${ panel . panelIndex }` ] || { } : { } ; if ( panel . columns || panel . sort ) { embeddableConfig . columns = panel . columns ; embeddableConfig . sort = panel . sort ; } const heightScaleFactor = useMargins ? PANEL_HEIGHT_SCALE_FACTOR_WITH_MARGINS : PANEL_HEIGHT_SCALE_FACTOR ; const DASHBOARD_GRID_COLUMN_COUNT = 48 ; const DEFAULT_PANEL_WIDTH = DASHBOARD_GRID_COLUMN_COUNT / 2 ; const DEFAULT_PANEL_HEIGHT = 15 ; const { columns , sort , row , col , size_x : sizeX , size_y : sizeY , ... rest } = panel ; const panelIndex = panel . panelIndex ? panel . panelIndex . toString ( ) : uuid . v4 ( ) ; return { ... rest , version , panelIndex , gridData : { x : ( col - 1 ) * PANEL_WIDTH_SCALE_FACTOR , y : ( row - 1 ) * heightScaleFactor , w : sizeX ? sizeX * PANEL_WIDTH_SCALE_FACTOR : DEFAULT_PANEL_WIDTH , h : sizeY ? sizeY * heightScaleFactor : DEFAULT_PANEL_HEIGHT , i : panelIndex , } , embeddableConfig , } ; } function migrate610PanelToLatest ( panel , version , useMargins , uiState ? ) { ( [ 'w' , 'x' , 'h' , 'y' ] as Array < keyof GridData > ) . forEach ( ( key ) => { if ( panel . gridData [ key ] === undefined ) { throw new Error ( i18n . translate ( 'dashboard.panel.unableToMigratePanelDataForSixThreeZeroErrorMessage' , { defaultMessage : 'Unable to migrate panel data for "6.3.0" backwards compatibility, panel does not contain expected field: {key}' , values : { key } , } ) ) ; } } ) ; const embeddableConfig = uiState ? uiState [ `P-${ panel . panelIndex }` ] || { } : { } ; if ( panel . columns || panel . sort ) { embeddableConfig . columns = panel . columns ; embeddableConfig . sort = panel . sort ; } const heightScaleFactor = useMargins ? PANEL_HEIGHT_SCALE_FACTOR_WITH_MARGINS : PANEL_HEIGHT_SCALE_FACTOR ; const { columns , sort , ... rest } = panel ; const panelIndex = panel . panelIndex ? panel . panelIndex . toString ( ) : uuid . v4 ( ) ; return { ... rest , version , panelIndex , gridData : { w : panel . gridData . w * PANEL_WIDTH_SCALE_FACTOR , h : panel . gridData . h * heightScaleFactor , x : panel . gridData . x * PANEL_WIDTH_SCALE_FACTOR , y : panel . gridData . y * heightScaleFactor , i : panel . gridData . i , } , embeddableConfig , } ; } function migrate620PanelToLatest ( panel , version , useMargins ) { const embeddableConfig = panel . embeddableConfig || { } ; if ( panel . columns || panel . sort ) { embeddableConfig . columns = panel . columns ; embeddableConfig . sort = panel . sort ; } const heightScaleFactor = useMargins ? PANEL_HEIGHT_SCALE_FACTOR_WITH_MARGINS : PANEL_HEIGHT_SCALE_FACTOR ; const { columns , sort , ... rest } = panel ; const panelIndex = panel . panelIndex ? panel . panelIndex . toString ( ) : uuid . v4 ( ) ; return { ... rest , version , panelIndex , gridData : { w : panel . gridData . w * PANEL_WIDTH_SCALE_FACTOR , h : panel . gridData . h * heightScaleFactor , x : panel . gridData . x * PANEL_WIDTH_SCALE_FACTOR , y : panel . gridData . y * heightScaleFactor , i : panel . gridData . i , } , embeddableConfig , } ; } function migrate630PanelToLatest ( panel , version ) { const embeddableConfig = panel . embeddableConfig || { } ; if ( panel . columns || panel . sort ) { embeddableConfig . columns = panel . columns ; embeddableConfig . sort = panel . sort ; } const { columns , sort , ... rest } = panel ; const panelIndex = panel . panelIndex ? panel . panelIndex . toString ( ) : uuid . v4 ( ) ; return { ... rest , version , panelIndex , embeddableConfig , } ; } function migrate640To720PanelsToLatest ( panel , version ) { const panelIndex = panel . panelIndex ? panel . panelIndex . toString ( ) : uuid . v4 ( ) ; return { ... panel , version , panelIndex , } ; } export function migratePanelsTo730 ( panels , version , useMargins , uiState ? ) { return panels . map ( ( panel ) => { if ( isPre61Panel ( panel ) ) { return migratePre61PanelToLatest ( panel , version , useMargins , uiState ) ; } if ( is61Panel ( panel ) ) { return migrate610PanelToLatest ( panel , version , useMargins , uiState ) ; } if ( is62Panel ( panel ) ) { return migrate620PanelToLatest ( panel , version , useMargins ) ; } if ( is63Panel ( panel ) ) { return migrate630PanelToLatest ( panel , version ) ; } if ( is640To720Panel ( panel ) ) { return migrate640To720PanelsToLatest ( panel , version ) ; } return panel as RawSavedDashboardPanel730ToLatest ; } ) ; }