From a81733c4928a2175a5570d098bfe1481dc291ef1 Mon Sep 17 00:00:00 2001 From: Rene De ren Date: Fri, 28 Nov 2025 16:29:24 +0100 Subject: [PATCH] added examples --- src/measurements/examples.js | 62 ++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/measurements/examples.js b/src/measurements/examples.js index 4e513c4..d34945d 100644 --- a/src/measurements/examples.js +++ b/src/measurements/examples.js @@ -345,6 +345,68 @@ basicContainer.getTypes().forEach(type => { } }); +// --------------------------------------------------------------------------- +// --- Child Aggregation ----------------------------------------------------- +// --------------------------------------------------------------------------- + +// ==================================== +// AGGREGATION WITH CHILD SERIES (sum) +// ==================================== +console.log(); +console.log('--- Example X: Aggregation with sum() and child series ---'); + +// Container where flow is stored internally in m3/h +const aggContainer = new MeasurementContainer({ + windowSize: 10, + defaultUnits: { + flow: 'm3/h', + }, +}); + +// Two pumps both feeding the same inlet position +aggContainer + .child('pumpA') + .type('flow') + .variant('measured') + .position('inlet') + .value(10, Date.now(), 'm3/h'); // 10 m3/h + +aggContainer + .child('pumpB') + .type('flow') + .variant('measured') + .position('inlet') + .value(15, Date.now(), 'm3/h'); // 15 m3/h + +// Another position, e.g. outlet, also with two pumps +aggContainer + .child('pumpA') + .type('flow') + .variant('measured') + .position('outlet') + .value(8, Date.now(), 'm3/h'); // 8 m3/h + +aggContainer + .child('pumpB') + .type('flow') + .variant('measured') + .position('outlet') + .value(11, Date.now(), 'm3/h'); // 11 m3/h + + +// 1) Sum only inlet position (children pumpA + pumpB) +const inletTotal = aggContainer.sum('flow', 'measured', ['inlet']); +console.log(`Total inlet flow: ${inletTotal} m3/h (expected 25 m3/h)`); + +// 2) Sum inlet + outlet positions together +const totalAll = aggContainer.sum('flow', 'measured', ['inlet', 'outlet']); +console.log(`Total inlet+outlet flow: ${totalAll} m3/h (expected 44 m3/h)`); + +// 3) Same sum but explicitly ask for a target unit (e.g. l/s) +// This will use convertModule(...) internally. +// If conversion is not supported, it will fall back to the raw value. +const totalAllLps = aggContainer.sum('flow', 'measured', ['inlet', 'outlet'], 'l/s'); +console.log(`Total inlet+outlet flow in l/s: ${totalAllLps} l/s (converted from m3/h)\n`); console.log('\n✅ All examples complete!\n');