DashboardScene: Fixes issue referring to library panel in dashboard data source (#87125)

* DashboardScene: Fixes issue using a library panel as source for dashboard data source

* Added test
This commit is contained in:
Torkel Ödegaard 2024-05-01 14:30:54 +02:00 committed by GitHub
parent f806b9749d
commit 4034a26972
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 6 deletions

View File

@ -53,7 +53,7 @@ export class DashboardDatasourceBehaviour extends SceneObjectBase<DashboardDatas
const sourcePanelQueryRunner = getQueryRunnerFor(panel);
if (!(sourcePanelQueryRunner instanceof SceneQueryRunner)) {
if (!sourcePanelQueryRunner) {
if (!(panel.parent instanceof LibraryVizPanel)) {
throw new Error('Could not find SceneQueryRunner for panel');
} else {

View File

@ -0,0 +1,26 @@
import { VizPanel } from '@grafana/scenes';
import { LibraryVizPanel } from '../scene/LibraryVizPanel';
import { PanelModelCompatibilityWrapper } from './PanelModelCompatibilityWrapper';
describe('PanelModelCompatibilityWrapper', () => {
it('Can get legacy id', () => {
const vizPanel = new VizPanel({ pluginId: 'test', title: 'test', description: 'test', key: 'panel-24' });
const panelModel = new PanelModelCompatibilityWrapper(vizPanel);
expect(panelModel.id).toBe(24);
});
it('Can get legacy id for lib panel', () => {
const libPanel = new LibraryVizPanel({
uid: 'a',
name: 'aa',
title: 'a',
panelKey: 'panel-24',
panel: new VizPanel({ pluginId: 'test', title: 'test', description: 'test', key: 'panel-24' }),
});
const panelModel = new PanelModelCompatibilityWrapper(libPanel.state.panel!);
expect(panelModel.id).toBe(24);
});
});

View File

@ -2,17 +2,13 @@ import { PanelModel } from '@grafana/data';
import { SceneDataTransformer, VizPanel } from '@grafana/scenes';
import { DataSourceRef, DataTransformerConfig } from '@grafana/schema';
import { LibraryVizPanel } from '../scene/LibraryVizPanel';
import { getPanelIdForVizPanel, getQueryRunnerFor } from './utils';
export class PanelModelCompatibilityWrapper implements PanelModel {
constructor(private _vizPanel: VizPanel) {}
public get id() {
const id = getPanelIdForVizPanel(
this._vizPanel.parent instanceof LibraryVizPanel ? this._vizPanel.parent : this._vizPanel
);
const id = getPanelIdForVizPanel(this._vizPanel);
if (isNaN(id)) {
console.error('VizPanel key could not be translated to a legacy numeric panel id', this._vizPanel);