{"version":3,"file":"Sum.cjs","names":[],"sources":["../../src/aggregator/Sum.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type {\n  Sum,\n  Aggregator,\n  Accumulation,\n  AccumulationRecord,\n} from './types';\nimport { AggregatorKind } from './types';\nimport type { HrTime } from '@opentelemetry/api';\nimport type { SumMetricData } 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 SumAccumulation implements Accumulation {\n  public startTime;\n  public monotonic;\n  private _current;\n  public reset;\n\n  constructor(\n    startTime: HrTime,\n    monotonic: boolean,\n    current = 0,\n    reset = false\n  ) {\n    this.startTime = startTime;\n    this.monotonic = monotonic;\n    this._current = current;\n    this.reset = reset;\n  }\n\n  record(value: number): void {\n    if (this.monotonic && value < 0) {\n      return;\n    }\n    this._current += value;\n  }\n\n  setStartTime(startTime: HrTime): void {\n    this.startTime = startTime;\n  }\n\n  toPointValue(): Sum {\n    return this._current;\n  }\n}\n\n/** Basic aggregator which calculates a Sum from individual measurements. */\nexport class SumAggregator implements Aggregator<SumAccumulation> {\n  public kind: AggregatorKind.SUM = AggregatorKind.SUM;\n  public monotonic: boolean;\n\n  constructor(monotonic: boolean) {\n    this.monotonic = monotonic;\n  }\n\n  createAccumulation(startTime: HrTime) {\n    return new SumAccumulation(startTime, this.monotonic);\n  }\n\n  /**\n   * Returns the result of the merge of the given accumulations.\n   */\n  merge(previous: SumAccumulation, delta: SumAccumulation): SumAccumulation {\n    const prevPv = previous.toPointValue();\n    const deltaPv = delta.toPointValue();\n    if (delta.reset) {\n      return new SumAccumulation(\n        delta.startTime,\n        this.monotonic,\n        deltaPv,\n        delta.reset\n      );\n    }\n    return new SumAccumulation(\n      previous.startTime,\n      this.monotonic,\n      prevPv + deltaPv\n    );\n  }\n\n  /**\n   * Returns a new DELTA aggregation by comparing two cumulative measurements.\n   */\n  diff(previous: SumAccumulation, current: SumAccumulation): SumAccumulation {\n    const prevPv = previous.toPointValue();\n    const currPv = current.toPointValue();\n    /**\n     * If the SumAggregator is a monotonic one and the previous point value is\n     * greater than the current one, a reset is deemed to be happened.\n     * Return the current point value to prevent the value from been reset.\n     */\n    if (this.monotonic && prevPv > currPv) {\n      return new SumAccumulation(\n        current.startTime,\n        this.monotonic,\n        currPv,\n        true\n      );\n    }\n    return new SumAccumulation(\n      current.startTime,\n      this.monotonic,\n      currPv - prevPv\n    );\n  }\n\n  toMetricData(\n    descriptor: InstrumentDescriptor,\n    aggregationTemporality: AggregationTemporality,\n    accumulationByAttributes: AccumulationRecord<SumAccumulation>[],\n    endTime: HrTime\n  ): Maybe<SumMetricData> {\n    return {\n      descriptor,\n      aggregationTemporality,\n      dataPointType: DataPointType.SUM,\n      dataPoints: accumulationByAttributes.map(([attributes, accumulation]) => {\n        return {\n          attributes,\n          startTime: accumulation.startTime,\n          endTime,\n          value: accumulation.toPointValue(),\n        };\n      }),\n      isMonotonic: this.monotonic,\n    };\n  }\n}\n"],"mappings":";;AAmBA,IAAa,kBAAb,MAAqD;CACnD;CACA;CACA;CACA;CAEA,YACE,WACA,WACA,UAAU,GACV,QAAQ,OACR;EACA,KAAK,YAAY;EACjB,KAAK,YAAY;EACjB,KAAK,WAAW;EAChB,KAAK,QAAQ;CACf;CAEA,OAAO,OAAqB;EAC1B,IAAI,KAAK,aAAa,QAAQ,GAC5B;EAEF,KAAK,YAAY;CACnB;CAEA,aAAa,WAAyB;EACpC,KAAK,YAAY;CACnB;CAEA,eAAoB;EAClB,OAAO,KAAK;CACd;AACF;;AAGA,IAAa,gBAAb,MAAkE;CAChE,OAAO;CACP;CAEA,YAAY,WAAoB;EAC9B,KAAK,YAAY;CACnB;CAEA,mBAAmB,WAAmB;EACpC,OAAO,IAAI,gBAAgB,WAAW,KAAK,SAAS;CACtD;;;;CAKA,MAAM,UAA2B,O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