@@ -169,6 +169,50 @@ class MeasurementContainer {
return this ;
}
/**
* Check whether a measurement series exists.
*
* You can rely on the current chain (type/variant/position already set via
* type().variant().position()), or pass them explicitly via the options.
*
* @param {object} options
* @param {string} [options.type] Override the current type
* @param {string} [options.variant] Override the current variant
* @param {string} [options.position] Override the current position
* @param {boolean} [options.requireValues=false]
* When true, the series must contain at least one stored value.
*
* @returns {boolean}
*/
exists ( { type , variant , position , requireValues = false } = { } ) {
const typeKey = type ? ? this . _currentType ;
if ( ! typeKey ) return false ;
const variantKey = variant ? ? this . _currentVariant ;
if ( ! variantKey ) return false ;
const positionKey = position ? ? this . _currentPosition ;
const typeBucket = this . measurements [ typeKey ] ;
if ( ! typeBucket ) return false ;
const variantBucket = typeBucket [ variantKey ] ;
if ( ! variantBucket ) return false ;
if ( ! positionKey ) {
// No specific position requested – just check the variant bucket.
return requireValues
? Object . values ( variantBucket ) . some ( m => m ? . values ? . length > 0 )
: Object . keys ( variantBucket ) . length > 0 ;
}
const measurement = variantBucket [ positionKey ] ;
if ( ! measurement ) return false ;
return requireValues ? measurement . values ? . length > 0 : true ;
}
unit ( unitName ) {
if ( ! this . _ensureChainIsValid ( ) ) return this ;
@@ -252,8 +296,9 @@ class MeasurementContainer {
const measurement = this . get ( ) ;
if ( ! measurement ) return null ;
const valu e = measurement . getLaggedSample ( lag ) ;
if ( valu e === null ) return null ;
let sampl e = measurement . getLaggedSample ( lag ) ;
if ( sampl e === null ) return null ;
const value = sample . value ;
// Return as-is if no unit conversion requested
if ( ! requestedUnit ) {
@@ -263,12 +308,17 @@ class MeasurementContainer {
// Convert if needed
if ( measurement . unit && requestedUnit !== measurement . unit ) {
try {
return convertModule ( value ) . from ( measurement . unit ) . to ( requestedUnit ) ;
const convertedValue = convertModule ( value ) . from ( measurement . unit ) . to ( requestedUnit ) ;
//replace old value in sample and return obj
sample . value = convertedValue ;
sample . unit = requestedUnit ;
return sample ;
} catch ( error ) {
if ( this . logger ) {
this . logger . error ( ` Unit conversion failed: ${ error . message } ` ) ;
}
return valu e; // Return original value if conversion fails
return sampl e; // Return original value if conversion fails
}
}