{"version":3,"file":"MetricDataSplitter.cjs","names":[],"sources":["../../src/export/MetricDataSplitter.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type { DataPoint, ResourceMetrics, ScopeMetrics } from './MetricData';\n\n/**\n * Splits a ResourceMetrics object into smaller ResourceMetrics objects\n * such that no batch exceeds maxExportBatchSize data points.\n * @param resourceMetrics The metrics to split.\n * @param maxExportBatchSize The maximum number of data points per batch.\n * @internal\n */\nexport function splitMetricData(\n  resourceMetrics: ResourceMetrics,\n  maxExportBatchSize: number\n): ResourceMetrics[] {\n  if (!Number.isInteger(maxExportBatchSize) || maxExportBatchSize <= 0) {\n    throw new Error('maxExportBatchSize must be a positive integer');\n  }\n  const batches: ResourceMetrics[] = [];\n  let currentBatchPoints = 0;\n  let currentScopeMetrics: ScopeMetrics[] = [];\n\n  function flush() {\n    if (currentScopeMetrics.length > 0) {\n      batches.push({\n        resource: resourceMetrics.resource,\n        scopeMetrics: currentScopeMetrics,\n      });\n      currentScopeMetrics = [];\n      currentBatchPoints = 0;\n    }\n  }\n\n  // Iterate through all scopes in the input metrics\n  for (const scopeMetric of resourceMetrics.scopeMetrics) {\n    let scopeMetricCopy: ScopeMetrics | null = null;\n    // Iterate through all metrics within the current scope\n    for (const metric of scopeMetric.metrics) {\n      const dataPoints = metric.dataPoints;\n      // If a metric has no data points, add it directly to the current batch\n      if (dataPoints.length === 0) {\n        if (!scopeMetricCopy) {\n          scopeMetricCopy = { scope: scopeMetric.scope, metrics: [] };\n          currentScopeMetrics.push(scopeMetricCopy);\n        }\n        scopeMetricCopy.metrics.push(metric);\n        continue;\n      }\n      // Chunk the data points of the current metric across batches. We iterate\n      // with an offset instead of repeatedly slicing the remaining tail, which\n      // keeps the overall work linear in the number of data points.\n      let offset = 0;\n      while (offset < dataPoints.length) {\n        const spaceLeft = maxExportBatchSize - currentBatchPoints;\n        const take = Math.min(spaceLeft, dataPoints.length - offset);\n        // Ensure we have a ScopeMetrics object in the current batch\n        if (!scopeMetricCopy) {\n          scopeMetricCopy = { scope: scopeMetric.scope, metrics: [] };\n          currentScopeMetrics.push(scopeMetricCopy);\n        }\n        // A metric receives exactly one contiguous chunk per batch (after\n        // appending, the batch is either full and flushed, or the metric is\n        // exhausted), so we can slice the data points directly rather than\n        // copying them incrementally.\n        const metricCopy = {\n          ...metric,\n          dataPoints: dataPoints.slice(\n            offset,\n            offset + take\n          ) as DataPoint<unknown>[],\n        } as typeof metric;\n        scopeMetricCopy.metrics.push(metricCopy);\n        offset += take;\n        currentBatchPoints += take;\n        // If the current batch is full, flush it and start a new one\n        if (currentBatchPoints === maxExportBatchSize) {\n          flush();\n          scopeMetricCopy = null; // Force recreation of scope copy in the next batch\n        }\n      }\n    }\n  }\n\n  flush();\n  return batches;\n}\n"],"mappings":";;;;;;;;AAcA,SAAgB,gBACd,iBACA,oBACmB;CACnB,IAAI,CAAC,OAAO,UAAU,kBAAkB,KAAK,sBAAsB,GACjE,MAAM,IAAI,MAAM,+CAA+C;CAEjE,MAAM,UAA6B,CAAC;CACpC,IAAI,qBAAqB;CACzB,IAAI,sBAAsC,CAAC;CAE3C,SAAS,QAAQ;EACf,IAAI,oBAAoB,SAAS,GAAG;GAClC,QAAQ,KAAK;IACX,UAAU,gBAAgB;IAC1B,cAAc;GAChB,CAAC;GACD,sBAAsB,CAAC;GACvB,qBAAqB;EACvB;CACF;CAGA,KAAK,MAAM,eAAe,gBAAgB,cAAc;EACtD,IAAI,kBAAuC;EAE3C,KAAK,MAAM,UAAU,YAAY,SAAS;GACxC,MAAM,aAAa,OAAO;GAE1B,IAAI,WAAW,WAAW,GAAG;IAC3B,IAAI,CAAC,iBAAiB;KACpB,kBAAkB;MAAE,OAAO,YAAY;MAAO,SAAS,CAAC;KAAE;KAC1D,oBAAoB,KAAK,eAAe;IAC1C;IACA,gBAAgB,QAAQ,KAAK,MAAM;IACnC;GACF;GAIA,IAAI,SAAS;GACb,OAAO,SAAS,WAAW,QAAQ;IACjC,MAAM,YAAY,qBAAqB;IACvC,MAAM,OAAO,KAAK,IAAI,WAAW,WAAW,SAAS,MAAM;IAE3D,IAAI,CAAC,iBAAiB;KACpB,kBAAkB;MAAE,OAAO,YAAY;MAAO,SAAS,CAAC;KAAE;KAC1D,oBAAoB,KAAK,eAAe;IAC1C;IAKA,MAAM,aAAa;KACjB,GAAG;KACH,YAAY,WAAW,MACrB,QACA,SAAS,IACX;IACF;IACA,gBAAgB,QAAQ,KAAK,UAAU;IACvC,UAAU;IACV,sBAAsB;IAEtB,IAAI,uBAAuB,oBAAoB;KAC7C,MAAM;KACN,kBAAkB;IACpB;GACF;EACF;CACF;CAEA,MAAM;CACN,OAAO;AACT"}