Compare commits
2 Commits
7fdab73ba0
...
slice/36-d
| Author | SHA1 | Date | |
|---|---|---|---|
| aac71eb129 | |||
| bdf87ffd67 |
@@ -24,12 +24,33 @@ function resolveChildNode(childId, ctx) {
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
const dashboards = source.generateDashboardsForGraph(childSource, {
|
||||
includeChildren: Boolean(msg.includeChildren ?? true),
|
||||
});
|
||||
|
||||
@@ -26,6 +26,29 @@ class nodeClass {
|
||||
|
||||
this._attachInputHandler();
|
||||
this._attachCloseHandler();
|
||||
this._attachLifecycleHook();
|
||||
}
|
||||
|
||||
// Subscribe to Node-RED's `flows:started` event to cache the deploy diff so
|
||||
// the child.register handler can decide whether *this* dashboardAPI's
|
||||
// subtree was affected. Predicate documented in Gitea issue #32 spike.
|
||||
_attachLifecycleHook() {
|
||||
if (!this.RED?.events?.on) return;
|
||||
this._flowsStartedListener = (payload) => {
|
||||
const diff = payload?.diff || null;
|
||||
this.source.lastFlowsStartedDiff = diff;
|
||||
this.source.lastFlowsStartedAt = Date.now();
|
||||
if (this.source?.logger?.debug) {
|
||||
const summary = diff
|
||||
? Object.fromEntries(
|
||||
['added', 'changed', 'removed', 'rewired', 'linked', 'flowChanged']
|
||||
.map((k) => [k, (diff[k] || []).length])
|
||||
)
|
||||
: null;
|
||||
this.source.logger.debug({ event: 'flows:started', type: payload?.type, diff: summary });
|
||||
}
|
||||
};
|
||||
this.RED.events.on('flows:started', this._flowsStartedListener);
|
||||
}
|
||||
|
||||
_buildConfig(uiConfig) {
|
||||
@@ -78,6 +101,10 @@ class nodeClass {
|
||||
|
||||
_attachCloseHandler() {
|
||||
this.node.on('close', (done) => {
|
||||
if (this._flowsStartedListener && this.RED?.events?.off) {
|
||||
this.RED.events.off('flows:started', this._flowsStartedListener);
|
||||
this._flowsStartedListener = null;
|
||||
}
|
||||
if (typeof done === 'function') done();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -168,6 +168,34 @@ class DashboardApi {
|
||||
return out;
|
||||
}
|
||||
|
||||
// Predicate from Gitea issue #32 spike (S1 findings). Given the diff payload
|
||||
// from Node-RED's flows:started event and a set of node ids that constitute
|
||||
// "my subtree", decides whether the subtree changed on this deploy.
|
||||
// `null` diff (first deploy / startup) → always regen (safe default).
|
||||
subtreeChanged(diff, subtreeIds) {
|
||||
if (!diff) return true;
|
||||
const mine = new Set(subtreeIds);
|
||||
for (const field of ['added', 'changed', 'removed', 'rewired']) {
|
||||
const arr = diff[field] || [];
|
||||
if (arr.some((id) => mine.has(id))) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Collect ids that constitute "this dashboardAPI + this child + its grandchildren"
|
||||
// for the diff predicate. Pulls grandchildren via the existing extractChildren walk.
|
||||
subtreeIdsFor(dashboardApiNodeId, childSource) {
|
||||
const ids = new Set();
|
||||
if (dashboardApiNodeId) ids.add(dashboardApiNodeId);
|
||||
const childId = childSource?.config?.general?.id;
|
||||
if (childId) ids.add(childId);
|
||||
for (const { childSource: gc } of this.extractChildren(childSource)) {
|
||||
const gcId = gc?.config?.general?.id;
|
||||
if (gcId) ids.add(gcId);
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
generateDashboardsForGraph(rootSource, { includeChildren = true } = {}) {
|
||||
if (!rootSource?.config) {
|
||||
this.logger.warn('generateDashboardsForGraph skipped: root source missing config');
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
'use strict';
|
||||
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const DashboardApi = require('../../src/specificClass.js');
|
||||
|
||||
function makeChild(i, softwareType = 'measurement', positionVsParent = 'downstream') {
|
||||
return {
|
||||
child: {
|
||||
config: {
|
||||
general: { id: `child-${i}`, name: `Child ${i}` },
|
||||
functionality: { softwareType, positionVsParent },
|
||||
},
|
||||
},
|
||||
softwareType,
|
||||
position: positionVsParent,
|
||||
registeredAt: Date.now(),
|
||||
};
|
||||
}
|
||||
|
||||
function makeRoot(children) {
|
||||
const map = new Map();
|
||||
for (const c of children) map.set(c.child.config.general.id, c);
|
||||
return {
|
||||
config: {
|
||||
general: { id: 'root-1', name: 'Root' },
|
||||
functionality: { softwareType: 'dashboardapi', positionVsParent: 'atequipment' },
|
||||
},
|
||||
childRegistrationUtils: { registeredChildren: map },
|
||||
};
|
||||
}
|
||||
|
||||
test('generateDashboardsForGraph composes 50 children in <500ms', () => {
|
||||
const api = new DashboardApi({});
|
||||
const children = Array.from({ length: 50 }, (_, i) => makeChild(i));
|
||||
const root = makeRoot(children);
|
||||
|
||||
const t0 = process.hrtime.bigint();
|
||||
const dashboards = api.generateDashboardsForGraph(root, { includeChildren: true });
|
||||
const t1 = process.hrtime.bigint();
|
||||
|
||||
const durationMs = Number(t1 - t0) / 1e6;
|
||||
assert.ok(durationMs < 500, `composition took ${durationMs.toFixed(1)}ms, expected <500ms`);
|
||||
assert.ok(dashboards.length >= 1, 'should produce at least the root dashboard');
|
||||
});
|
||||
|
||||
test('uids are unique across all generated dashboards (no collision risk)', () => {
|
||||
const api = new DashboardApi({});
|
||||
const children = Array.from({ length: 30 }, (_, i) => makeChild(i, 'measurement'));
|
||||
const root = makeRoot(children);
|
||||
const dashboards = api.generateDashboardsForGraph(root);
|
||||
const uids = dashboards.map((d) => d.uid);
|
||||
const unique = new Set(uids);
|
||||
assert.equal(unique.size, uids.length, `expected ${uids.length} unique uids, got ${unique.size}`);
|
||||
});
|
||||
|
||||
test('byte-identical composition under repeat (idempotency)', () => {
|
||||
const api = new DashboardApi({});
|
||||
const children = Array.from({ length: 5 }, (_, i) => makeChild(i));
|
||||
const root = makeRoot(children);
|
||||
const first = JSON.stringify(api.generateDashboardsForGraph(root).map((d) => d.dashboard));
|
||||
const second = JSON.stringify(api.generateDashboardsForGraph(root).map((d) => d.dashboard));
|
||||
assert.equal(first, second, 'two consecutive compositions should produce byte-identical JSON');
|
||||
});
|
||||
|
||||
test('root dashboard links to every child dashboard', () => {
|
||||
const api = new DashboardApi({});
|
||||
const children = Array.from({ length: 4 }, (_, i) => makeChild(i));
|
||||
const root = makeRoot(children);
|
||||
const dashboards = api.generateDashboardsForGraph(root);
|
||||
const rootDash = dashboards[0].dashboard;
|
||||
assert.ok(Array.isArray(rootDash.links), 'root dashboard should have links array');
|
||||
assert.equal(rootDash.links.length, 4, 'one link per registered child');
|
||||
});
|
||||
73
test/basic/slice36-diff-predicate.basic.test.js
Normal file
73
test/basic/slice36-diff-predicate.basic.test.js
Normal file
@@ -0,0 +1,73 @@
|
||||
'use strict';
|
||||
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const DashboardApi = require('../../src/specificClass.js');
|
||||
|
||||
test('subtreeChanged: null diff → always regen (safe default for cold start)', () => {
|
||||
const api = new DashboardApi({});
|
||||
assert.equal(api.subtreeChanged(null, new Set(['a', 'b'])), true);
|
||||
assert.equal(api.subtreeChanged(undefined, new Set(['a', 'b'])), true);
|
||||
});
|
||||
|
||||
test('subtreeChanged: empty diff arrays → no regen needed', () => {
|
||||
const api = new DashboardApi({});
|
||||
const diff = { added: [], changed: [], removed: [], rewired: [], linked: [], flowChanged: [] };
|
||||
assert.equal(api.subtreeChanged(diff, new Set(['a', 'b'])), false);
|
||||
});
|
||||
|
||||
test('subtreeChanged: id in added → regen', () => {
|
||||
const api = new DashboardApi({});
|
||||
const diff = { added: ['x', 'b'], changed: [], removed: [], rewired: [] };
|
||||
assert.equal(api.subtreeChanged(diff, new Set(['a', 'b'])), true);
|
||||
});
|
||||
|
||||
test('subtreeChanged: id in changed → regen', () => {
|
||||
const api = new DashboardApi({});
|
||||
const diff = { added: [], changed: ['a'], removed: [], rewired: [] };
|
||||
assert.equal(api.subtreeChanged(diff, new Set(['a', 'b'])), true);
|
||||
});
|
||||
|
||||
test('subtreeChanged: only unrelated ids → no regen', () => {
|
||||
const api = new DashboardApi({});
|
||||
const diff = { added: ['z'], changed: ['y'], removed: ['x'], rewired: ['w'] };
|
||||
assert.equal(api.subtreeChanged(diff, new Set(['a', 'b'])), false);
|
||||
});
|
||||
|
||||
test('subtreeChanged: tab id in diff but not in subtree → no regen', () => {
|
||||
// Tab id over-triggering avoidance: when an unrelated tab changes, its
|
||||
// tab id lands in changed/added but should not affect this dashboardAPI.
|
||||
const api = new DashboardApi({});
|
||||
const diff = { added: [], changed: ['unrelated_tab'], removed: [], rewired: [] };
|
||||
assert.equal(api.subtreeChanged(diff, new Set(['dashboardApiId', 'childA'])), false);
|
||||
});
|
||||
|
||||
test('subtreeIdsFor: includes dashboardAPI id + child id + grandchild ids', () => {
|
||||
const api = new DashboardApi({});
|
||||
const grandchild = {
|
||||
config: { general: { id: 'gc-1' }, functionality: { softwareType: 'measurement' } },
|
||||
};
|
||||
const grandchildEntry = { child: grandchild, position: 'downstream', softwareType: 'measurement' };
|
||||
const child = {
|
||||
config: { general: { id: 'child-1' }, functionality: { softwareType: 'pumpingStation' } },
|
||||
childRegistrationUtils: {
|
||||
registeredChildren: new Map([['gc-1', grandchildEntry]]),
|
||||
},
|
||||
};
|
||||
const ids = api.subtreeIdsFor('dApi-1', child);
|
||||
assert.equal(ids.has('dApi-1'), true);
|
||||
assert.equal(ids.has('child-1'), true);
|
||||
assert.equal(ids.has('gc-1'), true);
|
||||
assert.equal(ids.size, 3);
|
||||
});
|
||||
|
||||
test('subtreeIdsFor: handles child with no grandchildren', () => {
|
||||
const api = new DashboardApi({});
|
||||
const child = {
|
||||
config: { general: { id: 'child-1' }, functionality: { softwareType: 'measurement' } },
|
||||
};
|
||||
const ids = api.subtreeIdsFor('dApi-1', child);
|
||||
assert.equal(ids.size, 2);
|
||||
assert.ok(ids.has('dApi-1') && ids.has('child-1'));
|
||||
});
|
||||
Reference in New Issue
Block a user