thresholdAlarmWindow.test.mjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import assert from 'node:assert/strict'
  2. import { test } from 'node:test'
  3. import { getWindowChangeAlarm } from '../src/utils/thresholdAlarmWindow.ts'
  4. const second = (value) => value * 1000
  5. test('最近 10 秒内相邻数据上涨幅度大于超限范围时告警', () => {
  6. const points = [
  7. { ts: second(47), value: 800 },
  8. { ts: second(48), value: 901 },
  9. { ts: second(55), value: 901 }
  10. ]
  11. assert.deepEqual(getWindowChangeAlarm(points, second(55), 10, 100), {
  12. change: 101,
  13. direction: 'increase',
  14. fromValue: 800,
  15. fromTs: second(47),
  16. toTs: second(48),
  17. toValue: 901
  18. })
  19. })
  20. test('最近 10 秒内相邻数据下降幅度大于超限范围时同样告警', () => {
  21. const points = [
  22. { ts: second(47), value: 901 },
  23. { ts: second(48), value: 800 },
  24. { ts: second(55), value: 800 }
  25. ]
  26. assert.deepEqual(getWindowChangeAlarm(points, second(55), 10, 100), {
  27. change: 101,
  28. direction: 'decrease',
  29. fromValue: 901,
  30. fromTs: second(47),
  31. toTs: second(48),
  32. toValue: 800
  33. })
  34. })
  35. test('相邻数据间隔不固定时仍按相邻变化量判断', () => {
  36. const points = [
  37. { ts: second(46), value: 800 },
  38. { ts: second(47), value: 850 },
  39. { ts: second(49), value: 951 },
  40. { ts: second(53), value: 970 },
  41. { ts: second(55), value: 970 }
  42. ]
  43. assert.deepEqual(getWindowChangeAlarm(points, second(55), 10, 100), {
  44. change: 101,
  45. direction: 'increase',
  46. fromValue: 850,
  47. fromTs: second(47),
  48. toTs: second(49),
  49. toValue: 951
  50. })
  51. })
  52. test('相邻数据未超限时,即使窗口累计变化超限也不告警', () => {
  53. const points = [
  54. { ts: second(46), value: 850 },
  55. { ts: second(47), value: 900 },
  56. { ts: second(48), value: 950 },
  57. { ts: second(55), value: 950 }
  58. ]
  59. assert.equal(getWindowChangeAlarm(points, second(55), 10, 100), undefined)
  60. })
  61. test('窗口内有多次超限时返回最新一组相邻变化', () => {
  62. const points = [
  63. { ts: second(47), value: 800 },
  64. { ts: second(48), value: 901 },
  65. { ts: second(50), value: 800 },
  66. { ts: second(51), value: 650 },
  67. { ts: second(55), value: 650 }
  68. ]
  69. assert.deepEqual(getWindowChangeAlarm(points, second(55), 10, 100), {
  70. change: 150,
  71. direction: 'decrease',
  72. fromValue: 800,
  73. fromTs: second(50),
  74. toTs: second(51),
  75. toValue: 650
  76. })
  77. })
  78. test('窗口外的数据不参与幅度计算', () => {
  79. const points = [
  80. { ts: second(45), value: 1000 },
  81. { ts: second(46), value: 800 },
  82. { ts: second(55), value: 800 }
  83. ]
  84. assert.equal(getWindowChangeAlarm(points, second(55), 10, 100), undefined)
  85. })
  86. test('窗口随最新数据右移,窗口外的相邻超限退出后告警解除', () => {
  87. const points = [
  88. { ts: second(46), value: 800 },
  89. { ts: second(47), value: 901 },
  90. { ts: second(56), value: 900 }
  91. ]
  92. assert.equal(getWindowChangeAlarm(points, second(56), 10, 100), undefined)
  93. })
  94. test('范围边界相等时不触发', () => {
  95. const points = [
  96. { ts: second(54), value: 800 },
  97. { ts: second(55), value: 900 }
  98. ]
  99. assert.equal(getWindowChangeAlarm(points, second(55), 10, 100), undefined)
  100. })
  101. test('乱序数据中晚于当前时间的点不参与检查', () => {
  102. const points = [
  103. { ts: second(54), value: 800 },
  104. { ts: second(55), value: 900 },
  105. { ts: second(56), value: 1000 }
  106. ]
  107. assert.equal(getWindowChangeAlarm(points, second(55), 10, 100), undefined)
  108. })
  109. test('窗口内只有一个数据点时不触发', () => {
  110. const points = [{ ts: second(55), value: 800 }]
  111. assert.equal(getWindowChangeAlarm(points, second(55), 10, 0), undefined)
  112. })