Add plugins set to routeObject and use in analytics if 1

Signed-off-by: Eric Peterson <i.am@eric.pe>
This commit is contained in:
Eric Peterson
2023-05-28 19:53:30 +02:00
parent 0b24ce3270
commit 1ff191d593
3 changed files with 34 additions and 1 deletions
@@ -21,6 +21,7 @@ import {
AnalyticsContext,
RouteRef,
AnalyticsEventAttributes,
BackstagePlugin,
} from '@backstage/core-plugin-api';
import { BackstageRouteObject } from './types';
@@ -53,8 +54,10 @@ const getExtensionContext = (
// If there is a single route ref, return it.
// todo: get routeRef of rendered gathered mount point(?)
let routeRef: RouteRef | undefined;
let plugin: BackstagePlugin | undefined;
if (routeObject.routeRefs.size === 1) {
routeRef = routeObject.routeRefs.values().next().value;
plugin = routeObject.plugins.values().next().value;
}
const params = Object.entries(
@@ -68,7 +71,7 @@ const getExtensionContext = (
return {
extension: 'App',
pluginId: routeObject.plugin?.getId() || 'root',
pluginId: plugin?.getId() || 'root',
...(routeRef ? { routeRef: (routeRef as { id?: string }).id } : {}),
params,
};
@@ -33,6 +33,7 @@ export const MATCH_ALL_ROUTE: BackstageRouteObject = {
path: '*',
element: 'match-all', // These elements aren't used, so we add in a bit of debug information
routeRefs: new Set(),
plugins: new Set(),
};
function stringifyNode(node: ReactNode): string {
@@ -45,6 +46,16 @@ function stringifyNode(node: ReactNode): string {
return String(anyNode);
}
const pluginSet = (
plugin: BackstagePlugin | undefined,
): Set<BackstagePlugin> => {
const set = new Set<BackstagePlugin>();
if (plugin) {
set.add(plugin);
}
return set;
};
interface RoutingV2CollectorContext {
routeRef?: RouteRef;
gatherPath?: string;
@@ -132,6 +143,7 @@ export const routingV2Collector = createCollector(
routeRefs: new Set<RouteRef>(),
caseSensitive: Boolean(node.props?.caseSensitive),
children: [MATCH_ALL_ROUTE],
plugins: new Set<BackstagePlugin>(),
plugin: undefined,
};
parentChildren.push(newObj);
@@ -162,6 +174,7 @@ export const routingV2Collector = createCollector(
routeRefs: new Set([routeRef]),
caseSensitive: Boolean(node.props?.caseSensitive),
children: [MATCH_ALL_ROUTE],
plugins: pluginSet(plugin),
plugin,
};
parentChildren.push(newObj);
@@ -187,6 +200,15 @@ export const routingV2Collector = createCollector(
}
ctx?.obj?.routeRefs.add(mountPoint);
const mountPointPlugin = getComponentData<BackstagePlugin>(
node,
'core.plugin',
);
if (mountPointPlugin) {
ctx?.obj?.plugins.add(mountPointPlugin);
}
acc.paths.set(mountPoint, ctx.gatherPath);
acc.parents.set(mountPoint, ctx?.gatherRouteRef);
@@ -306,6 +328,12 @@ export const routingV1Collector = createCollector(
element: 'mounted',
routeRefs: new Set([routeRef]),
children: [MATCH_ALL_ROUTE],
plugins: pluginSet(
getComponentData<BackstagePlugin>(
node.props.element,
'core.plugin',
),
),
plugin: getComponentData<BackstagePlugin>(
node.props.element,
'core.plugin',
@@ -336,6 +364,7 @@ export const routingV1Collector = createCollector(
element: 'gathered',
routeRefs: new Set(),
children: [MATCH_ALL_ROUTE],
plugins: pluginSet(ctx?.obj?.plugin),
plugin: ctx?.obj?.plugin,
};
parentChildren.push(currentObj);
@@ -54,6 +54,7 @@ export interface BackstageRouteObject {
element: React.ReactNode;
path: string;
routeRefs: Set<RouteRef>;
plugins: Set<BackstagePlugin>;
plugin?: BackstagePlugin;
}