deviceStatusStats.test.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * 票 133:设备状态统计池的唯一计数入口。
  3. *
  4. * 钉住三条规则(CONTEXT.md「设备预警/报警统计」「图例入桶规则」):
  5. * 1. 报警/预警/工单是**可叠加标签**,一台设备可同时进多个池,池之和可大于台数;
  6. * 2. 在线/离线是**互斥**基础属性,只按台账状态入桶,不用「总数减离线」倒推;
  7. * 3. 缺状态/认不出的状态单列 UNKNOWN_STATE,不并入在线。
  8. */
  9. import assert from 'node:assert/strict'
  10. import { test } from 'node:test'
  11. import { deviceStatusStats, DEVICE_POOL_KEYS } from '../src/modules/cockpit/domain/deviceStatusStats.js'
  12. import { toMonitoringOverview, operationOverviewPanel } from '../src/modules/cockpit/domain/monitoringPanels.js'
  13. import { deviceStatusBreakdown, DEVICE_BREAKDOWN_KEYS } from '../src/modules/cockpit/domain/deviceStatusStats.js'
  14. test('标签可叠加:一台设备可同时进报警/预警/工单三个池', () => {
  15. const { available, total, counts } = deviceStatusStats([
  16. { code: 'D1', name: '一号', status: 'ONLINE', tags: ['ALARM', 'WARNING', 'WORK_ORDER'] },
  17. { code: 'D2', status: 'ONLINE', tags: ['ALARM'] },
  18. { code: 'D3', status: 'OFFLINE', tags: ['WARNING'] },
  19. ])
  20. assert.equal(available, true)
  21. assert.equal(total, 3)
  22. assert.equal(counts.ALARM, 2)
  23. assert.equal(counts.WARNING, 2)
  24. assert.equal(counts.WORK_ORDER, 1)
  25. // 在线/离线互斥,且与标签无关(离线设备照样可以带预警标签)
  26. assert.equal(counts.NORMAL, 2)
  27. assert.equal(counts.OFFLINE, 1)
  28. assert.equal(counts.UNKNOWN_STATE, 0)
  29. // 池之和允许大于台数 —— 这是规则本身,不是重复计数
  30. assert.ok(counts.ALARM + counts.WARNING + counts.WORK_ORDER > total)
  31. })
  32. test('在线/离线只按台账状态入桶;缺状态与认不出的状态单列未知', () => {
  33. const { counts } = deviceStatusStats([
  34. { code: 'A', status: 'ONLINE', tags: [] },
  35. { code: 'B', status: 'OFFLINE', tags: [] },
  36. { code: 'C', status: '', tags: [] },
  37. { code: 'D', tags: [] },
  38. { code: 'E', status: 'STALE', tags: [] },
  39. ])
  40. assert.equal(counts.NORMAL, 1)
  41. assert.equal(counts.OFFLINE, 1)
  42. assert.equal(counts.UNKNOWN_STATE, 3, '缺状态不得并入在线')
  43. })
  44. test('标签大小写归一、重复只计一次、认不出的丢弃', () => {
  45. const { counts } = deviceStatusStats([
  46. { code: 'X', status: 'ONLINE', tags: ['alarm', 'ALARM', 'ALARM', 'UNASSOCIATED', 'nonsense'] },
  47. ])
  48. assert.equal(counts.ALARM, 1)
  49. assert.equal(counts.WARNING, 0)
  50. assert.equal(counts.WORK_ORDER, 0)
  51. assert.equal(counts.NORMAL, 1)
  52. })
  53. test('空台账 → available:false,不补 0', () => {
  54. assert.deepEqual(deviceStatusStats([]), { available: false, total: null, counts: {}, devices: [] })
  55. assert.deepEqual(deviceStatusStats(null), { available: false, total: null, counts: {}, devices: [] })
  56. })
  57. test('逐设备事实供详情/筛选消费:deviceCode + base + tags', () => {
  58. const { devices } = deviceStatusStats([
  59. { code: 'D9', name: '九号', status: 'OFFLINE', tags: ['WARNING'] },
  60. ])
  61. assert.deepEqual(devices, [{ deviceCode: 'D9', deviceName: '九号', base: 'OFFLINE', tags: ['WARNING'] }])
  62. })
  63. test('计数桶顺序固定(环图与图例共用一份行序)', () => {
  64. assert.deepEqual(DEVICE_POOL_KEYS, ['ALARM', 'WARNING', 'WORK_ORDER', 'NORMAL', 'OFFLINE'])
  65. })
  66. test('适配层把后端 tags 带进 devices,计数函数才算得出报警/预警(防「映射漏字段」假绿)', () => {
  67. const raw = {
  68. counts: { equipmentCount: 3 },
  69. devices: [
  70. { deviceCode: 'A', deviceName: '甲', monitoringState: 'ONLINE', tags: ['ALARM'] },
  71. { deviceCode: 'B', monitoringState: 'OFFLINE', tags: ['ALARM', 'WARNING'] },
  72. { deviceCode: 'C', monitoringState: 'ONLINE', tags: [] },
  73. { deviceCode: 'D', monitoringState: 'ONLINE' },
  74. ],
  75. }
  76. const domain = toMonitoringOverview(raw)
  77. assert.deepEqual(domain.devices.map((device) => device.tags), [['ALARM'], ['ALARM', 'WARNING'], [], []],
  78. '缺失 tags 必须落空数组,而不是 undefined')
  79. const { counts } = deviceStatusStats(domain.devices)
  80. assert.equal(counts.ALARM, 2)
  81. assert.equal(counts.WARNING, 1)
  82. assert.equal(counts.NORMAL, 3)
  83. assert.equal(counts.OFFLINE, 1)
  84. })
  85. test('主状态互斥划分:离线+报警归报警,扇区合计恒等于台数', () => {
  86. const { available, total, counts } = deviceStatusBreakdown([
  87. { code: 'A', status: 'OFFLINE', tags: ['ALARM'] }, // 离线+报警 → 报警(旧指标六态会并进离线)
  88. { code: 'B', status: 'ONLINE', tags: ['ALARM', 'WARNING'] }, // 报警优先
  89. { code: 'C', status: 'ONLINE', tags: ['WORK_ORDER', 'WARNING'] }, // 工单优先于预警
  90. { code: 'D', status: 'ONLINE', tags: ['WARNING'] },
  91. { code: 'E', status: 'ONLINE', tags: [] },
  92. { code: 'F', status: 'OFFLINE', tags: [] },
  93. { code: 'G', tags: [] },
  94. ])
  95. assert.equal(available, true)
  96. assert.equal(total, 7)
  97. assert.equal(counts.ALARM, 2)
  98. assert.equal(counts.WORK_ORDER, 1)
  99. assert.equal(counts.WARNING, 1)
  100. assert.equal(counts.NORMAL, 1)
  101. assert.equal(counts.OFFLINE, 1)
  102. assert.equal(counts.UNKNOWN, 1)
  103. assert.equal(Object.values(counts).reduce((a, b) => a + b, 0), total, '互斥划分合计必须等于台数')
  104. assert.deepEqual(DEVICE_BREAKDOWN_KEYS, ['ALARM', 'WORK_ORDER', 'WARNING', 'NORMAL', 'OFFLINE', 'UNKNOWN'])
  105. })
  106. test('运行概况六态环:报警=16(含离线报警)、合计=395,且与 KPI 的池口径分开', () => {
  107. const devices = []
  108. // 16 台报警,其中 4 台离线——旧指标六态只数到 12
  109. for (let i = 0; i < 16; i += 1) {
  110. devices.push({ deviceCode: 'AL' + i, status: i < 4 ? 'OFFLINE' : 'ONLINE', tags: ['ALARM'] })
  111. }
  112. devices.push({ deviceCode: 'W1', status: 'ONLINE', tags: ['WARNING'] })
  113. devices.push({ deviceCode: 'W2', status: 'ONLINE', tags: ['WARNING', 'WORK_ORDER'] })
  114. for (let i = 0; i < 244; i += 1) devices.push({ deviceCode: 'N' + i, status: 'ONLINE', tags: [] })
  115. for (let i = 0; i < 133; i += 1) devices.push({ deviceCode: 'O' + i, status: 'OFFLINE', tags: [] })
  116. assert.equal(devices.length, 395)
  117. const pools = deviceStatusStats(devices)
  118. const view = operationOverviewPanel(
  119. { metricStatuses: [], abnormalEquipment: [], pagination: { total: 0 } },
  120. {},
  121. { devices: [], deviceStatusPools: pools },
  122. )
  123. const donut = view.blocks.find((block) => String(block.label || '').includes('按设备主状态'))
  124. assert.ok(donut, '六态环块必须存在(标题写明按设备主状态)')
  125. const sum = donut.rows.reduce((n, row) => n + (Number(row.count) || 0), 0)
  126. assert.equal(sum, 395, '扇区互斥,合计必须等于设备台数')
  127. const alarm = donut.rows.find((row) => row.key === 'ALARM')
  128. assert.equal(alarm.count, 16, '离线+报警也必须归报警')
  129. // 归一后的 fact 只有 base 没有 status——必须仍能分出正常/离线,不能全落「未发布」
  130. assert.equal(donut.rows.find((row) => row.key === 'NORMAL').count, 244)
  131. assert.equal(donut.rows.find((row) => row.key === 'OFFLINE').count, 133)
  132. assert.equal(donut.rows.find((row) => row.key === 'UNKNOWN').count, 0)
  133. })