Phase 1 wave 1: domain + nodered + stats infra (additive)

Adds platform infrastructure used by the upcoming refactor of
nodeClass / specificClass across all 12 nodes:

- src/domain/UnitPolicy.js     — extracted from rotatingMachine/MGC
- src/domain/ChildRouter.js    — declarative event routing on top of childRegistrationUtils
- src/domain/LatestWinsGate.js — extracted from MGC dispatch gate
- src/domain/HealthStatus.js   — standardised {level, flags, message, source}
- src/nodered/statusBadge.js   — compose / error / idle / byState / text helpers
- src/stats/index.js           — mean / stdDev / median / mad / lerp

All additive — no existing exports change shape.
56 unit tests pass under node:test.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
znetsixe
2026-05-10 18:27:29 +02:00
parent 9a998191cd
commit 47faf94048
12 changed files with 1374 additions and 0 deletions

184
src/domain/ChildRouter.js Normal file
View File

@@ -0,0 +1,184 @@
/**
* ChildRouter — declarative parent-side child registration & event routing.
*
* Replaces the per-node `registerChild` switch + manual
* `child.measurements.emitter.on(...)` wiring repeated in pumpingStation,
* rotatingMachine and machineGroupControl.
*
* See CONTRACTS.md §5. Built on top of `childRegistrationUtils`, which
* already canonicalises softwareType (e.g. rotatingmachine → machine).
*/
// Same alias map as childRegistrationUtils. Duplicated rather than imported
// because we need to canonicalise inputs to onRegister/onMeasurement/onPrediction
// at *declaration* time (before any child has registered), so that a domain
// can write `onRegister('rotatingmachine', ...)` or `onRegister('machine', ...)`
// interchangeably and have the dispatch match.
const SOFTWARE_TYPE_ALIASES = {
rotatingmachine: 'machine',
machinegroupcontrol: 'machinegroup',
};
function canonicalType(rawType) {
const t = String(rawType || '').toLowerCase();
return SOFTWARE_TYPE_ALIASES[t] || t;
}
class ChildRouter {
constructor(domain) {
this.domain = domain;
this.logger = domain?.logger || null;
// Subscription tables, keyed by canonical softwareType.
this._registerSubs = new Map(); // softwareType -> Array<fn>
this._measurementSubs = new Map(); // softwareType -> Array<{filter, fn}>
this._predictionSubs = new Map(); // softwareType -> Array<{filter, fn}>
// Track every emitter listener we attach so tearDown can remove them.
this._attached = [];
}
// ── declaration API ────────────────────────────────────────────────
onRegister(softwareType, fn) {
if (typeof fn !== 'function') {
throw new TypeError('ChildRouter.onRegister: fn must be a function');
}
const key = canonicalType(softwareType);
if (!this._registerSubs.has(key)) this._registerSubs.set(key, []);
this._registerSubs.get(key).push(fn);
return this;
}
onMeasurement(softwareType, filter, fn) {
return this._addEventSub(this._measurementSubs, softwareType, filter, fn, 'onMeasurement');
}
onPrediction(softwareType, filter, fn) {
return this._addEventSub(this._predictionSubs, softwareType, filter, fn, 'onPrediction');
}
_addEventSub(table, softwareType, filter, fn, label) {
if (typeof filter === 'function' && fn === undefined) {
// Allow `onMeasurement(type, fn)` shorthand — no filter.
fn = filter;
filter = {};
}
if (typeof fn !== 'function') {
throw new TypeError(`ChildRouter.${label}: fn must be a function`);
}
const key = canonicalType(softwareType);
if (!table.has(key)) table.set(key, []);
table.get(key).push({ filter: filter || {}, fn });
return this;
}
// ── dispatch ──────────────────────────────────────────────────────
/**
* Called by the domain's registerChild(). Runs onRegister handlers, then
* attaches measurement/prediction listeners on the child's emitter.
*/
dispatchRegister(child, softwareType) {
const key = canonicalType(softwareType);
const regHandlers = this._registerSubs.get(key) || [];
for (const fn of regHandlers) {
try { fn.call(this.domain, child, key); }
catch (err) { this._logHandlerError('onRegister', key, err); }
}
const emitter = child?.measurements?.emitter;
if (!emitter || typeof emitter.on !== 'function') return;
this._attachVariantListeners(child, key, emitter, 'measured', this._measurementSubs);
this._attachVariantListeners(child, key, emitter, 'predicted', this._predictionSubs);
}
_attachVariantListeners(child, key, emitter, variant, table) {
const subs = table.get(key) || [];
for (const { filter, fn } of subs) {
// Build the set of (type, position) tuples this sub matches. If a filter
// omits one or both of {type, position}, we can't pre-enumerate the event
// names — fall back to a wildcard listener via `emit`-time matching.
if (filter.type && filter.position) {
const eventName = `${filter.type}.${variant}.${String(filter.position).toLowerCase()}`;
this._attach(emitter, eventName, (data) => this._invoke(fn, data, child, variant));
continue;
}
// Wildcard: subscribe to a generic catch-all by patching emitter.emit.
// EventEmitter has no built-in wildcard — install a one-off proxy listener
// that intercepts every emit on this emitter and filters by name.
const proxyKey = `__childRouter_proxy_${variant}__`;
if (!emitter[proxyKey]) {
const origEmit = emitter.emit.bind(emitter);
const proxies = [];
emitter[proxyKey] = proxies;
emitter.emit = (eventName, ...args) => {
const parts = String(eventName).split('.');
if (parts.length === 3 && parts[1] === variant) {
for (const p of proxies) p({ type: parts[0], position: parts[2], args });
}
return origEmit(eventName, ...args);
};
// Track the proxy install for tearDown to undo.
this._attached.push({ emitter, kind: 'proxy', variant, original: origEmit, proxyKey });
}
const proxyFn = ({ type, position, args }) => {
if (filter.type && type !== filter.type) return;
if (filter.position && position !== String(filter.position).toLowerCase()) return;
this._invoke(fn, args[0], child, variant);
};
emitter[proxyKey].push(proxyFn);
this._attached.push({ emitter, kind: 'proxyEntry', proxyKey, proxyFn });
}
}
_attach(emitter, eventName, listener) {
emitter.on(eventName, listener);
this._attached.push({ emitter, kind: 'listener', eventName, listener });
}
_invoke(fn, eventData, child, variant) {
try { fn.call(this.domain, eventData, child); }
catch (err) { this._logHandlerError(`on${variant === 'measured' ? 'Measurement' : 'Prediction'}`, '', err); }
}
_logHandlerError(kind, key, err) {
if (this.logger?.warn) {
this.logger.warn(`ChildRouter ${kind}${key ? `[${key}]` : ''} handler threw: ${err?.message || err}`);
}
}
// ── teardown ──────────────────────────────────────────────────────
tearDown() {
// Two passes: drop concrete listeners + proxy entries first, then unwrap
// any proxies whose entry list is now empty. Order matters — restoring
// emit before clearing entries would leave dangling proxy state.
for (const rec of this._attached) {
if (rec.kind === 'listener') {
if (typeof rec.emitter.off === 'function') rec.emitter.off(rec.eventName, rec.listener);
else if (typeof rec.emitter.removeListener === 'function') rec.emitter.removeListener(rec.eventName, rec.listener);
} else if (rec.kind === 'proxyEntry') {
const proxies = rec.emitter[rec.proxyKey];
if (Array.isArray(proxies)) {
const idx = proxies.indexOf(rec.proxyFn);
if (idx >= 0) proxies.splice(idx, 1);
}
}
}
for (const rec of this._attached) {
if (rec.kind !== 'proxy') continue;
const proxies = rec.emitter[rec.proxyKey];
if (!Array.isArray(proxies) || proxies.length === 0) {
rec.emitter.emit = rec.original;
delete rec.emitter[rec.proxyKey];
}
}
this._attached = [];
}
}
module.exports = ChildRouter;

102
src/domain/HealthStatus.js Normal file
View File

@@ -0,0 +1,102 @@
/**
* HealthStatus — standardised health/quality datum.
* Contract: see .claude/refactor/CONTRACTS.md §9.
*
* Shape (always frozen):
* { level: 0|1|2|3, flags: string[], message: string, source: string|null }
*
* level 0 = nominal, 3 = unusable. Returned objects are frozen plain
* objects (not class instances) so they round-trip cleanly through
* JSON / InfluxDB serialisation.
*/
'use strict';
const LABELS = ['nominal', 'minor', 'major', 'critical'];
function _freeze(level, flags, message, source) {
return Object.freeze({
level,
flags: Object.freeze(flags.slice()),
message,
source: source == null ? null : String(source),
});
}
function _coerceDegradedLevel(level) {
const n = Math.trunc(Number(level));
if (!Number.isFinite(n) || n < 1) return 1;
if (n > 3) return 3;
return n;
}
function _coerceFlags(flags) {
if (!Array.isArray(flags)) return [];
const out = [];
for (const f of flags) {
if (f == null) continue;
out.push(String(f));
}
return out;
}
function ok(message, source) {
return _freeze(
0,
[],
typeof message === 'string' && message.length > 0 ? message : 'nominal',
source != null ? source : null,
);
}
function degraded(level, flags, message, source) {
const lvl = _coerceDegradedLevel(level);
const f = _coerceFlags(flags);
const m = typeof message === 'string' && message.length > 0
? message
: LABELS[lvl];
return _freeze(lvl, f, m, source != null ? source : null);
}
// Merge multiple statuses into one node-level status. Worst level wins
// for level/message/source; flags are concatenated and de-duped.
function compose(statuses) {
if (!Array.isArray(statuses) || statuses.length === 0) return ok();
let worst = null;
const seen = new Set();
const flags = [];
for (const s of statuses) {
if (!s || typeof s !== 'object') continue;
const lvl = Number.isFinite(s.level) ? s.level : 0;
if (worst === null || lvl > worst.level) {
worst = { level: lvl, message: s.message, source: s.source ?? null };
}
if (Array.isArray(s.flags)) {
for (const f of s.flags) {
if (f == null) continue;
const k = String(f);
if (!seen.has(k)) {
seen.add(k);
flags.push(k);
}
}
}
}
if (worst === null) return ok();
const message = typeof worst.message === 'string' && worst.message.length > 0
? worst.message
: LABELS[Math.max(0, Math.min(3, worst.level))];
return _freeze(worst.level, flags, message, worst.source);
}
function label(level) {
const n = Math.trunc(Number(level));
if (!Number.isFinite(n) || n < 0 || n > 3) return 'unknown';
return LABELS[n];
}
module.exports = { ok, degraded, compose, label };

View File

@@ -0,0 +1,74 @@
'use strict';
// Serialises an async dispatch so that high-frequency callers cannot stack
// up overlapping invocations. Intermediate values are dropped — only the
// most recent fire() during an in-flight dispatch is replayed afterwards.
// Extracted from machineGroupControl's _dispatchInFlight + _delayedCall
// pattern so MGC, pumpingStation, valveGroupControl etc. can share it.
class LatestWinsGate {
constructor(asyncDispatchFn, options = {}) {
if (typeof asyncDispatchFn !== 'function') {
throw new TypeError('LatestWinsGate requires an async dispatch function');
}
this._dispatch = asyncDispatchFn;
this._logger = options.logger || null;
this._inFlight = false;
this._pending = null; // { value, ctx } | null
this._drainResolvers = []; // resolved when idle again
this.lastError = null;
}
// 0 = idle, 1 = running with no pending, 2 = running with pending.
get size() {
if (!this._inFlight) return 0;
return this._pending ? 2 : 1;
}
// Never blocks the caller. If a dispatch is in flight, the latest
// value is parked; older parked values are silently overwritten.
fire(value, ctx) {
if (this._inFlight) {
this._pending = { value, ctx };
return;
}
this._run(value, ctx);
}
drain() {
if (!this._inFlight && !this._pending) return Promise.resolve();
return new Promise((resolve) => { this._drainResolvers.push(resolve); });
}
_run(value, ctx) {
this._inFlight = true;
// Kick the dispatch on a microtask so fire() always returns
// synchronously, even if _dispatch resolves immediately.
Promise.resolve()
.then(() => this._dispatch(value, ctx))
.catch((err) => {
this.lastError = err;
if (this._logger && typeof this._logger.error === 'function') {
this._logger.error(err);
}
// Swallow: an error must not deadlock the gate.
})
.then(() => this._afterDispatch());
}
_afterDispatch() {
this._inFlight = false;
if (this._pending) {
const { value, ctx } = this._pending;
this._pending = null;
this._run(value, ctx);
return;
}
// Idle — release any drain() waiters.
const waiters = this._drainResolvers;
this._drainResolvers = [];
for (const r of waiters) r();
}
}
module.exports = LatestWinsGate;

149
src/domain/UnitPolicy.js Normal file
View File

@@ -0,0 +1,149 @@
const convert = require('../convert/index.js');
// Map MeasurementContainer measurement-type names to convert-module
// "measure" families. Mirrors MeasurementContainer.measureMap so a policy
// declared with the type names domains use ('flow', 'pressure', ...) can be
// validated against the same convert-module families MeasurementContainer
// uses internally.
const TYPE_TO_MEASURE = Object.freeze({
pressure: 'pressure',
atmpressure: 'pressure',
flow: 'volumeFlowRate',
power: 'power',
hydraulicpower: 'power',
reactivepower: 'reactivePower',
apparentpower: 'apparentPower',
temperature: 'temperature',
volume: 'volume',
length: 'length',
mass: 'mass',
energy: 'energy',
reactiveenergy: 'reactiveEnergy',
});
const DEFAULT_REQUIRED_TYPES = Object.freeze(['flow', 'pressure', 'power', 'temperature']);
class UnitPolicy {
constructor({ canonical, output, curve, requireUnitForTypes, logger } = {}) {
this._canonical = freezeShallow(canonical);
this._output = freezeShallow(output);
this._curve = curve ? freezeShallow(curve) : null;
this._requireUnitForTypes = Object.freeze(
Array.isArray(requireUnitForTypes) ? [...requireUnitForTypes] : [...DEFAULT_REQUIRED_TYPES]
);
this._logger = logger || null;
// Warn-once memo: same (label, candidate) pair only logs the first time.
this._warned = new Set();
}
static declare(spec = {}) {
if (!spec.canonical || typeof spec.canonical !== 'object') {
throw new Error('UnitPolicy.declare: canonical units map is required');
}
if (!spec.output || typeof spec.output !== 'object') {
throw new Error('UnitPolicy.declare: output units map is required');
}
return new UnitPolicy(spec);
}
setLogger(logger) {
this._logger = logger || null;
return this;
}
canonical(type) {
return this._canonical[type] || null;
}
output(type) {
return this._output[type] || null;
}
curve(type) {
return this._curve ? (this._curve[type] || null) : null;
}
/**
* Validate a user-supplied unit string against `expectedMeasure`. On any
* mismatch return `fallback` and warn once for this (label, candidate)
* pair. On success return the trimmed candidate.
*/
resolve(candidate, expectedMeasure, fallback, label = 'unit') {
const fallbackUnit = String(fallback || '').trim();
const raw = typeof candidate === 'string' ? candidate.trim() : '';
if (!raw) return fallbackUnit;
try {
const desc = convert().describe(raw);
const measure = resolveMeasure(expectedMeasure);
if (measure && desc.measure !== measure) {
throw new Error(`expected ${measure} but got ${desc.measure}`);
}
return raw;
} catch (error) {
this._warnOnce(label, raw, `Invalid ${label} unit '${raw}' (${error.message}). Falling back to '${fallbackUnit}'.`);
return fallbackUnit;
}
}
/**
* Strict numeric conversion. Throws if value is not finite.
* No-ops (still returning a Number) when from/to are missing or equal.
*/
convert(value, fromUnit, toUnit, contextLabel = 'unit conversion') {
const numeric = Number(value);
if (!Number.isFinite(numeric)) {
throw new Error(`${contextLabel}: value '${value}' is not finite`);
}
if (!fromUnit || !toUnit || fromUnit === toUnit) return numeric;
return convert(numeric).from(fromUnit).to(toUnit);
}
/**
* Returns the option bag for `new MeasurementContainer(options, logger)`.
* Exact shape required by MeasurementContainer; see
* src/measurements/MeasurementContainer.js constructor.
*/
containerOptions() {
const defaultUnits = { ...this._output };
const preferredUnits = { ...this._output };
const canonicalUnits = { ...this._canonical };
return {
defaultUnits,
preferredUnits,
canonicalUnits,
storeCanonical: true,
strictUnitValidation: true,
throwOnInvalidUnit: true,
requireUnitForTypes: [...this._requireUnitForTypes],
};
}
_warnOnce(label, candidate, message) {
const key = `${label}::${candidate}`;
if (this._warned.has(key)) return;
this._warned.add(key);
if (this._logger && typeof this._logger.warn === 'function') {
this._logger.warn(message);
} else {
// Last-resort fallback so misconfigurations don't go silent in
// domains that haven't wired a logger yet.
console.warn(message);
}
}
}
function freezeShallow(obj) {
return Object.freeze({ ...(obj || {}) });
}
// Accepts either the convert-module measure family ('volumeFlowRate') or one
// of our type names ('flow') and returns the convert-module measure.
function resolveMeasure(expected) {
if (!expected) return null;
const lower = String(expected).trim().toLowerCase();
if (TYPE_TO_MEASURE[lower]) return TYPE_TO_MEASURE[lower];
return expected;
}
module.exports = UnitPolicy;