diff --git a/public/app/features/dashboard-scene/panel-edit/VizPanelManager.test.tsx b/public/app/features/dashboard-scene/panel-edit/VizPanelManager.test.tsx index bd656881769..38390a8d4cc 100644 --- a/public/app/features/dashboard-scene/panel-edit/VizPanelManager.test.tsx +++ b/public/app/features/dashboard-scene/panel-edit/VizPanelManager.test.tsx @@ -179,7 +179,14 @@ describe('VizPanelManager', () => { title: 'Panel A', key: 'panel-1', pluginId: 'table', - $data: new SceneQueryRunner({ key: 'data-query-runner', queries: [{ refId: 'A' }] }), + $data: new SceneQueryRunner({ + key: 'data-query-runner', + datasource: { + type: 'grafana-testdata-datasource', + uid: 'gdev-testdata', + }, + queries: [{ refId: 'A' }], + }), options: undefined, fieldConfig: { defaults: { @@ -210,7 +217,14 @@ describe('VizPanelManager', () => { title: 'Panel A', key: 'panel-1', pluginId: 'table', - $data: new SceneQueryRunner({ key: 'data-query-runner', queries: [{ refId: 'A' }] }), + $data: new SceneQueryRunner({ + key: 'data-query-runner', + datasource: { + type: 'grafana-testdata-datasource', + uid: 'gdev-testdata', + }, + queries: [{ refId: 'A' }], + }), options: { customOption: 'A', }, @@ -719,6 +733,23 @@ describe('VizPanelManager', () => { }); }); }); + + it('should load last used data source if no data source specified for a panel', async () => { + store.exists.mockReturnValue(true); + store.getObject.mockReturnValue({ + dashboardUid: 'ffbe00e2-803c-4d49-adb7-41aad336234f', + datasourceUid: 'gdev-testdata', + }); + const { scene, panel } = setupTest('panel-5'); + scene.setState({ editPanel: buildPanelEditScene(panel) }); + + const vizPanelManager = scene.state.editPanel!.state.vizManager; + vizPanelManager.activate(); + await Promise.resolve(); + + expect(vizPanelManager.state.datasource).toEqual(ds1Mock); + expect(vizPanelManager.state.dsSettings).toEqual(instance1SettingsMock); + }); }); const setupTest = (panelId: string) => { diff --git a/public/app/features/dashboard-scene/panel-edit/VizPanelManager.tsx b/public/app/features/dashboard-scene/panel-edit/VizPanelManager.tsx index 22aa2773e5e..47357312c9f 100644 --- a/public/app/features/dashboard-scene/panel-edit/VizPanelManager.tsx +++ b/public/app/features/dashboard-scene/panel-edit/VizPanelManager.tsx @@ -108,15 +108,37 @@ export class VizPanelManager extends SceneObjectBase { let datasourceToLoad = this.queryRunner.state.datasource; - if (!datasourceToLoad) { - return; - } - try { - // TODO: Handle default/last used datasource selection for new panel - // Ref: PanelEditorQueries / componentDidMount - const datasource = await getDataSourceSrv().get(datasourceToLoad); - const dsSettings = getDataSourceSrv().getInstanceSettings(datasourceToLoad); + let datasource: DataSourceApi | undefined; + let dsSettings: DataSourceInstanceSettings | undefined; + + if (!datasourceToLoad) { + const dashboardScene = getDashboardSceneFor(this); + const dashboardUid = dashboardScene.state.uid ?? ''; + const lastUsedDatasource = getLastUsedDatasourceFromStorage(dashboardUid!); + + // do we have a last used datasource for this dashboard + if (lastUsedDatasource?.datasourceUid !== null) { + // get datasource from dashbopard uid + dsSettings = getDataSourceSrv().getInstanceSettings({ uid: lastUsedDatasource?.datasourceUid }); + if (dsSettings) { + datasource = await getDataSourceSrv().get({ + uid: lastUsedDatasource?.datasourceUid, + type: dsSettings.type, + }); + + this.queryRunner.setState({ + datasource: { + uid: lastUsedDatasource?.datasourceUid, + type: dsSettings.type, + }, + }); + } + } + } else { + datasource = await getDataSourceSrv().get(datasourceToLoad); + dsSettings = getDataSourceSrv().getInstanceSettings(datasourceToLoad); + } if (datasource && dsSettings) { this.setState({ diff --git a/public/app/features/dashboard-scene/panel-edit/testfiles/testDashboard.ts b/public/app/features/dashboard-scene/panel-edit/testfiles/testDashboard.ts index e5ab4038444..f524e20f99c 100644 --- a/public/app/features/dashboard-scene/panel-edit/testfiles/testDashboard.ts +++ b/public/app/features/dashboard-scene/panel-edit/testfiles/testDashboard.ts @@ -327,6 +327,89 @@ export const panelWithDashboardQueryAndTransformations = { ], type: 'table', }; + +export const panelWithNoDataSource = { + fieldConfig: { + defaults: { + color: { + mode: 'palette-classic', + }, + custom: { + axisBorderShow: false, + axisCenteredZero: false, + axisColorMode: 'text', + axisLabel: '', + axisPlacement: 'auto', + barAlignment: 0, + drawStyle: 'line', + fillOpacity: 0, + gradientMode: 'none', + hideFrom: { + legend: false, + tooltip: false, + viz: false, + }, + insertNulls: false, + lineInterpolation: 'linear', + lineWidth: 1, + pointSize: 5, + scaleDistribution: { + type: 'linear', + }, + showPoints: 'auto', + spanNulls: false, + stacking: { + group: 'A', + mode: 'none', + }, + thresholdsStyle: { + mode: 'off', + }, + }, + mappings: [], + thresholds: { + mode: 'absolute', + steps: [ + { + color: 'green', + value: null, + }, + { + color: 'red', + value: 80, + }, + ], + }, + }, + overrides: [], + }, + gridPos: { + h: 8, + w: 12, + x: 0, + y: 0, + }, + id: 5, + options: { + legend: { + calcs: [], + displayMode: 'list', + placement: 'bottom', + showLegend: true, + }, + tooltip: { + mode: 'single', + sort: 'none', + }, + }, + targets: [ + { + refId: 'A', + }, + ], + title: 'Panel with no data source', + type: 'timeseries', +}; export const testDashboard = { annotations: { list: [ @@ -355,6 +438,7 @@ export const testDashboard = { panelWithTransformations, panelWithDashboardQuery, panelWithDashboardQueryAndTransformations, + panelWithNoDataSource, ], refresh: '', schemaVersion: 39,