From 92138de21bd653ccb9f5c23650ddcdba05044daa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Fri, 1 May 2020 14:14:52 +0200 Subject: [PATCH] PanelEditor: Fixes issue changing panel type from graph to stat and thresholds not taking affect after going back to dashboard (#24144) * PanelEditor: Fixes issue with panel type change and query result reuse * removed unused imports * removed unused imports (cherry picked from commit a00636b2eca23cc655856c99110b24b3bb9fc487) --- .../PanelEditor/state/actions.test.ts | 1 + .../components/PanelEditor/state/actions.ts | 4 ++++ .../dashboard/dashgrid/PanelChrome.tsx | 20 ++++++++++--------- .../dashboard/state/DashboardModel.ts | 16 ++++----------- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/public/app/features/dashboard/components/PanelEditor/state/actions.test.ts b/public/app/features/dashboard/components/PanelEditor/state/actions.test.ts index a640c4f14f3..0a17952c866 100644 --- a/public/app/features/dashboard/components/PanelEditor/state/actions.test.ts +++ b/public/app/features/dashboard/components/PanelEditor/state/actions.test.ts @@ -88,6 +88,7 @@ describe('panelEditor actions', () => { expect(dispatchedActions.length).toBe(3); expect(dispatchedActions[0].type).toBe(panelModelAndPluginReady.type); + expect(sourcePanel.plugin).toEqual(panel.plugin); }); it('should discard changes when shouldDiscardChanges is true', async () => { diff --git a/public/app/features/dashboard/components/PanelEditor/state/actions.ts b/public/app/features/dashboard/components/PanelEditor/state/actions.ts index b2edde42681..78541e2260d 100644 --- a/public/app/features/dashboard/components/PanelEditor/state/actions.ts +++ b/public/app/features/dashboard/components/PanelEditor/state/actions.ts @@ -47,6 +47,10 @@ export function panelEditorCleanUp(): ThunkResult { sourcePanel.restoreModel(modifiedSaveModel); + // Loaded plugin is not included in the persisted properties + // So is not handled by restoreModel + sourcePanel.plugin = panel.plugin; + if (panelTypeChanged) { dispatch(panelModelAndPluginReady({ panelId: sourcePanel.id, plugin: panel.plugin! })); } diff --git a/public/app/features/dashboard/dashgrid/PanelChrome.tsx b/public/app/features/dashboard/dashgrid/PanelChrome.tsx index b8ca99b21d7..47602ccfe1b 100644 --- a/public/app/features/dashboard/dashgrid/PanelChrome.tsx +++ b/public/app/features/dashboard/dashgrid/PanelChrome.tsx @@ -73,6 +73,7 @@ export class PanelChrome extends PureComponent { panel.events.on(PanelEvents.refresh, this.onRefresh); panel.events.on(PanelEvents.render, this.onRender); + dashboard.panelInitialized(this.props.panel); // Move snapshot data into the query response @@ -99,6 +100,15 @@ export class PanelChrome extends PureComponent { this.setState({ isFirstLoad: false }); } } + + if (!this.querySubscription) { + this.querySubscription = panel + .getQueryRunner() + .getData() + .subscribe({ + next: data => this.onDataUpdate(data), + }); + } } componentWillUnmount() { @@ -184,15 +194,7 @@ export class PanelChrome extends PureComponent { return; } - const queryRunner = panel.getQueryRunner(); - - if (!this.querySubscription) { - this.querySubscription = queryRunner.getData().subscribe({ - next: data => this.onDataUpdate(data), - }); - } - - queryRunner.run({ + panel.getQueryRunner().run({ datasource: panel.datasource, queries: panel.targets, panelId: panel.id, diff --git a/public/app/features/dashboard/state/DashboardModel.ts b/public/app/features/dashboard/state/DashboardModel.ts index 5051cdfbbee..e462b17e805 100644 --- a/public/app/features/dashboard/state/DashboardModel.ts +++ b/public/app/features/dashboard/state/DashboardModel.ts @@ -314,18 +314,10 @@ export class DashboardModel { panelInitialized(panel: PanelModel) { panel.initialized(); - if (this.panelInEdit === panel) { - if (this.panelInEdit.getQueryRunner().getLastResult()) { - return; - } else { - // refresh if panel is in edit mode and there is no last result - panel.refresh(); - } - } else { - // refresh new panels unless we are in fullscreen / edit mode - if (!this.otherPanelInFullscreen(panel)) { - panel.refresh(); - } + const lastResult = panel.getQueryRunner().getLastResult(); + + if (!this.otherPanelInFullscreen(panel) && !lastResult) { + panel.refresh(); } }