P6: convert dashboardAPI to platform infrastructure

Refactor of dashboardAPI to use BaseNodeAdapter + commandRegistry + statusBadge.
dashboardAPI follows the platform refactor plan in .claude/refactor/MODULE_SPLIT.md.
Tests stay green; CONTRACT.md generated; legacy aliases preserved.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
znetsixe
2026-05-10 22:23:45 +02:00
parent 869ba4fca5
commit 2874608375
5 changed files with 191 additions and 91 deletions

64
src/commands/handlers.js Normal file
View File

@@ -0,0 +1,64 @@
'use strict';
// Resolve a child's source object from a registration payload.
// Payload may be: a string (node id) | { source: {...} } | { config: {...} }.
function resolveChildSource(payload, ctx) {
if (payload?.source?.config) return payload.source;
if (payload?.config) return { config: payload.config };
if (typeof payload === 'string') {
const childNode = resolveChildNode(payload, ctx);
return childNode?.source || null;
}
return null;
}
function resolveChildNode(childId, ctx) {
const runtimeNode = ctx.RED?.nodes?.getNode?.(childId);
if (runtimeNode?.source?.config) return runtimeNode;
const flowNode = ctx.node?._flow?.getNode?.(childId);
if (flowNode?.source?.config) return flowNode;
return runtimeNode || flowNode || null;
}
// On child.register: build the dashboard graph (root + direct children) and
// emit one Grafana upsert HTTP request per dashboard on Port 0.
function registerChild(source, msg, ctx) {
const childSource = resolveChildSource(msg.payload, ctx);
if (!childSource?.config) {
throw new Error('Missing or invalid child node');
}
const dashboards = source.generateDashboardsForGraph(childSource, {
includeChildren: Boolean(msg.includeChildren ?? true),
});
const url = source.grafanaUpsertUrl();
const headers = { Accept: 'application/json', 'Content-Type': 'application/json' };
const token = source.config?.grafanaConnector?.bearerToken;
if (token) headers.Authorization = `Bearer ${token}`;
for (const dash of dashboards) {
ctx.send({
...msg,
topic: 'create',
url,
method: 'POST',
headers,
payload: source.buildUpsertRequest({
dashboard: dash.dashboard,
folderId: 0,
overwrite: true,
}),
meta: {
nodeId: dash.nodeId,
softwareType: dash.softwareType,
uid: dash.uid,
title: dash.title,
},
});
}
}
module.exports = { registerChild };