| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- /**
- * 票 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'
- 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('运行概况:一个「设备状态」块——环给在线/离线,可叠加标签作为细条并入,末尾「本数 / 395 台」', () => {
- 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 view = operationOverviewPanel(
- { metricStatuses: [], abnormalEquipment: [], pagination: { total: 0 } },
- {},
- { devices: [], deviceStatusPools: deviceStatusStats(devices) },
- )
- // 只有一个块承载这件事:标题就叫「设备状态」(不再有「在线 / 离线」或「业务标签」副标题)
- const stateBlocks = view.blocks.filter((block) => block.kind === 'donut' || block.kind === 'bars')
- assert.equal(stateBlocks.length, 1, '环与细条必须在同一个块里')
- const block = stateBlocks[0]
- assert.equal(block.label, '设备状态')
- assert.equal(block.total, 395, '环心读数=设备总数')
- assert.equal(block.rows.find((row) => row.label === '在线').count, 258)
- assert.equal(block.rows.find((row) => row.label === '离线').count, 137)
- assert.ok(block.bars && block.bars.rows.length === 3, '并入三条可叠加标签细条')
- assert.equal(block.bars.total, 395, '细条满宽=395 台(末端读数 16 / 395 台)')
- const byLabel = Object.fromEntries(block.bars.rows.map((row) => [row.label, row]))
- assert.equal(byLabel['报警'].count, 16)
- assert.equal(byLabel['预警'].count, 2)
- assert.equal(byLabel['工单'].count, 1)
- assert.ok(Math.abs(byLabel['报警'].ratio - 16 / 395) < 1e-9, '条长应按 395 归一,而不是按本块最大值')
- })
|