[v11.0.x] DashboardScene: Fixes issue referring to library panel in dashboard data source (#87173)

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

(cherry picked from commit 4034a26972)

Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
This commit is contained in:
grafana-delivery-bot[bot]
2024-05-01 15:25:41 +02:00
committed by GitHub
co-authored by Torkel Ödegaard
parent a8cd6d9025
commit 7c1cc61fb8
3 changed files with 28 additions and 6 deletions
@@ -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 {
@@ -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);
});
});
@@ -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);