thresholdAlarmWindow.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. export interface AlarmPoint {
  2. ts: number
  3. value: number
  4. }
  5. export interface WindowChangeAlarm {
  6. change: number
  7. direction: 'increase' | 'decrease'
  8. fromValue: number
  9. fromTs: number
  10. toTs: number
  11. toValue: number
  12. }
  13. export function getWindowChangeAlarm(
  14. points: AlarmPoint[],
  15. anchorTs: number,
  16. windowSeconds: number,
  17. range: number
  18. ): WindowChangeAlarm | undefined {
  19. if (windowSeconds <= 0 || range < 0) return undefined
  20. const startTs = anchorTs - windowSeconds * 1000
  21. const windowPoints = points
  22. .filter((point) => point.ts > startTs && point.ts <= anchorTs)
  23. .sort((left, right) => left.ts - right.ts)
  24. let alarm: WindowChangeAlarm | undefined
  25. for (let index = 1; index < windowPoints.length; index += 1) {
  26. const previousPoint = windowPoints[index - 1]
  27. const currentPoint = windowPoints[index]
  28. const delta = currentPoint.value - previousPoint.value
  29. const change = Math.abs(delta)
  30. if (change <= range) continue
  31. alarm = {
  32. change,
  33. direction: delta > 0 ? 'increase' : 'decrease',
  34. fromValue: previousPoint.value,
  35. fromTs: previousPoint.ts,
  36. toTs: currentPoint.ts,
  37. toValue: currentPoint.value
  38. }
  39. }
  40. return alarm
  41. }