deviceStatusStats.test.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. test('标签可叠加:一台设备可同时进报警/预警/工单三个池', () => {
  14. const { available, total, counts } = deviceStatusStats([
  15. { code: 'D1', name: '一号', status: 'ONLINE', tags: ['ALARM', 'WARNING', 'WORK_ORDER'] },
  16. { code: 'D2', status: 'ONLINE', tags: ['ALARM'] },
  17. { code: 'D3', status: 'OFFLINE', tags: ['WARNING'] },
  18. ])
  19. assert.equal(available, true)
  20. assert.equal(total, 3)
  21. assert.equal(counts.ALARM, 2)
  22. assert.equal(counts.WARNING, 2)
  23. assert.equal(counts.WORK_ORDER, 1)
  24. // 在线/离线互斥,且与标签无关(离线设备照样可以带预警标签)
  25. assert.equal(counts.NORMAL, 2)
  26. assert.equal(counts.OFFLINE, 1)
  27. assert.equal(counts.UNKNOWN_STATE, 0)
  28. // 池之和允许大于台数 —— 这是规则本身,不是重复计数
  29. assert.ok(counts.ALARM + counts.WARNING + counts.WORK_ORDER > total)
  30. })
  31. test('在线/离线只按台账状态入桶;缺状态与认不出的状态单列未知', () => {
  32. const { counts } = deviceStatusStats([
  33. { code: 'A', status: 'ONLINE', tags: [] },
  34. { code: 'B', status: 'OFFLINE', tags: [] },
  35. { code: 'C', status: '', tags: [] },
  36. { code: 'D', tags: [] },
  37. { code: 'E', status: 'STALE', tags: [] },
  38. ])
  39. assert.equal(counts.NORMAL, 1)
  40. assert.equal(counts.OFFLINE, 1)
  41. assert.equal(counts.UNKNOWN_STATE, 3, '缺状态不得并入在线')
  42. })
  43. test('标签大小写归一、重复只计一次、认不出的丢弃', () => {
  44. const { counts } = deviceStatusStats([
  45. { code: 'X', status: 'ONLINE', tags: ['alarm', 'ALARM', 'ALARM', 'UNASSOCIATED', 'nonsense'] },
  46. ])
  47. assert.equal(counts.ALARM, 1)
  48. assert.equal(counts.WARNING, 0)
  49. assert.equal(counts.WORK_ORDER, 0)
  50. assert.equal(counts.NORMAL, 1)
  51. })
  52. test('空台账 → available:false,不补 0', () => {
  53. assert.deepEqual(deviceStatusStats([]), { available: false, total: null, counts: {}, devices: [] })
  54. assert.deepEqual(deviceStatusStats(null), { available: false, total: null, counts: {}, devices: [] })
  55. })
  56. test('逐设备事实供详情/筛选消费:deviceCode + base + tags', () => {
  57. const { devices } = deviceStatusStats([
  58. { code: 'D9', name: '九号', status: 'OFFLINE', tags: ['WARNING'] },
  59. ])
  60. assert.deepEqual(devices, [{ deviceCode: 'D9', deviceName: '九号', base: 'OFFLINE', tags: ['WARNING'] }])
  61. })
  62. test('计数桶顺序固定(环图与图例共用一份行序)', () => {
  63. assert.deepEqual(DEVICE_POOL_KEYS, ['ALARM', 'WARNING', 'WORK_ORDER', 'NORMAL', 'OFFLINE'])
  64. })
  65. test('适配层把后端 tags 带进 devices,计数函数才算得出报警/预警(防「映射漏字段」假绿)', () => {
  66. const raw = {
  67. counts: { equipmentCount: 3 },
  68. devices: [
  69. { deviceCode: 'A', deviceName: '甲', monitoringState: 'ONLINE', tags: ['ALARM'] },
  70. { deviceCode: 'B', monitoringState: 'OFFLINE', tags: ['ALARM', 'WARNING'] },
  71. { deviceCode: 'C', monitoringState: 'ONLINE', tags: [] },
  72. { deviceCode: 'D', monitoringState: 'ONLINE' },
  73. ],
  74. }
  75. const domain = toMonitoringOverview(raw)
  76. assert.deepEqual(domain.devices.map((device) => device.tags), [['ALARM'], ['ALARM', 'WARNING'], [], []],
  77. '缺失 tags 必须落空数组,而不是 undefined')
  78. const { counts } = deviceStatusStats(domain.devices)
  79. assert.equal(counts.ALARM, 2)
  80. assert.equal(counts.WARNING, 1)
  81. assert.equal(counts.NORMAL, 3)
  82. assert.equal(counts.OFFLINE, 1)
  83. })
  84. test('运行概况:一个「设备状态」块——环给在线/离线,可叠加标签作为细条并入,末尾「本数 / 395 台」', () => {
  85. const devices = []
  86. // 16 台报警,其中 4 台离线——旧指标六态只数到 12
  87. for (let i = 0; i < 16; i += 1) {
  88. devices.push({ deviceCode: 'AL' + i, status: i < 4 ? 'OFFLINE' : 'ONLINE', tags: ['ALARM'] })
  89. }
  90. devices.push({ deviceCode: 'W1', status: 'ONLINE', tags: ['WARNING'] })
  91. devices.push({ deviceCode: 'W2', status: 'ONLINE', tags: ['WARNING', 'WORK_ORDER'] })
  92. for (let i = 0; i < 244; i += 1) devices.push({ deviceCode: 'N' + i, status: 'ONLINE', tags: [] })
  93. for (let i = 0; i < 133; i += 1) devices.push({ deviceCode: 'O' + i, status: 'OFFLINE', tags: [] })
  94. assert.equal(devices.length, 395)
  95. const view = operationOverviewPanel(
  96. { metricStatuses: [], abnormalEquipment: [], pagination: { total: 0 } },
  97. {},
  98. { devices: [], deviceStatusPools: deviceStatusStats(devices) },
  99. )
  100. // 只有一个块承载这件事:标题就叫「设备状态」(不再有「在线 / 离线」或「业务标签」副标题)
  101. const stateBlocks = view.blocks.filter((block) => block.kind === 'donut' || block.kind === 'bars')
  102. assert.equal(stateBlocks.length, 1, '环与细条必须在同一个块里')
  103. const block = stateBlocks[0]
  104. assert.equal(block.label, '设备状态')
  105. assert.equal(block.total, 395, '环心读数=设备总数')
  106. assert.equal(block.rows.find((row) => row.label === '在线').count, 258)
  107. assert.equal(block.rows.find((row) => row.label === '离线').count, 137)
  108. assert.ok(block.bars && block.bars.rows.length === 3, '并入三条可叠加标签细条')
  109. assert.equal(block.bars.total, 395, '细条满宽=395 台(末端读数 16 / 395 台)')
  110. const byLabel = Object.fromEntries(block.bars.rows.map((row) => [row.label, row]))
  111. assert.equal(byLabel['报警'].count, 16)
  112. assert.equal(byLabel['预警'].count, 2)
  113. assert.equal(byLabel['工单'].count, 1)
  114. assert.ok(Math.abs(byLabel['报警'].ratio - 16 / 395) < 1e-9, '条长应按 395 归一,而不是按本块最大值')
  115. })