{"version":3,"file":"LastValue.mjs","names":[],"sources":["../../src/aggregator/LastValue.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type {\n  Accumulation,\n  AccumulationRecord,\n  Aggregator,\n  LastValue,\n} from './types';\nimport { AggregatorKind } from './types';\nimport type { HrTime } from '@opentelemetry/api';\nimport { millisToHrTime, hrTimeToMicroseconds } from '@opentelemetry/core';\nimport type { GaugeMetricData } from '../export/MetricData';\nimport { DataPointType } from '../export/MetricData';\nimport type { Maybe } from '../utils';\nimport type { AggregationTemporality } from '../export/AggregationTemporality';\nimport type { InstrumentDescriptor } from '../InstrumentDescriptor';\n\nexport class LastValueAccumulation implements Accumulation {\n  public startTime;\n  private _current;\n  public sampleTime;\n\n  constructor(startTime: HrTime, current = 0, sampleTime: HrTime = [0, 0]) {\n    this.startTime = startTime;\n    this._current = current;\n    this.sampleTime = sampleTime;\n  }\n\n  record(value: number): void {\n    this._current = value;\n    this.sampleTime = millisToHrTime(Date.now());\n  }\n\n  setStartTime(startTime: HrTime): void {\n    this.startTime = startTime;\n  }\n\n  toPointValue(): LastValue {\n    return this._current;\n  }\n}\n\n/** Basic aggregator which calculates a LastValue from individual measurements. */\nexport class LastValueAggregator implements Aggregator<LastValueAccumulation> {\n  public kind: AggregatorKind.LAST_VALUE = AggregatorKind.LAST_VALUE;\n\n  createAccumulation(startTime: HrTime) {\n    return new LastValueAccumulation(startTime);\n  }\n\n  /**\n   * Returns the result of the merge of the given accumulations.\n   *\n   * Return the newly captured (delta) accumulation for LastValueAggregator.\n   */\n  merge(\n    previous: LastValueAccumulation,\n    delta: LastValueAccumulation\n  ): LastValueAccumulation {\n    // nanoseconds may lose precisions.\n    const latestAccumulation =\n      hrTimeToMicroseconds(delta.sampleTime) >=\n      hrTimeToMicroseconds(previous.sampleTime)\n        ? delta\n        : previous;\n    return new LastValueAccumulation(\n      previous.startTime,\n      latestAccumulation.toPointValue(),\n      latestAccumulation.sampleTime\n    );\n  }\n\n  /**\n   * Returns a new DELTA aggregation by comparing two cumulative measurements.\n   *\n   * A delta aggregation is not meaningful to LastValueAggregator, just return\n   * the newly captured (delta) accumulation for LastValueAggregator.\n   */\n  diff(\n    previous: LastValueAccumulation,\n    current: LastValueAccumulation\n  ): LastValueAccumulation {\n    // nanoseconds may lose precisions.\n    const latestAccumulation =\n      hrTimeToMicroseconds(current.sampleTime) >=\n      hrTimeToMicroseconds(previous.sampleTime)\n        ? current\n        : previous;\n    return new LastValueAccumulation(\n      current.startTime,\n      latestAccumulation.toPointValue(),\n      latestAccumulation.sampleTime\n    );\n  }\n\n  toMetricData(\n    descriptor: InstrumentDescriptor,\n    aggregationTemporality: AggregationTemporality,\n    accumulationByAttributes: AccumulationRecord<LastValueAccumulation>[],\n    endTime: HrTime\n  ): Maybe<GaugeMetricData> {\n    return {\n      descriptor,\n      aggregationTemporality,\n      dataPointType: DataPointType.GAUGE,\n      dataPoints: accumulationByAttributes.map(([attributes, accumulation]) => {\n        return {\n          attributes,\n          startTime: accumulation.startTime,\n          endTime,\n          value: accumulation.toPointValue(),\n        };\n      }),\n    };\n  }\n}\n"],"mappings":";;;AAoBA,IAAa,wBAAb,MAA2D;CACzD;CACA;CACA;CAEA,YAAY,WAAmB,UAAU,GAAG,aAAqB,CAAC,GAAG,CAAC,GAAG;EACvE,KAAK,YAAY;EACjB,KAAK,WAAW;EAChB,KAAK,aAAa;CACpB;CAEA,OAAO,OAAqB;EAC1B,KAAK,WAAW;EAChB,KAAK,aAAa,eAAe,KAAK,IAAI,CAAC;CAC7C;CAEA,aAAa,WAAyB;EACpC,KAAK,YAAY;CACnB;CAEA,eAA0B;EACxB,OAAO,KAAK;CACd;AACF;;AAGA,IAAa,sBAAb,MAA8E;CAC5E,OAAO;CAEP,mBAAmB,WAAmB;EACpC,OAAO,IAAI,sBAAsB,SAAS;CAC5C;;;;;;CAOA,MACE,UACA,OACuB;EAEvB,MAAM,qBACJ,qBAAqB,MAAM,UAAU,KACrC,qBAAqB,SAAS,UAAU,IACpC,QACA;EACN,OAAO,IAAI,sBACT,SAAS,WACT,mBAAmB,aAAa,GAChC,mBAAmB,UACrB;CACF;;;;;;;CAQA,KACE,UACA,SACuB;EAEvB,MAAM,qBACJ,qBAAqB,QAAQ,UAAU,KACvC,qBAAqB,SAAS,UAAU,IACpC,UACA;EACN,OAAO,IAAI,sBACT,QAAQ,WACR,mBAAmB,aAAa,GAChC,mBAAmB,UACrB;CACF;CAEA,aACE,YACA,wBACA,0BACA,SACwB;EACxB,OAAO;GACL;GACA;GACA,eAAA;GACA,YAAY,yBAAyB,KAAK,CAAC,YAAY,kBAAkB;IACvE,OAAO;KACL;KACA,WAAW,aAAa;KACxB;KACA,OAAO,aAAa,aAAa;IACnC;GACF,CAAC;EACH;CACF;AACF"}