Compare commits
2 Commits
a76f22281e
...
slice/41-m
| Author | SHA1 | Date | |
|---|---|---|---|
| 3c8427ed7a | |||
| 8964b0b638 |
@@ -122,7 +122,12 @@
|
||||
}
|
||||
],
|
||||
"title": "Scaling",
|
||||
"type": "stat"
|
||||
"type": "stat",
|
||||
"meta": {
|
||||
"emittedFields": [
|
||||
"scaling"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"datasource": {
|
||||
@@ -174,7 +179,12 @@
|
||||
}
|
||||
],
|
||||
"title": "Abs Dist Peak",
|
||||
"type": "stat"
|
||||
"type": "stat",
|
||||
"meta": {
|
||||
"emittedFields": [
|
||||
"absDistFromPeak"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"datasource": {
|
||||
@@ -227,7 +237,12 @@
|
||||
}
|
||||
],
|
||||
"title": "Rel Dist Peak",
|
||||
"type": "stat"
|
||||
"type": "stat",
|
||||
"meta": {
|
||||
"emittedFields": [
|
||||
"relDistFromPeak"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"gridPos": {
|
||||
@@ -253,7 +268,58 @@
|
||||
"fillOpacity": 10
|
||||
}
|
||||
},
|
||||
"overrides": []
|
||||
"overrides": [
|
||||
{
|
||||
"matcher": {
|
||||
"id": "byRegexp",
|
||||
"options": ".+\\.min$"
|
||||
},
|
||||
"properties": [
|
||||
{
|
||||
"id": "custom.lineStyle",
|
||||
"value": {
|
||||
"fill": "dash",
|
||||
"dash": [
|
||||
10,
|
||||
10
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "color",
|
||||
"value": {
|
||||
"mode": "fixed",
|
||||
"fixedColor": "orange"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"matcher": {
|
||||
"id": "byRegexp",
|
||||
"options": ".+\\.max$"
|
||||
},
|
||||
"properties": [
|
||||
{
|
||||
"id": "custom.lineStyle",
|
||||
"value": {
|
||||
"fill": "dash",
|
||||
"dash": [
|
||||
10,
|
||||
10
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "color",
|
||||
"value": {
|
||||
"mode": "fixed",
|
||||
"fixedColor": "red"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"gridPos": {
|
||||
"h": 8,
|
||||
@@ -278,7 +344,13 @@
|
||||
}
|
||||
],
|
||||
"title": "Total Flow",
|
||||
"type": "timeseries"
|
||||
"type": "timeseries",
|
||||
"meta": {
|
||||
"emittedFields": [
|
||||
"flow.total",
|
||||
"flow.group"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"datasource": {
|
||||
@@ -293,7 +365,58 @@
|
||||
"fillOpacity": 10
|
||||
}
|
||||
},
|
||||
"overrides": []
|
||||
"overrides": [
|
||||
{
|
||||
"matcher": {
|
||||
"id": "byRegexp",
|
||||
"options": ".+\\.min$"
|
||||
},
|
||||
"properties": [
|
||||
{
|
||||
"id": "custom.lineStyle",
|
||||
"value": {
|
||||
"fill": "dash",
|
||||
"dash": [
|
||||
10,
|
||||
10
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "color",
|
||||
"value": {
|
||||
"mode": "fixed",
|
||||
"fixedColor": "orange"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"matcher": {
|
||||
"id": "byRegexp",
|
||||
"options": ".+\\.max$"
|
||||
},
|
||||
"properties": [
|
||||
{
|
||||
"id": "custom.lineStyle",
|
||||
"value": {
|
||||
"fill": "dash",
|
||||
"dash": [
|
||||
10,
|
||||
10
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "color",
|
||||
"value": {
|
||||
"mode": "fixed",
|
||||
"fixedColor": "red"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"gridPos": {
|
||||
"h": 8,
|
||||
@@ -318,7 +441,13 @@
|
||||
}
|
||||
],
|
||||
"title": "Total Power",
|
||||
"type": "timeseries"
|
||||
"type": "timeseries",
|
||||
"meta": {
|
||||
"emittedFields": [
|
||||
"power.total",
|
||||
"power.group"
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"schemaVersion": 39,
|
||||
|
||||
@@ -22,35 +22,9 @@ function resolveChildNode(childId, ctx) {
|
||||
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.
|
||||
//
|
||||
// Diff-skip behavior (PRD F-1, S1 spike #32): if the latest flows:started
|
||||
// payload's `diff` indicates that NEITHER the dashboardAPI itself NOR this
|
||||
// child NOR its grandchildren changed, skip composition and log no-diff. The
|
||||
// first call after startup (no cached diff yet) regenerates unconditionally.
|
||||
function registerChild(source, msg, ctx) {
|
||||
const childSource = resolveChildSource(msg.payload, ctx);
|
||||
if (!childSource?.config) {
|
||||
throw new Error('Missing or invalid child node');
|
||||
}
|
||||
|
||||
const subtreeIds = source.subtreeIdsFor(ctx.node?.id, childSource);
|
||||
const changed = source.subtreeChanged(source.lastFlowsStartedDiff, subtreeIds);
|
||||
if (!changed) {
|
||||
if (source.logger?.info) {
|
||||
source.logger.info({
|
||||
event: 'regen-skipped',
|
||||
outcome: 'no-diff',
|
||||
trigger: 'child.register',
|
||||
dashboardApiId: ctx.node?.id,
|
||||
childId: childSource?.config?.general?.id,
|
||||
subtreeSize: subtreeIds.size,
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Shared emit path used by both child.register (auto, deploy-driven) and
|
||||
// regenerate-dashboard (manual). `trigger` distinguishes the two for logs.
|
||||
function emitDashboardsFor(source, childSource, ctx, msg, trigger) {
|
||||
const dashboards = source.generateDashboardsForGraph(childSource, {
|
||||
includeChildren: Boolean(msg.includeChildren ?? true),
|
||||
});
|
||||
@@ -77,9 +51,73 @@ function registerChild(source, msg, ctx) {
|
||||
softwareType: dash.softwareType,
|
||||
uid: dash.uid,
|
||||
title: dash.title,
|
||||
trigger,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (source.logger?.info) {
|
||||
source.logger.info({
|
||||
event: 'regen-emitted',
|
||||
trigger,
|
||||
dashboardApiId: ctx.node?.id,
|
||||
childId: childSource?.config?.general?.id,
|
||||
dashboardCount: dashboards.length,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { registerChild };
|
||||
// On child.register: build the dashboard graph (root + direct children) and
|
||||
// emit one Grafana upsert HTTP request per dashboard on Port 0.
|
||||
//
|
||||
// Diff-skip behavior (PRD F-1, S1 spike #32): if the latest flows:started
|
||||
// payload's `diff` indicates that NEITHER the dashboardAPI itself NOR this
|
||||
// child NOR its grandchildren changed, skip composition and log no-diff. The
|
||||
// first call after startup (no cached diff yet) regenerates unconditionally.
|
||||
function registerChild(source, msg, ctx) {
|
||||
const childSource = resolveChildSource(msg.payload, ctx);
|
||||
if (!childSource?.config) {
|
||||
throw new Error('Missing or invalid child node');
|
||||
}
|
||||
|
||||
// Cache the child source for later manual regen (#41).
|
||||
source.recordChild?.(childSource);
|
||||
|
||||
const subtreeIds = source.subtreeIdsFor(ctx.node?.id, childSource);
|
||||
const changed = source.subtreeChanged(source.lastFlowsStartedDiff, subtreeIds);
|
||||
if (!changed) {
|
||||
if (source.logger?.info) {
|
||||
source.logger.info({
|
||||
event: 'regen-skipped',
|
||||
outcome: 'no-diff',
|
||||
trigger: 'child.register',
|
||||
dashboardApiId: ctx.node?.id,
|
||||
childId: childSource?.config?.general?.id,
|
||||
subtreeSize: subtreeIds.size,
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
emitDashboardsFor(source, childSource, ctx, msg, 'child.register');
|
||||
}
|
||||
|
||||
// On regenerate-dashboard: re-emit dashboards for every cached child source,
|
||||
// bypassing the diff predicate. Useful as an operator escape hatch when
|
||||
// auto-regen missed an edge case or when the operator just wants to refresh.
|
||||
function regenerateDashboard(source, msg, ctx) {
|
||||
const cached = source.cachedChildSources?.() || [];
|
||||
if (source.logger?.info) {
|
||||
source.logger.info({
|
||||
event: 'manual-regen-requested',
|
||||
trigger: 'manual',
|
||||
dashboardApiId: ctx.node?.id,
|
||||
cachedChildCount: cached.length,
|
||||
});
|
||||
}
|
||||
for (const childSource of cached) {
|
||||
emitDashboardsFor(source, childSource, ctx, msg, 'manual');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { registerChild, regenerateDashboard };
|
||||
|
||||
@@ -13,4 +13,10 @@ module.exports = [
|
||||
payloadSchema: { type: 'any' },
|
||||
handler: handlers.registerChild,
|
||||
},
|
||||
{
|
||||
topic: 'regenerate-dashboard',
|
||||
aliases: ['regen'],
|
||||
payloadSchema: { type: 'any' },
|
||||
handler: handlers.regenerateDashboard,
|
||||
},
|
||||
];
|
||||
|
||||
@@ -75,6 +75,20 @@ class DashboardApi {
|
||||
this.config.general.logging.logLevel,
|
||||
this.config.general.name
|
||||
);
|
||||
|
||||
// Light state cache for manual regen (#41). Stores the latest child
|
||||
// source object per child id so `regenerate-dashboard` can re-emit
|
||||
// dashboards without waiting for children to re-register.
|
||||
this._lastChildSources = new Map();
|
||||
}
|
||||
|
||||
recordChild(childSource) {
|
||||
const id = childSource?.config?.general?.id;
|
||||
if (id) this._lastChildSources.set(id, childSource);
|
||||
}
|
||||
|
||||
cachedChildSources() {
|
||||
return Array.from(this._lastChildSources.values());
|
||||
}
|
||||
|
||||
_templatesDir() {
|
||||
|
||||
69
test/basic/slice40-mgc-template.basic.test.js
Normal file
69
test/basic/slice40-mgc-template.basic.test.js
Normal file
@@ -0,0 +1,69 @@
|
||||
'use strict';
|
||||
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const DashboardApi = require('../../src/specificClass.js');
|
||||
|
||||
test('MGC template panels are all group-level (no per-pump fields)', () => {
|
||||
const api = new DashboardApi({});
|
||||
const dash = api.loadTemplate('machineGroup');
|
||||
const PER_PUMP = new Set(['ctrl', 'state', 'runtime', 'pressure.upstream', 'pressure.downstream', 'temperature']);
|
||||
for (const panel of dash.panels || []) {
|
||||
if (panel.type === 'row') continue;
|
||||
const fields = panel?.meta?.emittedFields || [];
|
||||
for (const f of fields) {
|
||||
assert.ok(!PER_PUMP.has(f),
|
||||
`MGC panel "${panel.title}" emits ${f}, which belongs to rotatingMachine (per-pump). Move to children.`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('MGC group panels are annotated (mode, scaling, abs/rel peak, totals)', () => {
|
||||
const api = new DashboardApi({});
|
||||
const dash = api.loadTemplate('machineGroup');
|
||||
const non = dash.panels.filter((p) => p.type !== 'row');
|
||||
const annotated = non.filter((p) => p?.meta?.emittedFields);
|
||||
assert.equal(annotated.length, non.length, 'every non-row MGC panel annotated');
|
||||
});
|
||||
|
||||
test('MGC timeseries panels carry dashed-bounds overrides for .min/.max', () => {
|
||||
const api = new DashboardApi({});
|
||||
const dash = api.loadTemplate('machineGroup');
|
||||
const ts = dash.panels.filter((p) => p.type === 'timeseries');
|
||||
for (const panel of ts) {
|
||||
const ov = panel?.fieldConfig?.overrides || [];
|
||||
const hasMin = ov.some((o) => o.matcher?.id === 'byRegexp' && /\.min\$/.test(o.matcher?.options || ''));
|
||||
const hasMax = ov.some((o) => o.matcher?.id === 'byRegexp' && /\.max\$/.test(o.matcher?.options || ''));
|
||||
assert.ok(hasMin && hasMax, `MGC ts panel "${panel.title}" missing .min/.max dashed override`);
|
||||
}
|
||||
});
|
||||
|
||||
test('MGC composer dedups parent panels covered by pump children', () => {
|
||||
// If a rotatingMachine child claims to emit `flow.total` (it shouldn't, but
|
||||
// suppose), the parent MGC's "Total Flow" panel would be removed. Verify
|
||||
// the composer applies the same dedup rule to MGC parents.
|
||||
const api = new DashboardApi({});
|
||||
function makeChildSrc(id) {
|
||||
return { config: { general: { id }, functionality: { softwareType: 'machine', positionVsParent: 'downstream' } } };
|
||||
}
|
||||
const child = makeChildSrc('pump-1');
|
||||
const root = {
|
||||
config: { general: { id: 'mgc-1', name: 'MGC' }, functionality: { softwareType: 'machineGroupControl', positionVsParent: 'atequipment' } },
|
||||
childRegistrationUtils: { registeredChildren: new Map([['pump-1', { child, position: 'downstream', softwareType: 'machine' }]]) },
|
||||
};
|
||||
const origLoad = api.loadTemplate.bind(api);
|
||||
api.loadTemplate = function (t) {
|
||||
const dash = origLoad(t);
|
||||
if (t === 'machine') {
|
||||
// Make the pump's template falsely claim it emits flow.total/flow.group
|
||||
const firstPanel = dash.panels.find((p) => p.type !== 'row');
|
||||
if (firstPanel) (firstPanel.meta ||= {}).emittedFields = ['flow.total', 'flow.group'];
|
||||
}
|
||||
return dash;
|
||||
};
|
||||
const results = api.generateDashboardsForGraph(root);
|
||||
const mgcDash = results[0].dashboard;
|
||||
const totalFlowPanel = mgcDash.panels.find((p) => p.title === 'Total Flow');
|
||||
assert.ok(!totalFlowPanel, 'MGC Total Flow panel should be removed when child claims flow.total/flow.group');
|
||||
});
|
||||
75
test/basic/slice41-manual-regen.basic.test.js
Normal file
75
test/basic/slice41-manual-regen.basic.test.js
Normal file
@@ -0,0 +1,75 @@
|
||||
'use strict';
|
||||
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const DashboardApi = require('../../src/specificClass.js');
|
||||
const handlers = require('../../src/commands/handlers.js');
|
||||
|
||||
function makeCtx(sends, nodeId = 'dApi-1') {
|
||||
return {
|
||||
node: { id: nodeId },
|
||||
RED: { nodes: { getNode: () => null } },
|
||||
send: (m) => sends.push(m),
|
||||
logger: null,
|
||||
};
|
||||
}
|
||||
|
||||
function makeChildPayload(id, softwareType = 'measurement') {
|
||||
return {
|
||||
config: {
|
||||
general: { id, name: id },
|
||||
functionality: { softwareType, positionVsParent: 'downstream' },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
test('recordChild caches child source by id; subsequent ones replace by id', () => {
|
||||
const api = new DashboardApi({});
|
||||
api.recordChild(makeChildPayload('a'));
|
||||
api.recordChild(makeChildPayload('b'));
|
||||
api.recordChild(makeChildPayload('a')); // replace
|
||||
assert.equal(api.cachedChildSources().length, 2);
|
||||
});
|
||||
|
||||
test('regenerate-dashboard with no cached children is a no-op (no msgs emitted)', () => {
|
||||
const api = new DashboardApi({});
|
||||
const sends = [];
|
||||
handlers.regenerateDashboard(api, { topic: 'regenerate-dashboard', payload: {} }, makeCtx(sends));
|
||||
assert.equal(sends.length, 0);
|
||||
});
|
||||
|
||||
test('regenerate-dashboard re-emits for each cached child, bypassing diff', () => {
|
||||
const api = new DashboardApi({});
|
||||
// Pre-populate cache as if two children had registered.
|
||||
api.recordChild(makeChildPayload('m-1'));
|
||||
api.recordChild(makeChildPayload('m-2'));
|
||||
|
||||
// Set a diff that says nothing changed — registerChild would skip, but
|
||||
// regenerateDashboard should ignore the predicate.
|
||||
api.lastFlowsStartedDiff = { added: [], changed: [], removed: [], rewired: [] };
|
||||
|
||||
const sends = [];
|
||||
handlers.regenerateDashboard(api, { topic: 'regenerate-dashboard', payload: {} }, makeCtx(sends));
|
||||
// Each child yields at least one dashboard message (the root for the child's view).
|
||||
assert.ok(sends.length >= 2, `expected ≥2 emitted msgs, got ${sends.length}`);
|
||||
// Every emitted msg carries trigger: 'manual' in meta.
|
||||
for (const m of sends) assert.equal(m.meta?.trigger, 'manual');
|
||||
});
|
||||
|
||||
test('child.register stamps trigger: child.register in emitted msg meta', () => {
|
||||
const api = new DashboardApi({});
|
||||
api.lastFlowsStartedDiff = null; // cold-start → always regen
|
||||
const sends = [];
|
||||
handlers.registerChild(api, { topic: 'child.register', payload: makeChildPayload('m-3') }, makeCtx(sends));
|
||||
assert.ok(sends.length >= 1);
|
||||
for (const m of sends) assert.equal(m.meta?.trigger, 'child.register');
|
||||
});
|
||||
|
||||
test('command registry exposes regenerate-dashboard with regen alias', () => {
|
||||
const registry = require('../../src/commands/index.js');
|
||||
const entry = registry.find((e) => e.topic === 'regenerate-dashboard');
|
||||
assert.ok(entry, 'topic registered');
|
||||
assert.deepEqual(entry.aliases, ['regen']);
|
||||
assert.equal(typeof entry.handler, 'function');
|
||||
});
|
||||
Reference in New Issue
Block a user