Plugins: Extend panel menu with links from plugins (#63089)

* feat(plugins): introduce dashboard panel menu placement for adding menu items

* test: add test for getPanelMenu()

* added an unique identifier for each extension.

* added context to getPluginExtensions.

* wip

* Wip

* wiwip

* Wip

* feat: WWWIIIIPPPP 🧨

* Wip

* Renamed some of the types to align a bit better.

* added limit to how many extensions a plugin can register per placement.

* decreased number of items to 2

* will trim the lenght of titles to max 25 chars.

* wrapping configure function with error handling.

* added error handling for all scenarios.

* moved extension menu items to the bottom of the more sub menu.

* added tests for configuring the title.

* minor refactorings.

* changed so you need to specify the full path in package.json.

* wip

* removed unused type.

* big refactor to make things simpler and to centralize all configure error/validation handling.

* added missing import.

* fixed failing tests.

* fixed tests.

* revert(extensions): remove static extensions config in favour of registering via AppPlugin APIs

* removed the compose that didn't work for some reason.

* added tests just to verify that validation and error handling is tied together in configuration function.

* adding some more values to the context.

* draft validation.

* added missing tests for getPanelMenu.

* added more tests.

* refactor(extensions): move logic for validating extension link config to function

* Fixed ts errors.

* Update packages/grafana-data/src/types/app.ts

Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>

* Update packages/grafana-runtime/src/services/pluginExtensions/extensions.test.ts

Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>

* refactor(extensions): rename limiter -> pluginPlacementCount

* refactor(getpanelmenu): remove redundant continue statement

---------

Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>
Co-authored-by: Marcus Andersson <marcus.andersson@grafana.com>
This commit is contained in:
Jack Westbrook
2023-03-02 15:42:00 +01:00
committed by GitHub
parent 5bd2fac9c8
commit 8c8f584b41
36 changed files with 1382 additions and 713 deletions

View File

@@ -0,0 +1,79 @@
import { AppConfigureExtension, AppPluginExtensionLink } from '@grafana/data';
import { createErrorHandling } from './errorHandling';
describe('extension error handling', () => {
const pluginId = 'grafana-basic-app';
const errorHandler = createErrorHandling<AppPluginExtensionLink>({
pluginId: pluginId,
title: 'Go to page one',
logger: jest.fn(),
});
const context = {};
const extension: AppPluginExtensionLink = {
title: 'Go to page one',
description: 'Will navigate the user to page one',
path: `/a/${pluginId}/one`,
};
it('should return configured link if configure is successful', () => {
const configureWithErrorHandling = errorHandler(() => {
return {
title: 'This is a new title',
};
});
const configured = configureWithErrorHandling(extension, context);
expect(configured).toEqual({
title: 'This is a new title',
});
});
it('should return undefined if configure throws error', () => {
const configureWithErrorHandling = errorHandler(() => {
throw new Error();
});
const configured = configureWithErrorHandling(extension, context);
expect(configured).toBeUndefined();
});
it('should return undefined if configure is promise/async-based', () => {
const promisebased = (async () => {}) as AppConfigureExtension<AppPluginExtensionLink>;
const configureWithErrorHandling = errorHandler(promisebased);
const configured = configureWithErrorHandling(extension, context);
expect(configured).toBeUndefined();
});
it('should return undefined if configure is not a function', () => {
const objectbased = {} as AppConfigureExtension<AppPluginExtensionLink>;
const configureWithErrorHandling = errorHandler(objectbased);
const configured = configureWithErrorHandling(extension, context);
expect(configured).toBeUndefined();
});
it('should return undefined if configure returns other than an object', () => {
const returnString = (() => '') as AppConfigureExtension<AppPluginExtensionLink>;
const configureWithErrorHandling = errorHandler(returnString);
const configured = configureWithErrorHandling(extension, context);
expect(configured).toBeUndefined();
});
it('should return undefined if configure returns undefined', () => {
const returnUndefined = () => undefined;
const configureWithErrorHandling = errorHandler(returnUndefined);
const configured = configureWithErrorHandling(extension, context);
expect(configured).toBeUndefined();
});
});

View File

@@ -0,0 +1,43 @@
import { isFunction, isObject } from 'lodash';
import type { AppConfigureExtension } from '@grafana/data';
type Options = {
pluginId: string;
title: string;
logger: (msg: string, error?: unknown) => void;
};
export function createErrorHandling<T>(options: Options) {
const { pluginId, title, logger } = options;
return (configure: AppConfigureExtension<T>): AppConfigureExtension<T> => {
return function handleErrors(extension, context) {
try {
if (!isFunction(configure)) {
logger(`[Plugins] ${pluginId} provided invalid configuration function for extension '${title}'.`);
return;
}
const result = configure(extension, context);
if (result instanceof Promise) {
logger(
`[Plugins] ${pluginId} provided an unsupported async/promise-based configureation function for extension '${title}'.`
);
result.catch(() => {});
return;
}
if (!isObject(result) && typeof result !== 'undefined') {
logger(`[Plugins] ${pluginId} returned an inccorect object in configure function for extension '${title}'.`);
return;
}
return result;
} catch (error) {
logger(`[Plugins] ${pluginId} thow an error while configure extension '${title}'`, error);
return;
}
};
};
}

View File

@@ -0,0 +1,3 @@
export enum GrafanaExtensions {
DashboardPanelMenu = 'grafana/dashboard/panel/menu',
}

View File

@@ -1,100 +0,0 @@
import { AppPluginConfig, PluginExtensionTypes, PluginsExtensionLinkConfig } from '@grafana/runtime';
import { createPluginExtensionsRegistry } from './registry';
describe('Plugin registry', () => {
describe('createPluginExtensionsRegistry function', () => {
const registry = createPluginExtensionsRegistry({
'belugacdn-app': createConfig([
{
placement: 'plugins/belugacdn-app/menu',
title: 'The title',
type: PluginExtensionTypes.link,
description: 'Incidents are occurring!',
path: '/incidents/declare',
},
]),
'strava-app': createConfig([
{
placement: 'plugins/strava-app/menu',
title: 'The title',
type: PluginExtensionTypes.link,
description: 'Incidents are occurring!',
path: '/incidents/declare',
},
]),
'duplicate-links-app': createConfig([
{
placement: 'plugins/duplicate-links-app/menu',
title: 'The title',
type: PluginExtensionTypes.link,
description: 'Incidents are occurring!',
path: '/incidents/declare',
},
{
placement: 'plugins/duplicate-links-app/menu',
title: 'The title',
type: PluginExtensionTypes.link,
description: 'Incidents are occurring!',
path: '/incidents/declare2',
},
]),
'no-extensions-app': createConfig(undefined),
});
it('should configure a registry link', () => {
const [link] = registry['plugins/belugacdn-app/menu'];
expect(link).toEqual({
title: 'The title',
type: 'link',
description: 'Incidents are occurring!',
path: '/a/belugacdn-app/incidents/declare',
key: 539074708,
});
});
it('should configure all registry targets', () => {
const numberOfTargets = Object.keys(registry).length;
expect(numberOfTargets).toBe(3);
});
it('should configure registry targets from multiple plugins', () => {
const [pluginALink] = registry['plugins/belugacdn-app/menu'];
const [pluginBLink] = registry['plugins/strava-app/menu'];
expect(pluginALink).toEqual({
title: 'The title',
type: 'link',
description: 'Incidents are occurring!',
path: '/a/belugacdn-app/incidents/declare',
key: 539074708,
});
expect(pluginBLink).toEqual({
title: 'The title',
type: 'link',
description: 'Incidents are occurring!',
path: '/a/strava-app/incidents/declare',
key: -1637066384,
});
});
it('should configure multiple links for a single target', () => {
const links = registry['plugins/duplicate-links-app/menu'];
expect(links.length).toBe(2);
});
});
});
function createConfig(extensions?: PluginsExtensionLinkConfig[]): AppPluginConfig {
return {
id: 'myorg-basic-app',
preload: false,
path: '',
version: '',
extensions,
};
}

View File

@@ -1,54 +0,0 @@
import {
AppPluginConfig,
PluginExtensionTypes,
PluginsExtensionLinkConfig,
PluginsExtensionRegistry,
PluginsExtensionLink,
} from '@grafana/runtime';
export function createPluginExtensionsRegistry(apps: Record<string, AppPluginConfig> = {}): PluginsExtensionRegistry {
const registry: PluginsExtensionRegistry = {};
for (const [pluginId, config] of Object.entries(apps)) {
const extensions = config.extensions;
if (!Array.isArray(extensions)) {
continue;
}
for (const extension of extensions) {
const placement = extension.placement;
const item = createRegistryItem(pluginId, extension);
if (!Array.isArray(registry[placement])) {
registry[placement] = [item];
continue;
}
registry[placement].push(item);
continue;
}
}
for (const key of Object.keys(registry)) {
Object.freeze(registry[key]);
}
return Object.freeze(registry);
}
function createRegistryItem(pluginId: string, extension: PluginsExtensionLinkConfig): PluginsExtensionLink {
const path = `/a/${pluginId}${extension.path}`;
return Object.freeze({
type: PluginExtensionTypes.link,
title: extension.title,
description: extension.description,
path: path,
key: hashKey(`${extension.title}${path}`),
});
}
function hashKey(key: string): number {
return Array.from(key).reduce((s, c) => (Math.imul(31, s) + c.charCodeAt(0)) | 0, 0);
}

View File

@@ -0,0 +1,326 @@
import { PluginExtensionTypes } from '@grafana/data';
import { createPluginExtensionRegistry } from './registryFactory';
const validateLink = jest.fn((configure, extension, context) => configure?.(extension, context));
const errorHandler = jest.fn((configure, extension, context) => configure?.(extension, context));
jest.mock('./errorHandling', () => ({
...jest.requireActual('./errorHandling'),
createErrorHandling: jest.fn(() => {
return jest.fn((configure) => {
return jest.fn((extension, context) => errorHandler(configure, extension, context));
});
}),
}));
jest.mock('./validateLink', () => ({
...jest.requireActual('./validateLink'),
createLinkValidator: jest.fn(() => {
return jest.fn((configure) => {
return jest.fn((extension, context) => validateLink(configure, extension, context));
});
}),
}));
describe('Creating extensions registry', () => {
beforeEach(() => {
validateLink.mockClear();
errorHandler.mockClear();
});
it('should register an extension', () => {
const registry = createPluginExtensionRegistry([
{
pluginId: 'belugacdn-app',
linkExtensions: [
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
},
],
},
]);
const numberOfPlacements = Object.keys(registry).length;
const extensions = registry['grafana/dashboard/panel/menu'];
expect(numberOfPlacements).toBe(1);
expect(extensions).toEqual([
{
configure: undefined,
extension: {
title: 'Open incident',
type: PluginExtensionTypes.link,
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
key: -68154691,
},
},
]);
});
it('should register extensions from one plugin with multiple placements', () => {
const registry = createPluginExtensionRegistry([
{
pluginId: 'belugacdn-app',
linkExtensions: [
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
},
{
placement: 'plugins/grafana-slo-app/slo-breached',
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
},
],
},
]);
const numberOfPlacements = Object.keys(registry).length;
const panelExtensions = registry['grafana/dashboard/panel/menu'];
const sloExtensions = registry['plugins/grafana-slo-app/slo-breached'];
expect(numberOfPlacements).toBe(2);
expect(panelExtensions).toEqual([
{
configure: undefined,
extension: {
title: 'Open incident',
type: PluginExtensionTypes.link,
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
key: -68154691,
},
},
]);
expect(sloExtensions).toEqual([
{
configure: undefined,
extension: {
title: 'Open incident',
type: PluginExtensionTypes.link,
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
key: -1638987831,
},
},
]);
});
it('should register extensions from multiple plugins with multiple placements', () => {
const registry = createPluginExtensionRegistry([
{
pluginId: 'belugacdn-app',
linkExtensions: [
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
},
{
placement: 'plugins/grafana-slo-app/slo-breached',
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
},
],
},
{
pluginId: 'grafana-monitoring-app',
linkExtensions: [
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open Incident',
description: 'You can create an incident from this context',
path: '/a/grafana-monitoring-app/incidents/declare',
},
],
},
]);
const numberOfPlacements = Object.keys(registry).length;
const panelExtensions = registry['grafana/dashboard/panel/menu'];
const sloExtensions = registry['plugins/grafana-slo-app/slo-breached'];
expect(numberOfPlacements).toBe(2);
expect(panelExtensions).toEqual([
{
configure: undefined,
extension: {
title: 'Open incident',
type: PluginExtensionTypes.link,
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
key: -68154691,
},
},
{
configure: undefined,
extension: {
title: 'Open Incident',
type: PluginExtensionTypes.link,
description: 'You can create an incident from this context',
path: '/a/grafana-monitoring-app/incidents/declare',
key: -540306829,
},
},
]);
expect(sloExtensions).toEqual([
{
configure: undefined,
extension: {
title: 'Open incident',
type: PluginExtensionTypes.link,
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
key: -1638987831,
},
},
]);
});
it('should register maximum 2 extensions per plugin and placement', () => {
const registry = createPluginExtensionRegistry([
{
pluginId: 'belugacdn-app',
linkExtensions: [
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
},
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident 2',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
},
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident 3',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
},
],
},
]);
const numberOfPlacements = Object.keys(registry).length;
const panelExtensions = registry['grafana/dashboard/panel/menu'];
expect(numberOfPlacements).toBe(1);
expect(panelExtensions).toEqual([
{
configure: undefined,
extension: {
title: 'Open incident',
type: PluginExtensionTypes.link,
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
key: -68154691,
},
},
{
configure: undefined,
extension: {
title: 'Open incident 2',
type: PluginExtensionTypes.link,
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
key: -1072147569,
},
},
]);
});
it('should not register extensions with invalid path configured', () => {
const registry = createPluginExtensionRegistry([
{
pluginId: 'belugacdn-app',
linkExtensions: [
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/incidents/declare',
},
],
},
]);
const numberOfPlacements = Object.keys(registry).length;
expect(numberOfPlacements).toBe(0);
});
it('should wrap configure function with link extension validator', () => {
const registry = createPluginExtensionRegistry([
{
pluginId: 'belugacdn-app',
linkExtensions: [
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
configure: () => ({}),
},
],
},
]);
const extensions = registry['grafana/dashboard/panel/menu'];
const [extension] = extensions;
const context = {};
const configurable = {
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
};
extension?.configure?.(context);
expect(validateLink).toBeCalledWith(expect.any(Function), configurable, context);
});
it('should wrap configure function with extension error handling', () => {
const registry = createPluginExtensionRegistry([
{
pluginId: 'belugacdn-app',
linkExtensions: [
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
configure: () => ({}),
},
],
},
]);
const extensions = registry['grafana/dashboard/panel/menu'];
const [extension] = extensions;
const context = {};
const configurable = {
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
};
extension?.configure?.(context);
expect(errorHandler).toBeCalledWith(expect.any(Function), configurable, context);
});
});

View File

@@ -0,0 +1,132 @@
import {
AppConfigureExtension,
AppPluginExtensionLink,
AppPluginExtensionLinkConfig,
PluginExtensionLink,
PluginExtensionTypes,
} from '@grafana/data';
import type {
PluginExtensionRegistry,
PluginExtensionRegistryItem,
RegistryConfigureExtension,
} from '@grafana/runtime';
import { PluginPreloadResult } from '../pluginPreloader';
import { createErrorHandling } from './errorHandling';
import { createLinkValidator, isValidLinkPath } from './validateLink';
export function createPluginExtensionRegistry(preloadResults: PluginPreloadResult[]): PluginExtensionRegistry {
const registry: PluginExtensionRegistry = {};
for (const result of preloadResults) {
const pluginPlacementCount: Record<string, number> = {};
const { pluginId, linkExtensions, error } = result;
if (!Array.isArray(linkExtensions) || error) {
continue;
}
for (const extension of linkExtensions) {
const placement = extension.placement;
pluginPlacementCount[placement] = (pluginPlacementCount[placement] ?? 0) + 1;
const item = createRegistryLink(pluginId, extension);
// If there was an issue initialising the plugin, skip adding its extensions to the registry
// or if the plugin already have placed 2 items at the extension point.
if (!item || pluginPlacementCount[placement] > 2) {
continue;
}
if (!Array.isArray(registry[placement])) {
registry[placement] = [item];
continue;
}
registry[placement].push(item);
}
}
for (const item of Object.keys(registry)) {
Object.freeze(registry[item]);
}
return Object.freeze(registry);
}
function createRegistryLink(
pluginId: string,
config: AppPluginExtensionLinkConfig
): PluginExtensionRegistryItem<PluginExtensionLink> | undefined {
if (!isValidLinkPath(pluginId, config.path)) {
return undefined;
}
const id = `${pluginId}${config.placement}${config.title}`;
const extension = Object.freeze({
type: PluginExtensionTypes.link,
title: config.title,
description: config.description,
key: hashKey(id),
path: config.path,
});
return Object.freeze({
extension: extension,
configure: createLinkConfigure(pluginId, config, extension),
});
}
function hashKey(key: string): number {
return Array.from(key).reduce((s, c) => (Math.imul(31, s) + c.charCodeAt(0)) | 0, 0);
}
function createLinkConfigure(
pluginId: string,
config: AppPluginExtensionLinkConfig,
extension: PluginExtensionLink
): RegistryConfigureExtension<PluginExtensionLink> | undefined {
if (!config.configure) {
return undefined;
}
const options = {
pluginId: pluginId,
title: config.title,
logger: console.warn,
};
const mapper = mapToRegistryType(extension);
const validator = createLinkValidator(options);
const errorHandler = createErrorHandling<AppPluginExtensionLink>(options);
return mapper(validator(errorHandler(config.configure)));
}
function mapToRegistryType(
extension: PluginExtensionLink
): (configure: AppConfigureExtension<AppPluginExtensionLink>) => RegistryConfigureExtension<PluginExtensionLink> {
const configurable: AppPluginExtensionLink = {
title: extension.title,
description: extension.description,
path: extension.path,
};
return (configure) => {
return function mapper(context: object): PluginExtensionLink | undefined {
const configured = configure(configurable, context);
if (!configured) {
return undefined;
}
return {
...extension,
title: configured.title ?? extension.title,
description: configured.description ?? extension.description,
path: configured.path ?? extension.path,
};
};
};
}

View File

@@ -0,0 +1,52 @@
import type { AppPluginExtensionLink } from '@grafana/data';
import { createLinkValidator } from './validateLink';
describe('extension link validator', () => {
const pluginId = 'grafana-basic-app';
const validator = createLinkValidator({
pluginId,
title: 'Link to something',
logger: jest.fn(),
});
const context = {};
const extension: AppPluginExtensionLink = {
title: 'Go to page one',
description: 'Will navigate the user to page one',
path: `/a/${pluginId}/one`,
};
it('should return link configuration if path is valid', () => {
const configureWithValidation = validator(() => {
return {
path: `/a/${pluginId}/other`,
};
});
const configured = configureWithValidation(extension, context);
expect(configured).toEqual({
path: `/a/${pluginId}/other`,
});
});
it('should return undefined if path is invalid', () => {
const configureWithValidation = validator(() => {
return {
path: `/other`,
};
});
const configured = configureWithValidation(extension, context);
expect(configured).toBeUndefined();
});
it('should return undefined if undefined is returned from inner configure', () => {
const configureWithValidation = validator(() => {
return undefined;
});
const configured = configureWithValidation(extension, context);
expect(configured).toBeUndefined();
});
});

View File

@@ -0,0 +1,30 @@
import type { AppConfigureExtension, AppPluginExtensionLink } from '@grafana/data';
type Options = {
pluginId: string;
title: string;
logger: (msg: string, error?: unknown) => void;
};
export function createLinkValidator(options: Options) {
const { pluginId, title, logger } = options;
return (configure: AppConfigureExtension<AppPluginExtensionLink>): AppConfigureExtension<AppPluginExtensionLink> => {
return function validateLink(link, context) {
const configured = configure(link, context);
if (!isValidLinkPath(pluginId, configured?.path)) {
logger(
`[Plugins] Disabled extension '${title}' for '${pluginId}' beause configure didn't return a path with the correct prefix: '${`/a/${pluginId}/`}'`
);
return undefined;
}
return configured;
};
};
}
export function isValidLinkPath(pluginId: string, path?: string): boolean {
return path?.startsWith(`/a/${pluginId}/`) === true;
}

View File

@@ -1,17 +1,27 @@
import { AppPluginConfig } from '@grafana/runtime';
import { AppPluginExtensionLinkConfig } from '@grafana/data';
import type { AppPluginConfig } from '@grafana/runtime';
import { importPluginModule } from './plugin_loader';
import * as pluginLoader from './plugin_loader';
export async function preloadPlugins(apps: Record<string, AppPluginConfig> = {}): Promise<void> {
export type PluginPreloadResult = {
pluginId: string;
linkExtensions: AppPluginExtensionLinkConfig[];
error?: unknown;
};
export async function preloadPlugins(apps: Record<string, AppPluginConfig> = {}): Promise<PluginPreloadResult[]> {
const pluginsToPreload = Object.values(apps).filter((app) => app.preload);
await Promise.all(pluginsToPreload.map(preloadPlugin));
return Promise.all(pluginsToPreload.map(preload));
}
async function preloadPlugin(plugin: AppPluginConfig): Promise<void> {
const { path, version } = plugin;
async function preload(config: AppPluginConfig): Promise<PluginPreloadResult> {
const { path, version, id: pluginId } = config;
try {
await importPluginModule(path, version);
} catch (error: unknown) {
console.error(`Failed to load plugin: ${path} (version: ${version})`, error);
const { plugin } = await pluginLoader.importPluginModule(path, version);
const { linkExtensions = [] } = plugin;
return { pluginId, linkExtensions };
} catch (error) {
console.error(`[Plugins] Failed to preload plugin: ${path} (version: ${version})`, error);
return { pluginId, linkExtensions: [], error };
}
}