| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- /**
- * 票 133:设备状态统计池的唯一计数入口。
- *
- * 钉住三条规则(CONTEXT.md「设备预警/报警统计」「图例入桶规则」):
- * 1. 报警/预警/工单是**可叠加标签**,一台设备可同时进多个池,池之和可大于台数;
- * 2. 在线/离线是**互斥**基础属性,只按台账状态入桶,不用「总数减离线」倒推;
- * 3. 缺状态/认不出的状态单列 UNKNOWN_STATE,不并入在线。
- */
- import assert from 'node:assert/strict'
- import { test } from 'node:test'
- import { deviceStatusStats, DEVICE_POOL_KEYS } from '../src/modules/cockpit/domain/deviceStatusStats.js'
- import { toMonitoringOverview, operationOverviewPanel } from '../src/modules/cockpit/domain/monitoringPanels.js'
- import { deviceStatusBreakdown, DEVICE_BREAKDOWN_KEYS } from '../src/modules/cockpit/domain/deviceStatusStats.js'
- test('标签可叠加:一台设备可同时进报警/预警/工单三个池', () => {
- const { available, total, counts } = deviceStatusStats([
- { code: 'D1', name: '一号', status: 'ONLINE', tags: ['ALARM', 'WARNING', 'WORK_ORDER'] },
- { code: 'D2', status: 'ONLINE', tags: ['ALARM'] },
- { code: 'D3', status: 'OFFLINE', tags: ['WARNING'] },
- ])
- assert.equal(available, true)
- assert.equal(total, 3)
- assert.equal(counts.ALARM, 2)
- assert.equal(counts.WARNING, 2)
- assert.equal(counts.WORK_ORDER, 1)
- // 在线/离线互斥,且与标签无关(离线设备照样可以带预警标签)
- assert.equal(counts.NORMAL, 2)
- assert.equal(counts.OFFLINE, 1)
- assert.equal(counts.UNKNOWN_STATE, 0)
- // 池之和允许大于台数 —— 这是规则本身,不是重复计数
- assert.ok(counts.ALARM + counts.WARNING + counts.WORK_ORDER > total)
- })
- test('在线/离线只按台账状态入桶;缺状态与认不出的状态单列未知', () => {
- const { counts } = deviceStatusStats([
- { code: 'A', status: 'ONLINE', tags: [] },
- { code: 'B', status: 'OFFLINE', tags: [] },
- { code: 'C', status: '', tags: [] },
- { code: 'D', tags: [] },
- { code: 'E', status: 'STALE', tags: [] },
- ])
- assert.equal(counts.NORMAL, 1)
- assert.equal(counts.OFFLINE, 1)
- assert.equal(counts.UNKNOWN_STATE, 3, '缺状态不得并入在线')
- })
- test('标签大小写归一、重复只计一次、认不出的丢弃', () => {
- const { counts } = deviceStatusStats([
- { code: 'X', status: 'ONLINE', tags: ['alarm', 'ALARM', 'ALARM', 'UNASSOCIATED', 'nonsense'] },
- ])
- assert.equal(counts.ALARM, 1)
- assert.equal(counts.WARNING, 0)
- assert.equal(counts.WORK_ORDER, 0)
- assert.equal(counts.NORMAL, 1)
- })
- test('空台账 → available:false,不补 0', () => {
- assert.deepEqual(deviceStatusStats([]), { available: false, total: null, counts: {}, devices: [] })
- assert.deepEqual(deviceStatusStats(null), { available: false, total: null, counts: {}, devices: [] })
- })
- test('逐设备事实供详情/筛选消费:deviceCode + base + tags', () => {
- const { devices } = deviceStatusStats([
- { code: 'D9', name: '九号', status: 'OFFLINE', tags: ['WARNING'] },
- ])
- assert.deepEqual(devices, [{ deviceCode: 'D9', deviceName: '九号', base: 'OFFLINE', tags: ['WARNING'] }])
- })
- test('计数桶顺序固定(环图与图例共用一份行序)', () => {
- assert.deepEqual(DEVICE_POOL_KEYS, ['ALARM', 'WARNING', 'WORK_ORDER', 'NORMAL', 'OFFLINE'])
- })
- test('适配层把后端 tags 带进 devices,计数函数才算得出报警/预警(防「映射漏字段」假绿)', () => {
- const raw = {
- counts: { equipmentCount: 3 },
- devices: [
- { deviceCode: 'A', deviceName: '甲', monitoringState: 'ONLINE', tags: ['ALARM'] },
- { deviceCode: 'B', monitoringState: 'OFFLINE', tags: ['ALARM', 'WARNING'] },
- { deviceCode: 'C', monitoringState: 'ONLINE', tags: [] },
- { deviceCode: 'D', monitoringState: 'ONLINE' },
- ],
- }
- const domain = toMonitoringOverview(raw)
- assert.deepEqual(domain.devices.map((device) => device.tags), [['ALARM'], ['ALARM', 'WARNING'], [], []],
- '缺失 tags 必须落空数组,而不是 undefined')
- const { counts } = deviceStatusStats(domain.devices)
- assert.equal(counts.ALARM, 2)
- assert.equal(counts.WARNING, 1)
- assert.equal(counts.NORMAL, 3)
- assert.equal(counts.OFFLINE, 1)
- })
- test('主状态互斥划分:离线+报警归报警,扇区合计恒等于台数', () => {
- const { available, total, counts } = deviceStatusBreakdown([
- { code: 'A', status: 'OFFLINE', tags: ['ALARM'] }, // 离线+报警 → 报警(旧指标六态会并进离线)
- { code: 'B', status: 'ONLINE', tags: ['ALARM', 'WARNING'] }, // 报警优先
- { code: 'C', status: 'ONLINE', tags: ['WORK_ORDER', 'WARNING'] }, // 工单优先于预警
- { code: 'D', status: 'ONLINE', tags: ['WARNING'] },
- { code: 'E', status: 'ONLINE', tags: [] },
- { code: 'F', status: 'OFFLINE', tags: [] },
- { code: 'G', tags: [] },
- ])
- assert.equal(available, true)
- assert.equal(total, 7)
- assert.equal(counts.ALARM, 2)
- assert.equal(counts.WORK_ORDER, 1)
- assert.equal(counts.WARNING, 1)
- assert.equal(counts.NORMAL, 1)
- assert.equal(counts.OFFLINE, 1)
- assert.equal(counts.UNKNOWN, 1)
- assert.equal(Object.values(counts).reduce((a, b) => a + b, 0), total, '互斥划分合计必须等于台数')
- assert.deepEqual(DEVICE_BREAKDOWN_KEYS, ['ALARM', 'WORK_ORDER', 'WARNING', 'NORMAL', 'OFFLINE', 'UNKNOWN'])
- })
- test('运行概况六态环:报警=16(含离线报警)、合计=395,且与 KPI 的池口径分开', () => {
- const devices = []
- // 16 台报警,其中 4 台离线——旧指标六态只数到 12
- for (let i = 0; i < 16; i += 1) {
- devices.push({ deviceCode: 'AL' + i, status: i < 4 ? 'OFFLINE' : 'ONLINE', tags: ['ALARM'] })
- }
- devices.push({ deviceCode: 'W1', status: 'ONLINE', tags: ['WARNING'] })
- devices.push({ deviceCode: 'W2', status: 'ONLINE', tags: ['WARNING', 'WORK_ORDER'] })
- for (let i = 0; i < 244; i += 1) devices.push({ deviceCode: 'N' + i, status: 'ONLINE', tags: [] })
- for (let i = 0; i < 133; i += 1) devices.push({ deviceCode: 'O' + i, status: 'OFFLINE', tags: [] })
- assert.equal(devices.length, 395)
- const pools = deviceStatusStats(devices)
- const view = operationOverviewPanel(
- { metricStatuses: [], abnormalEquipment: [], pagination: { total: 0 } },
- {},
- { devices: [], deviceStatusPools: pools },
- )
- const donut = view.blocks.find((block) => String(block.label || '').includes('按设备主状态'))
- assert.ok(donut, '六态环块必须存在(标题写明按设备主状态)')
- const sum = donut.rows.reduce((n, row) => n + (Number(row.count) || 0), 0)
- assert.equal(sum, 395, '扇区互斥,合计必须等于设备台数')
- const alarm = donut.rows.find((row) => row.key === 'ALARM')
- assert.equal(alarm.count, 16, '离线+报警也必须归报警')
- // 归一后的 fact 只有 base 没有 status——必须仍能分出正常/离线,不能全落「未发布」
- assert.equal(donut.rows.find((row) => row.key === 'NORMAL').count, 244)
- assert.equal(donut.rows.find((row) => row.key === 'OFFLINE').count, 133)
- assert.equal(donut.rows.find((row) => row.key === 'UNKNOWN').count, 0)
- })
|