From e7a67e749c1c9efefaf20dd343e91d4788104473 Mon Sep 17 00:00:00 2001 From: Ben Sully Date: Fri, 12 May 2023 13:12:00 +0100 Subject: [PATCH] Plugins: Pass panel data in plugin extension context (#67509) * Plugins: Pass panel data in plugin extension context Similar to https://github.com/grafana/grafana/pull/65861, this passes the panel's data as part of the context object used when configuring extension links. This is useful if the plugin wants to conditionally show the link depending on the presence or absence of certain features in the data. For example in the ML plugin we only want to offer Outlier Detection links for a query if the query returned more than 3 series. * Update getPanelMenu extension test to include data object --- .../src/types/pluginExtensions.ts | 2 ++ .../dashboard/utils/getPanelMenu.test.ts | 35 ++++++++++++++++++- .../features/dashboard/utils/getPanelMenu.ts | 1 + 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/grafana-data/src/types/pluginExtensions.ts b/packages/grafana-data/src/types/pluginExtensions.ts index 1bb6eedce20..1ed7cb1d866 100644 --- a/packages/grafana-data/src/types/pluginExtensions.ts +++ b/packages/grafana-data/src/types/pluginExtensions.ts @@ -1,6 +1,7 @@ import { DataQuery } from '@grafana/schema'; import { ScopedVars } from './ScopedVars'; +import { PanelData } from './panel'; import { RawTimeRange, TimeZone } from './time'; // Plugin Extensions types @@ -78,6 +79,7 @@ export type PluginExtensionPanelContext = { dashboard: Dashboard; targets: DataQuery[]; scopedVars?: ScopedVars; + data?: PanelData; }; type Dashboard = { diff --git a/public/app/features/dashboard/utils/getPanelMenu.test.ts b/public/app/features/dashboard/utils/getPanelMenu.test.ts index ab80ab55047..d4a16dcbfe2 100644 --- a/public/app/features/dashboard/utils/getPanelMenu.test.ts +++ b/public/app/features/dashboard/utils/getPanelMenu.test.ts @@ -1,4 +1,13 @@ -import { PanelMenuItem, PluginExtensionPanelContext, PluginExtensionTypes } from '@grafana/data'; +import { + dateTime, + FieldType, + LoadingState, + PanelData, + PanelMenuItem, + PluginExtensionPanelContext, + PluginExtensionTypes, + toDataFrame, +} from '@grafana/data'; import { getPluginExtensions } from '@grafana/runtime'; import config from 'app/core/config'; import * as actions from 'app/features/explore/state/main'; @@ -189,6 +198,26 @@ describe('getPanelMenu()', () => { }); it('should pass context with correct values when configuring extension', () => { + const data: PanelData = { + series: [ + toDataFrame({ + fields: [ + { name: 'time', type: FieldType.time }, + { name: 'score', type: FieldType.number }, + ], + }), + ], + timeRange: { + from: dateTime(), + to: dateTime(), + raw: { + from: 'now', + to: 'now-1h', + }, + }, + state: LoadingState.Done, + }; + const panel = new PanelModel({ type: 'timeseries', id: 1, @@ -207,6 +236,9 @@ describe('getPanelMenu()', () => { value: 'a', }, }, + queryRunner: { + getLastResult: jest.fn(() => data), + }, }); const dashboard = createDashboardModelFixture({ @@ -250,6 +282,7 @@ describe('getPanelMenu()', () => { value: 'a', }, }, + data, }; expect(getPluginExtensions).toBeCalledWith(expect.objectContaining({ context })); diff --git a/public/app/features/dashboard/utils/getPanelMenu.ts b/public/app/features/dashboard/utils/getPanelMenu.ts index 3a13722b51a..20a3d20bd0d 100644 --- a/public/app/features/dashboard/utils/getPanelMenu.ts +++ b/public/app/features/dashboard/utils/getPanelMenu.ts @@ -342,5 +342,6 @@ function createExtensionContext(panel: PanelModel, dashboard: DashboardModel): P }, targets: panel.targets, scopedVars: panel.scopedVars, + data: panel.getQueryRunner().getLastResult(), }; }