TypeScript Analytics API aggregation dropping trend arrays and throwing NaN on efficiency ratios

genesys-cloud-node-sdk handles the initial fetch, but the interval aggregation is where things get messy. Step one is pulling from /api/v2/analytics/agents/summary with interval: “PT15M” to grab handling time and wrap-up durations. We aggregate per agent and queue first. Then we strip out non-productive time using a quick filter like record.wrapUpTime > 300 ? 0 : record.wrapUpTime. The next step calculates efficiency ratios against a target threshold of 75%.

The outlier detection uses a basic z-score calculation in TypeScript. Something along the lines of zScore = (val - mean) / stdDev. If it crosses 1.5, we flag it. Generating utilization trends over configurable windows means we have to handle missing data points with interpolation. The function just checks if (!bucket.value) bucket.value = (prev + next) / 2. We’ve got a REST endpoint wrapping all this at /internal/metrics/utilization, but the response payload keeps dropping the trend array when the window shifts past midnight.

Here’s the aggregation loop we’re running: data.records.reduce((acc, rec) => { acc[rec.agentId] = (acc[rec.agentId] || 0) + (rec.handlingTime - Math.min(rec.wrapUpTime, 300)); return acc; }, {})

The API returns sparse arrays on low-volume queues. We’re getting NaN in the efficiency ratio when the denominator drops to zero. The dashboard integration expects a flat JSON object with agentId, utilizationPct, and trendLine. The interpolation keeps dropping values anyway.

{
 "agentId": "12345",
 "utilizationPct": null,
 "trendLine": [],
 "error": "Missing data points in bucket range"
}