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
This commit is contained in:
Ben Sully 2023-05-12 13:12:00 +01:00 committed by GitHub
parent 09d7d25c9f
commit e7a67e749c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 1 deletions

View File

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

View File

@ -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 }));

View File

@ -342,5 +342,6 @@ function createExtensionContext(panel: PanelModel, dashboard: DashboardModel): P
},
targets: panel.targets,
scopedVars: panel.scopedVars,
data: panel.getQueryRunner().getLastResult(),
};
}