VizPanelManager: Load default/last used data source if no data source specified for a panel (#84999)

This commit is contained in:
Dominik Prokop 2024-03-22 21:08:40 +01:00 committed by GitHub
parent 566cee7d6b
commit b6cea0d7fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 147 additions and 10 deletions

View File

@ -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) => {

View File

@ -108,15 +108,37 @@ export class VizPanelManager extends SceneObjectBase<VizPanelManagerState> {
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({

View File

@ -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,