|
|
|
|
@@ -9,6 +9,7 @@ class MeasurementContainer {
|
|
|
|
|
this.windowSize = options.windowSize || 10; // Default window size
|
|
|
|
|
|
|
|
|
|
// For chaining context
|
|
|
|
|
this._currentChildId = null;
|
|
|
|
|
this._currentType = null;
|
|
|
|
|
this._currentVariant = null;
|
|
|
|
|
this._currentPosition = null;
|
|
|
|
|
@@ -49,6 +50,11 @@ class MeasurementContainer {
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
child(childId) {
|
|
|
|
|
this._currentChildId = childId || 'default';
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setChildName(childName) {
|
|
|
|
|
this.childName = childName;
|
|
|
|
|
return this;
|
|
|
|
|
@@ -72,6 +78,13 @@ class MeasurementContainer {
|
|
|
|
|
null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getUnit(type) {
|
|
|
|
|
if (!type) return null;
|
|
|
|
|
if (this.preferredUnits && this.preferredUnits[type]) return this.preferredUnits[type];
|
|
|
|
|
if (this.defaultUnits && this.defaultUnits[type]) return this.defaultUnits[type];
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Chainable methods
|
|
|
|
|
type(typeName) {
|
|
|
|
|
this._currentType = typeName;
|
|
|
|
|
@@ -226,35 +239,46 @@ class MeasurementContainer {
|
|
|
|
|
// Terminal operations - get data out
|
|
|
|
|
get() {
|
|
|
|
|
if (!this._ensureChainIsValid()) return null;
|
|
|
|
|
return this._getOrCreateMeasurement();
|
|
|
|
|
const variantBucket = this.measurements[this._currentType]?.[this._currentVariant];
|
|
|
|
|
if (!variantBucket) return null;
|
|
|
|
|
const posBucket = variantBucket[this._currentPosition];
|
|
|
|
|
if (!posBucket) return null;
|
|
|
|
|
|
|
|
|
|
// Legacy single measurement
|
|
|
|
|
if (posBucket?.getCurrentValue) return posBucket;
|
|
|
|
|
|
|
|
|
|
// Child-aware: pick requested child, otherwise fall back to default, otherwise first available
|
|
|
|
|
if (posBucket && typeof posBucket === 'object') {
|
|
|
|
|
const requestedKey = this._currentChildId || this.childId;
|
|
|
|
|
const keys = Object.keys(posBucket);
|
|
|
|
|
if (!keys.length) return null;
|
|
|
|
|
const measurement =
|
|
|
|
|
(requestedKey && posBucket[requestedKey]) ||
|
|
|
|
|
posBucket.default ||
|
|
|
|
|
posBucket[keys[0]];
|
|
|
|
|
return measurement || null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getCurrentValue(requestedUnit = null) {
|
|
|
|
|
const measurement = this.get();
|
|
|
|
|
if (!measurement) return null;
|
|
|
|
|
|
|
|
|
|
const value = measurement.getCurrentValue();
|
|
|
|
|
if (value === null) return null;
|
|
|
|
|
|
|
|
|
|
// Return as-is if no unit conversion requested
|
|
|
|
|
if (!requestedUnit) {
|
|
|
|
|
if (!requestedUnit || !measurement.unit || requestedUnit === measurement.unit) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert if needed
|
|
|
|
|
if (measurement.unit && requestedUnit !== measurement.unit) {
|
|
|
|
|
try {
|
|
|
|
|
return convertModule(value).from(measurement.unit).to(requestedUnit);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (this.logger) {
|
|
|
|
|
this.logger.error(`Unit conversion failed: ${error.message}`);
|
|
|
|
|
}
|
|
|
|
|
return value; // Return original value if conversion fails
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.logger) this.logger.error(`Unit conversion failed: ${error.message}`);
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getAverage(requestedUnit = null) {
|
|
|
|
|
const measurement = this.get();
|
|
|
|
|
@@ -357,19 +381,72 @@ class MeasurementContainer {
|
|
|
|
|
return sample;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sum(type, variant, positions = [], targetUnit = null) {
|
|
|
|
|
const bucket = this.measurements?.[type]?.[variant];
|
|
|
|
|
if (!bucket) return 0;
|
|
|
|
|
return positions
|
|
|
|
|
.map((pos) => {
|
|
|
|
|
const posBucket = bucket[pos];
|
|
|
|
|
if (!posBucket) return 0;
|
|
|
|
|
return Object.values(posBucket)
|
|
|
|
|
.map((m) => {
|
|
|
|
|
if (!m?.getCurrentValue) return 0;
|
|
|
|
|
const val = m.getCurrentValue();
|
|
|
|
|
if (val == null) return 0;
|
|
|
|
|
const fromUnit = m.unit || targetUnit;
|
|
|
|
|
if (!targetUnit || !fromUnit || fromUnit === targetUnit) return val;
|
|
|
|
|
try { return convertModule(val).from(fromUnit).to(targetUnit); } catch { return val; }
|
|
|
|
|
})
|
|
|
|
|
.reduce((acc, v) => acc + (Number.isFinite(v) ? v : 0), 0);
|
|
|
|
|
})
|
|
|
|
|
.reduce((acc, v) => acc + v, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getFlattenedOutput() {
|
|
|
|
|
const out = {};
|
|
|
|
|
Object.entries(this.measurements).forEach(([type, variants]) => {
|
|
|
|
|
Object.entries(variants).forEach(([variant, positions]) => {
|
|
|
|
|
Object.entries(positions).forEach(([position, entry]) => {
|
|
|
|
|
// Legacy single series
|
|
|
|
|
if (entry?.getCurrentValue) {
|
|
|
|
|
out[`${type}.${variant}.${position}`] = entry.getCurrentValue();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Child-bucketed series
|
|
|
|
|
if (entry && typeof entry === 'object') {
|
|
|
|
|
Object.entries(entry).forEach(([childId, m]) => {
|
|
|
|
|
if (m?.getCurrentValue) {
|
|
|
|
|
out[`${type}.${variant}.${position}.${childId}`] = m.getCurrentValue();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Difference calculations between positions
|
|
|
|
|
difference({ from = "downstream", to = "upstream", unit: requestedUnit } = {}) {
|
|
|
|
|
difference({ from = "downstream", to = "upstream", unit: requestedUnit } = {}) {
|
|
|
|
|
if (!this._currentType || !this._currentVariant) {
|
|
|
|
|
throw new Error("Type and variant must be specified for difference calculation");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const get = pos =>
|
|
|
|
|
this.measurements?.[this._currentType]?.[this._currentVariant]?.[pos] || null;
|
|
|
|
|
const get = pos => {
|
|
|
|
|
const bucket = this.measurements?.[this._currentType]?.[this._currentVariant]?.[pos];
|
|
|
|
|
if (!bucket) return null;
|
|
|
|
|
// child-aware bucket: pick current childId/default or first available
|
|
|
|
|
if (bucket && typeof bucket === 'object' && !bucket.getCurrentValue) {
|
|
|
|
|
const childKey = this._currentChildId || this.childId || Object.keys(bucket)[0];
|
|
|
|
|
return bucket?.[childKey] || null;
|
|
|
|
|
}
|
|
|
|
|
// legacy single measurement
|
|
|
|
|
return bucket;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const a = get(from);
|
|
|
|
|
const b = get(to);
|
|
|
|
|
if (!a || !b || a.values.length === 0 || b.values.length === 0) {
|
|
|
|
|
if (!a || !b || !a.values || !b.values || a.values.length === 0 || b.values.length === 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -380,14 +457,8 @@ difference({ from = "downstream", to = "upstream", unit: requestedUnit } = {}) {
|
|
|
|
|
const aAvg = this._convertValueToUnit(a.getAverage(), a.unit, targetUnit);
|
|
|
|
|
const bAvg = this._convertValueToUnit(b.getAverage(), b.unit, targetUnit);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
value: aVal - bVal,
|
|
|
|
|
avgDiff: aAvg - bAvg,
|
|
|
|
|
unit: targetUnit,
|
|
|
|
|
from,
|
|
|
|
|
to,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return { value: aVal - bVal, avgDiff: aAvg - bAvg, unit: targetUnit, from, to };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helper methods
|
|
|
|
|
_ensureChainIsValid() {
|
|
|
|
|
@@ -410,18 +481,26 @@ difference({ from = "downstream", to = "upstream", unit: requestedUnit } = {}) {
|
|
|
|
|
this.measurements[this._currentType][this._currentVariant] = {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!this.measurements[this._currentType][this._currentVariant][this._currentPosition]) {
|
|
|
|
|
this.measurements[this._currentType][this._currentVariant][this._currentPosition] =
|
|
|
|
|
new MeasurementBuilder()
|
|
|
|
|
const positionKey = this._currentPosition;
|
|
|
|
|
const childKey = this._currentChildId || this.childId || 'default';
|
|
|
|
|
|
|
|
|
|
if (!this.measurements[this._currentType][this._currentVariant][positionKey]) {
|
|
|
|
|
this.measurements[this._currentType][this._currentVariant][positionKey] = {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bucket = this.measurements[this._currentType][this._currentVariant][positionKey];
|
|
|
|
|
|
|
|
|
|
if (!bucket[childKey]) {
|
|
|
|
|
bucket[childKey] = new MeasurementBuilder()
|
|
|
|
|
.setType(this._currentType)
|
|
|
|
|
.setVariant(this._currentVariant)
|
|
|
|
|
.setPosition(this._currentPosition)
|
|
|
|
|
.setPosition(positionKey)
|
|
|
|
|
.setWindowSize(this.windowSize)
|
|
|
|
|
.setDistance(this._currentDistance)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.measurements[this._currentType][this._currentVariant][this._currentPosition];
|
|
|
|
|
return bucket[childKey];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Additional utility methods
|
|
|
|
|
|