Skip to content

Commit

Permalink
Graph Panel: Add feature toggle that will allow automatic migration t…
Browse files Browse the repository at this point in the history
…o timeseries panel (grafana#50631)
  • Loading branch information
ryantxu authored Jun 11, 2022
1 parent b6f97e8 commit ae8dd73
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/grafana-data/src/types/featureToggles.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface FeatureToggles {
traceToMetrics?: boolean;
prometheusStreamingJSONParser?: boolean;
validateDashboardsOnSave?: boolean;
autoMigrateGraphPanels?: boolean;
prometheusWideSeries?: boolean;
canvasPanelNesting?: boolean;
cloudMonitoringExperimentalUI?: boolean;
Expand Down
6 changes: 6 additions & 0 deletions pkg/services/featuremgmt/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ var (
State: FeatureStateAlpha,
RequiresRestart: true,
},
{
Name: "autoMigrateGraphPanels",
Description: "Replace the angular graph panel with timeseries",
State: FeatureStateBeta,
FrontendOnly: true,
},
{
Name: "prometheusWideSeries",
Description: "Enable wide series responses in the Prometheus datasource",
Expand Down
4 changes: 4 additions & 0 deletions pkg/services/featuremgmt/toggles_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ const (
// Validate dashboard JSON POSTed to api/dashboards/db
FlagValidateDashboardsOnSave = "validateDashboardsOnSave"

// FlagAutoMigrateGraphPanels
// Replace the angular graph panel with timeseries
FlagAutoMigrateGraphPanels = "autoMigrateGraphPanels"

// FlagPrometheusWideSeries
// Enable wide series responses in the Prometheus datasource
FlagPrometheusWideSeries = "prometheusWideSeries"
Expand Down
40 changes: 35 additions & 5 deletions public/app/features/dashboard/state/PanelModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ export class PanelModel implements DataConfigSource, IPanelModel {

libraryPanel?: { uid: undefined; name: string } | PanelModelLibraryPanel;

autoMigrateFrom?: string;

// non persisted
isViewing = false;
isEditing = false;
Expand Down Expand Up @@ -222,6 +224,12 @@ export class PanelModel implements DataConfigSource, IPanelModel {
(this as any)[property] = model[property];
}

// Special 'graph' migration logic
if (this.type === 'graph' && config?.featureToggles?.autoMigrateGraphPanels) {
this.autoMigrateFrom = this.type;
this.type = 'timeseries';
}

// defaults
defaultsDeep(this, cloneDeep(defaults));

Expand Down Expand Up @@ -368,6 +376,18 @@ export class PanelModel implements DataConfigSource, IPanelModel {
this.plugin = plugin;
const version = getPluginVersion(plugin);

if (this.autoMigrateFrom) {
const wasAngular = this.autoMigrateFrom === 'graph';
this.callPanelTypeChangeHandler(
plugin,
this.autoMigrateFrom,
this.getOptionsToRemember(), // old options
wasAngular
);

delete this.autoMigrateFrom;
}

if (plugin.onPanelMigration) {
if (version !== this.pluginVersion) {
this.options = plugin.onPanelMigration(this);
Expand Down Expand Up @@ -401,6 +421,19 @@ export class PanelModel implements DataConfigSource, IPanelModel {
};
}

// Let panel plugins inspect options from previous panel and keep any that it can use
private callPanelTypeChangeHandler(
newPlugin: PanelPlugin,
oldPluginId: string,
oldOptions: any,
wasAngular: boolean
) {
if (newPlugin.onPanelTypeChanged) {
const prevOptions = wasAngular ? { angular: oldOptions } : oldOptions.options;
Object.assign(this.options, newPlugin.onPanelTypeChanged(this, oldPluginId, prevOptions, this.fieldConfig));
}
}

changePlugin(newPlugin: PanelPlugin) {
const pluginId = newPlugin.meta.id;
const oldOptions: any = this.getOptionsToRemember();
Expand All @@ -415,11 +448,8 @@ export class PanelModel implements DataConfigSource, IPanelModel {
this.clearPropertiesBeforePluginChange();
this.restorePanelOptions(pluginId);

// Let panel plugins inspect options from previous panel and keep any that it can use
if (newPlugin.onPanelTypeChanged) {
const prevOptions = wasAngular ? { angular: oldOptions } : oldOptions.options;
Object.assign(this.options, newPlugin.onPanelTypeChanged(this, oldPluginId, prevOptions, prevFieldConfig));
}
// Potentially modify current options
this.callPanelTypeChangeHandler(newPlugin, oldPluginId, oldOptions, wasAngular);

// switch
this.type = pluginId;
Expand Down

0 comments on commit ae8dd73

Please sign in to comment.