Просмотр исходного кода

fix(cockpit): 历史行同时同步当前事件类型码

ChenJZ 7 часов назад
Родитель
Сommit
84f0c808bb
2 измененных файлов с 54 добавлено и 9 удалено
  1. 46 8
      src/modules/cockpit/stores/eventStore.js
  2. 8 1
      tests/eventStatusMerge.test.js

+ 46 - 8
src/modules/cockpit/stores/eventStore.js

@@ -94,22 +94,40 @@ function initialSections() {
     }
 }
 
-/** 当前事件分区是本次请求里最新的一份状态事实;历史/效率行只借用它的 status 字段。 */
-function currentStatusByCode(current) {
+const currentDomainEvents = (current) => {
+    const data = current && current.data
+    if (!data) return []
+    if (Array.isArray(data.events)) return data.events
+    const sectionEvents = data.sections && data.sections.events && data.sections.events.data
+    return Array.isArray(sectionEvents) ? sectionEvents : []
+}
+
+const eventCodeOf = (event) => event && (event.codeText || event.code || event.eventCode || event.eventNo)
+
+export function currentStatusByCode(current) {
     const result = new Map()
-    const events = current && current.data && Array.isArray(current.data.events) ? current.data.events : []
-    for (const event of events) {
-        const code = event && (event.eventCode || event.eventNo)
+    for (const event of currentDomainEvents(current)) {
+        const code = eventCodeOf(event)
         if (code && event.status) result.set(String(code), event.status)
     }
     return result
 }
 
+export function currentTypeByCode(current) {
+    const result = new Map()
+    for (const event of currentDomainEvents(current)) {
+        const code = eventCodeOf(event)
+        const type = event.type || event.eventType
+        if (code && type) result.set(String(code), type)
+    }
+    return result
+}
+
 export function mergeRowsStatus(rows, statuses) {
     if (!Array.isArray(rows) || !statuses.size) return rows
     return rows.map((row) => {
         if (!row) return row
-        const status = statuses.get(String(row.eventCode || row.eventNo || ''))
+        const status = statuses.get(String(eventCodeOf(row) || ''))
         return status && status !== row.status ? { ...row, status } : row
     })
 }
@@ -121,6 +139,25 @@ export function mergeSectionStatus(section, statuses) {
     return merged === events ? section : { ...section, data: { ...section.data, events: merged } }
 }
 
+function mergeRowsType(rows, types) {
+    if (!Array.isArray(rows) || !types.size) return rows
+    return rows.map((row) => {
+        if (!row) return row
+        const type = types.get(String(eventCodeOf(row) || ''))
+        if (!type) return row
+        const next = { ...row, type }
+        if (Object.prototype.hasOwnProperty.call(row, 'eventType')) next.eventType = type
+        return next
+    })
+}
+
+export function mergeSectionType(section, types) {
+    const events = section && section.data && section.data.events
+    if (!Array.isArray(events)) return section
+    const merged = mergeRowsType(events, types)
+    return merged === events ? section : { ...section, data: { ...section.data, events: merged } }
+}
+
 export const useEventStore = defineStore('eventStore', () => {
     /** 当前切换状态:`WARNING`(预警)或 `ALARM`(报警)。默认预警(`/events/current` 的 mode 必填)。 */
     const mode = ref(EVENT_FAMILY.WARNING)
@@ -425,14 +462,15 @@ export const useEventStore = defineStore('eventStore', () => {
         // 当前事件分区本次刚取到;历史/效率行只借用它的 status。
         // 后端历史/效率快照即使暂时滞后,列表卡片也不会显示与详情相反的旧状态。
         const currentStatuses = currentStatusByCode(current)
-        const historyWithStatus = mergeSectionStatus(history, currentStatuses)
+        const currentTypes = currentTypeByCode(current)
+        const historyWithStatus = mergeSectionType(mergeSectionStatus(history, currentStatuses), currentTypes)
         // 票 71 返工:把逐日趋势挂进效率分区(domain 只读 `incidentTrend` 字段)。
         // 取失败(null)时保持缺失 ⇒ 界面说「不画」的原因,不画假线。
         // **同时**用这批带 status 的行替换列表数据源:效率端点不返回 status,
         // 用它画列表会把每条都写成「状态未发布」(维护者 2026-10-29 第 1 条反馈)。
         const trend = incidentTrend && incidentTrend.days ? incidentTrend : null
         const trendRows = trend && Array.isArray(incidentTrend.rows)
-            ? mergeRowsStatus(incidentTrend.rows, currentStatuses)
+            ? mergeRowsType(mergeRowsStatus(incidentTrend.rows, currentStatuses), currentTypes)
             : null
         const efficiencyWithTrend = efficiency && efficiency.data
             ? {

+ 8 - 1
tests/eventStatusMerge.test.js

@@ -2,7 +2,7 @@
 import assert from 'node:assert/strict'
 import { test } from 'node:test'
 
-import { mergeRowsStatus, mergeSectionStatus } from '../src/modules/cockpit/stores/eventStore.js'
+import { mergeRowsStatus, mergeSectionStatus, mergeSectionType } from '../src/modules/cockpit/stores/eventStore.js'
 
 test('当前事件状态覆盖历史/效率行的旧状态', () => {
     const current = { data: { events: [{ eventCode: 'BJ1', status: 'HANDLED' }] } }
@@ -17,3 +17,10 @@ test('当前事件状态覆盖历史/效率行的旧状态', () => {
     assert.equal(rows[0].status, 'HANDLED')
     assert.equal(history.data.events[0].status, 'UNHANDLED', '不得原地改原数组')
 })
+
+test('当前事件类型覆盖历史行的类型码', () => {
+    const history = { data: { events: [{ eventCode: 'YJ1', eventType: '12' }] } }
+    const merged = mergeSectionType(history, new Map([['YJ1', 'WARN-AMMONIA-NITROGEN']]))
+    assert.equal(merged.data.events[0].eventType, 'WARN-AMMONIA-NITROGEN')
+    assert.equal(history.data.events[0].eventType, '12')
+})