Dashboards: Unify angular auto-migration code (#63915)

This commit is contained in:
Ryan McKinley
2023-03-27 08:11:45 -07:00
committed by GitHub
parent f948482386
commit 36e8ca7f13
9 changed files with 68 additions and 81 deletions

View File

@@ -16,7 +16,7 @@ import {
TimeZone,
UrlQueryValue,
} from '@grafana/data';
import { RefreshEvent, TimeRangeUpdatedEvent } from '@grafana/runtime';
import { RefreshEvent, TimeRangeUpdatedEvent, config } from '@grafana/runtime';
import { Dashboard } from '@grafana/schema';
import { DEFAULT_ANNOTATION_COLOR } from '@grafana/ui';
import { GRID_CELL_HEIGHT, GRID_CELL_VMARGIN, GRID_COLUMN_COUNT, REPEAT_DIR_VERTICAL } from 'app/core/constants';
@@ -41,7 +41,7 @@ import { getTimeSrv } from '../services/TimeSrv';
import { mergePanels, PanelMergeInfo } from '../utils/panelMerge';
import { DashboardMigrator } from './DashboardMigrator';
import { GridPos, PanelModel } from './PanelModel';
import { GridPos, PanelModel, autoMigrateAngular } from './PanelModel';
import { TimeModel } from './TimeModel';
import { deleteScopeVars, isOnTheSameGridRow } from './utils';
@@ -108,6 +108,7 @@ export class DashboardModel implements TimeModel {
// repeat process cycles
declare meta: DashboardMeta;
events: EventBusExtended;
private getVariablesFromState: GetVariables;
static nonPersistedProperties: { [str: string]: boolean } = {
events: true,
@@ -126,7 +127,18 @@ export class DashboardModel implements TimeModel {
lastRefresh: true,
};
constructor(data: Dashboard, meta?: DashboardMeta, private getVariablesFromState: GetVariables = getVariablesByKey) {
constructor(
data: Dashboard,
meta?: DashboardMeta,
options?: {
// By default this uses variables from redux state
getVariablesFromState?: GetVariables;
// Force the loader to migrate panels
autoMigrateOldPanels?: boolean;
}
) {
this.getVariablesFromState = options?.getVariablesFromState ?? getVariablesByKey;
this.events = new EventBusSrv();
this.id = data.id || null;
// UID is not there for newly created dashboards
@@ -162,6 +174,17 @@ export class DashboardModel implements TimeModel {
this.initMeta(meta);
this.updateSchema(data);
// Auto-migrate old angular panels
if (options?.autoMigrateOldPanels || !config.angularSupportEnabled || config.featureToggles.autoMigrateOldPanels) {
this.panels.forEach((p) => {
const newType = autoMigrateAngular[p.type];
if (!p.autoMigrateFrom && newType) {
p.autoMigrateFrom = p.type;
p.type = newType;
}
});
}
this.addBuiltInAnnotationQuery();
this.sortPanelsByGridPos();
this.panelsAffectedByVariableChange = null;