export interface AlarmPoint { ts: number value: number } export interface WindowChangeAlarm { change: number direction: 'increase' | 'decrease' fromValue: number fromTs: number toTs: number toValue: number } export function getWindowChangeAlarm( points: AlarmPoint[], anchorTs: number, windowSeconds: number, range: number ): WindowChangeAlarm | undefined { if (windowSeconds <= 0 || range < 0) return undefined const startTs = anchorTs - windowSeconds * 1000 const windowPoints = points .filter((point) => point.ts > startTs && point.ts <= anchorTs) .sort((left, right) => left.ts - right.ts) let alarm: WindowChangeAlarm | undefined for (let index = 1; index < windowPoints.length; index += 1) { const previousPoint = windowPoints[index - 1] const currentPoint = windowPoints[index] const delta = currentPoint.value - previousPoint.value const change = Math.abs(delta) if (change <= range) continue alarm = { change, direction: delta > 0 ? 'increase' : 'decrease', fromValue: previousPoint.value, fromTs: previousPoint.ts, toTs: currentPoint.ts, toValue: currentPoint.value } } return alarm }