Ver Fonte

fix(cockpit): 预警族窗口在发布时刻缺失时回退更新时间(票 130)

效率/多维两处窗口判据统一补上发布时间缺失回退 update_time(与历史分析同口径),不写回 eventTime;自动触发→自动解除的预警在解除时以 create_time 补写 publish_time;补两条回归测试与 README 窗口口径注记。
Kazerin há 4 dias atrás
pai
commit
b07011cc0e

+ 2 - 0
docs/cockpit/README.md

@@ -103,6 +103,8 @@
 - `events/history-analysis` 同时返回两块内容:`rows[]` 是**小时桶聚合行**(趋势与行级处置率口径,不携带逐事件坐标/阈值);`events[]` 是**逐事件行**(票 61 新增,形状与 `events/current` 的 `EventItem` 同构),按 `eventTime` 倒序、同时间按 `eventNo` 升序,由 `page`/`pageSize`(默认 1/100,上限 1000)分页,元数据在 `pagination` 中,`pagination.total` 即窗口内事件总数。逐事件坐标仍只在 `events/current` 提供,历史逐事件行的 `coordinateValidation` 为 `null`。
 - 预警族三个事件端点(`events/history-analysis`、`events/handling-efficiency`、`events/multidimensional-analysis`)一律排除**草稿**(`DRAFT`)预警(票 126):草稿只是未发布的编辑稿,不进逐事件行、不进小时桶/六态分布/维度桶、不计 `pagination.total`,也**不进** `warningHandlingEfficiency` 的分母。判据与总览计数同一条——`status IS NULL OR UPPER(TRIM(status)) <> 'DRAFT'`,即状态为空/缺失的行**保留**,不因词表缺口被丢掉。`events/current` 的口径不同(排除 DRAFT 与 CLOSED),两处各自成立、互不覆盖。
 
+- **窗口判据的发布时间回退(票 130)**:预警族的时间窗以事件发布时刻(`early_warning.publish_time`)界定;当某条预警的发布时刻**缺失或不可解析**时,`events/history-analysis`、`events/handling-efficiency`、`events/multidimensional-analysis` 三条端点统一**回退到更新时间**(`update_time`)判窗与分桶——这是有前提的读数,不代表采集到了独立的发布时间,也不会把缺失的发布时刻伪装成存在(逐事件行排序仍按原值,缺发布时刻的排末尾)。与之配套,系统自动触发→自动解除的预警(从未经人工发布)会在自动解除时以生效时刻(`create_time`,即触发时刻)补写 `publish_time`(票 130);本回退保证即便历史行为空、或回填未及,已存在的预警也不会因发布时间缺失被窗口整批抹掉。此回退与上方「排除草稿」是两条独立判据:草稿在判窗前即被排除,不受回退影响;非草稿预警一律按上述回退判窗。报警族无此依赖(报警不读 `publish_time`)。
+
 ### 监测点详情设备坐标
 
 `GET /api/cockpit/v1/monitoring-points/{monitoringPointCode}` 的 `data.devices[]` 保留点位下的全部关联设备(包括离线设备),以 `deviceCode` 作为唯一关联键,并返回设备自身坐标与状态:

+ 44 - 0
pipe-network-service/zksy-admin/src/test/java/com/zksy/web/controller/cockpit/CockpitHandlingEfficiencyControllerTest.java

@@ -252,6 +252,33 @@ class CockpitHandlingEfficiencyControllerTest {
                 .andExpect(jsonPath("$.data.summary.warningHandlingEfficiency.denominator").value(3));
     }
 
+
+    /**
+     * 票 130:预警的 publish_time 为空、update_time 有值时,窗口判据必须回退到 update_time,
+     * 该预警不得被整批抹掉。当前夹具都显式给了发布时间,覆盖不到这个缺陷。
+     */
+    @Test
+    void warningWithMissingPublishTimeButPresentUpdateTimeStaysInWindow() throws Exception {
+        when(adapter.load()).thenReturn(fixtureWithPublishTimeMissing());
+
+        mockMvc.perform(get(ENDPOINT)
+                        .param("eventFamily", "WARNING")
+                        .param("startTime", START)
+                        .param("endTime", END))
+                .andExpect(status().isOk())
+                // 发布时刻为空、update_time 在窗内 → 仍计一条,六态桶不再全 0 且求和等于总数。
+                .andExpect(jsonPath("$.data.summary.totalEvents").value(1))
+                .andExpect(jsonPath("$.data.pagination.total").value(1))
+                .andExpect(jsonPath("$.data.events", hasSize(1)))
+                .andExpect(jsonPath("$.data.events[0].eventNo").value("EW-NOPUB"))
+                // 只含已知状态时六态齐全(无 UNKNOWN 桶);CLOSED 为第六个(索引 5)。
+                .andExpect(jsonPath("$.data.warningStateCounts", hasSize(6)))
+                .andExpect(jsonPath("$.data.warningStateCounts[5].stateCode").value("CLOSED"))
+                .andExpect(jsonPath("$.data.warningStateCounts[5].count").value(1))
+                .andExpect(jsonPath("$.data.summary.warningHandlingEfficiency.denominator").value(1))
+                .andExpect(jsonPath("$.data.summary.warningHandlingEfficiency.numerator").value(1));
+    }
+
     @Test
     void warningFamilyEmptyWindowReportsTypedEmptyStatesInsteadOfFakeZero() throws Exception {
         mockMvc.perform(get(ENDPOINT)
@@ -761,6 +788,23 @@ class CockpitHandlingEfficiencyControllerTest {
                         scopeOrder("WO-U", 0, "GAS_SENSOR", null)));
     }
 
+    /**
+     * 票 130:一条发布于「空」、仅 update_time 有值的已解除预警(update_time 落在窗内)。
+     * 发布时刻 eventTime 传 null,update_time 传 10:30(窗口 08:00–12:00 内)。
+     */
+    private static CockpitHandlingEfficiencySnapshot fixtureWithPublishTimeMissing() {
+        return new CockpitHandlingEfficiencySnapshot(
+                "2026-09-02T11:59:50+08:00",
+                types(),
+                Collections.singletonList(
+                        warningWithWorkflow("EW-NOPUB", 2, null, "GAS_SENSOR",
+                                "D-GAS-1", "燃气浓度计-001", WarningWorkflow.CLOSED,
+                                "2026-09-02T10:30:00+08:00")),
+                Collections.emptyList(),
+                Collections.emptyList(),
+                Collections.emptyList());
+    }
+
     private static java.util.List<CockpitHandlingEfficiencySnapshot.SourceType> types() {
         return Arrays.asList(
                 new CockpitHandlingEfficiencySnapshot.SourceType(

+ 35 - 3
pipe-network-service/zksy-admin/src/test/java/com/zksy/web/controller/cockpit/CockpitMultidimensionalAnalysisControllerTest.java

@@ -119,6 +119,29 @@ class CockpitMultidimensionalAnalysisControllerTest {
                 .andExpect(jsonPath("$.data.mapFilter.deviceCodes", hasSize(2)));
     }
 
+    /**
+     * 票 130:预警 publish_time 为空、update_time 在窗内时,多维窗口判据必须回退到 update_time,
+     * 该预警不得被整批抹掉(总数为 0)。
+     */
+    @Test
+    void warningWithMissingPublishTimeButPresentUpdateTimeIsCounted() throws Exception {
+        when(adapter.load()).thenReturn(new CockpitMultidimensionalAnalysisSnapshot(
+                "2026-09-02T15:59:50+08:00",
+                Arrays.asList(type("GAS_SENSOR", "燃气浓度计", "GAS")),
+                Arrays.asList(
+                        warningWithUpdateTime("MD-NOPUB", "GAS_CONCENTRATION", "浓度超限", 2,
+                                null, "2026-09-02T09:30:00+08:00", 0, "GAS_SENSOR", "D-1", "CLOSED")),
+                Collections.emptyList()));
+
+        mockMvc.perform(get(ENDPOINT)
+                        .param("eventFamily", "WARNING")
+                        .param("startTime", START)
+                        .param("endTime", END))
+                .andExpect(status().isOk())
+                .andExpect(jsonPath("$.data.totalCount").value(1))
+                .andExpect(jsonPath("$.data.mapFilter.deviceCodes", hasSize(1)));
+    }
+
     @Test
     void alarmFamilyIsIsolatedFromWarnings() throws Exception {
         mockMvc.perform(get(ENDPOINT)
@@ -526,15 +549,24 @@ class CockpitMultidimensionalAnalysisControllerTest {
     private static CockpitMultidimensionalAnalysisSnapshot.SourceWarning warning(
             String no, String eventType, String cause, Integer level, String eventTime,
             Integer district, String typeCode, String deviceCode) {
-        return warning(no, eventType, cause, level, eventTime, district, typeCode, deviceCode, null);
+        return new CockpitMultidimensionalAnalysisSnapshot.SourceWarning(
+                no, eventType, cause, level, eventTime, null, district, typeCode, deviceCode, null);
     }
 
-    /** 票 126:带业务状态的样本(DRAFT 用于验证草稿排除)。 */
+    /** 票 126:带业务状态的样本(DRAFT 用于验证草稿排除);票 130:更新时刻留空。 */
     private static CockpitMultidimensionalAnalysisSnapshot.SourceWarning warning(
             String no, String eventType, String cause, Integer level, String eventTime,
             Integer district, String typeCode, String deviceCode, String status) {
         return new CockpitMultidimensionalAnalysisSnapshot.SourceWarning(
-                no, eventType, cause, level, eventTime, district, typeCode, deviceCode, status);
+                no, eventType, cause, level, eventTime, null, district, typeCode, deviceCode, status);
+    }
+
+    /** 票 130:带更新时刻的样本 —— 发布时刻为 null、更新时刻在窗内,验证窗口回退。 */
+    private static CockpitMultidimensionalAnalysisSnapshot.SourceWarning warningWithUpdateTime(
+            String no, String eventType, String cause, Integer level, String eventTime, String updateTime,
+            Integer district, String typeCode, String deviceCode, String status) {
+        return new CockpitMultidimensionalAnalysisSnapshot.SourceWarning(
+                no, eventType, cause, level, eventTime, updateTime, district, typeCode, deviceCode, status);
     }
 
     private static CockpitMultidimensionalAnalysisSnapshot.SourceAlarm alarm(

+ 2 - 0
pipe-network-service/zksy-system/src/main/java/com/zksy/cockpit/event/BusinessCockpitEventAdapter.java

@@ -587,6 +587,8 @@ public class BusinessCockpitEventAdapter {
                         null,
                         level(item.warning.getWarningLevel()),
                         offsetTime(item.warning.getPublishTime()),
+                        // 发布时刻缺失时,服务层可回退到更新时间判窗/分桶;不改写 eventTime,排序仍按原值。
+                        offsetTime(item.warning.getUpdateTime()),
                         districtCode(item.warning.getDeviceCode(), facts),
                         typeCode(item.warning.getDeviceCode(), facts),
                         trimToNull(item.warning.getDeviceCode()),

+ 7 - 3
pipe-network-service/zksy-system/src/main/java/com/zksy/cockpit/event/CockpitHandlingEfficiencyService.java

@@ -606,11 +606,15 @@ public class CockpitHandlingEfficiencyService {
     private static List<EventFact> inWindow(List<EventFact> facts, TimeWindow window) {
         List<EventFact> result = new ArrayList<>();
         for (EventFact fact : facts) {
-            OffsetDateTime eventTime = parseEventTime(fact.eventTime);
-            if (eventTime == null) {
+            OffsetDateTime reference = parseEventTime(fact.eventTime);
+            if (reference == null) {
+                // 发布时刻缺失时退回更新时间,仅用于判窗;不写回 eventTime,排序仍按原值为 null 排末尾。
+                reference = parseEventTime(fact.updateTime);
+            }
+            if (reference == null) {
                 continue;
             }
-            if (eventTime.isBefore(window.start) || eventTime.isAfter(window.end)) {
+            if (reference.isBefore(window.start) || reference.isAfter(window.end)) {
                 continue;
             }
             result.add(fact);

+ 25 - 4
pipe-network-service/zksy-system/src/main/java/com/zksy/cockpit/event/CockpitMultidimensionalAnalysisService.java

@@ -312,7 +312,11 @@ public class CockpitMultidimensionalAnalysisService {
             labels.put(key, key);
         }
         for (Fact fact : facts) {
-            ZonedDateTime day = fact.eventTime.atZoneSameInstant(BUSINESS_ZONE).truncatedTo(ChronoUnit.DAYS);
+            OffsetDateTime reference = referenceTime(fact);
+            if (reference == null) {
+                continue;   // 理论上进不了 scoped(已被 inWindow 过滤),防御性保留
+            }
+            ZonedDateTime day = reference.atZoneSameInstant(BUSINESS_ZONE).truncatedTo(ChronoUnit.DAYS);
             String key = DAY.format(day);
             if (counts.containsKey(key)) {
                 counts.merge(key, 1, Integer::sum);
@@ -369,6 +373,7 @@ public class CockpitMultidimensionalAnalysisService {
                     warning.getCause(),
                     warning.getLevel(),
                     parseEventTime(warning.getEventTime()),
+                    parseEventTime(warning.getUpdateTime()),   // ← 新增
                     warning.getDistrict(),
                     type.getIndustry(),
                     type.getCode(),
@@ -397,6 +402,7 @@ public class CockpitMultidimensionalAnalysisService {
                     alarm.getCause(),
                     alarm.getLevel(),
                     parseEventTime(alarm.getEventTime()),
+                    null,                                        // ← 报警族不回退
                     alarm.getDistrict(),
                     type.getIndustry(),
                     type.getCode(),
@@ -421,11 +427,22 @@ public class CockpitMultidimensionalAnalysisService {
         return type;
     }
 
+    /**
+     * 判窗/分桶的参照时刻:优先事件发布时刻,缺失时回退到更新时间(有前提的读数)。
+     * 回退值仅用于判定,**不写回 {@link Fact#eventTime}** —— 事件排序仍按原值,
+     * 缺失如实排末尾,不假装发布时刻存在。
+     */
+    private static OffsetDateTime referenceTime(Fact fact) {
+        return fact.eventTime != null ? fact.eventTime : fact.updateTime;
+    }
+
     private static boolean inWindow(Fact fact, TimeWindow window) {
-        if (fact.eventTime == null) {
+        // 判窗用参照时刻(发布时刻缺失时回退更新时间);两者皆空才丢。
+        OffsetDateTime reference = referenceTime(fact);
+        if (reference == null) {
             return false;
         }
-        return !fact.eventTime.isBefore(window.start) && !fact.eventTime.isAfter(window.end);
+        return !reference.isBefore(window.start) && !reference.isAfter(window.end);
     }
 
     private static Map<String, CockpitMultidimensionalAnalysisSnapshot.SourceType> types(
@@ -567,17 +584,21 @@ public class CockpitMultidimensionalAnalysisService {
         private final String cause;
         private final Integer level;
         private final OffsetDateTime eventTime;
+        /** 更新时间;发布时刻缺失时作为判窗/分桶的回退时刻(仅判定,不写回 eventTime)。 */
+        private final OffsetDateTime updateTime;
         private final Integer district;
         private final String industry;
         private final String deviceType;
         private final String deviceCode;
 
         private Fact(String eventType, String cause, Integer level, OffsetDateTime eventTime,
-                Integer district, String industry, String deviceType, String deviceCode) {
+                     OffsetDateTime updateTime, Integer district, String industry,
+                     String deviceType, String deviceCode) {
             this.eventType = eventType;
             this.cause = cause;
             this.level = level;
             this.eventTime = eventTime;
+            this.updateTime = updateTime;
             this.district = district;
             this.industry = industry;
             this.deviceType = deviceType;

+ 6 - 0
pipe-network-service/zksy-system/src/main/java/com/zksy/cockpit/event/CockpitMultidimensionalAnalysisSnapshot.java

@@ -66,6 +66,8 @@ public class CockpitMultidimensionalAnalysisSnapshot {
         private final String cause;
         private final Integer level;
         private final String eventTime;
+        /** 预警记录最后更新时间;发布时刻缺失时,服务层以它回退判窗/分桶(仅判定,不改写 eventTime)。 */
+        private final String updateTime;
         private final Integer district;
         private final String typeCode;
         private final String deviceCode;
@@ -78,6 +80,7 @@ public class CockpitMultidimensionalAnalysisSnapshot {
                 String cause,
                 Integer level,
                 String eventTime,
+                String updateTime,
                 Integer district,
                 String typeCode,
                 String deviceCode,
@@ -87,6 +90,7 @@ public class CockpitMultidimensionalAnalysisSnapshot {
             this.cause = cause;
             this.level = level;
             this.eventTime = eventTime;
+            this.updateTime = updateTime;
             this.district = district;
             this.typeCode = typeCode;
             this.deviceCode = deviceCode;
@@ -98,6 +102,8 @@ public class CockpitMultidimensionalAnalysisSnapshot {
         public String getCause() { return cause; }
         public Integer getLevel() { return level; }
         public String getEventTime() { return eventTime; }
+        /** 预警记录最后更新时间;发布时刻缺失时,服务层以它回退判窗/分桶(仅判定,不改写 eventTime)。 */
+        public String getUpdateTime() { return updateTime; }
         /** 片区编码(0-城南 1-城北 2-太常,权威来源 equipment_base.district);未分区为 {@code null},不猜。 */
         public Integer getDistrict() { return district; }
         public String getTypeCode() { return typeCode; }

+ 5 - 0
zk-api-service/src/main/java/com/zksy/api/mapper/AutoWarningClosureMapper.java

@@ -10,7 +10,12 @@ public interface AutoWarningClosureMapper {
             + "AND status='DRAFT' ORDER BY create_time DESC LIMIT 1 FOR UPDATE")
     EarlyWarning lockDraft(@Param("deviceCode") String deviceCode, @Param("marker") String marker);
 
+    // 票 130:本条只关闭未发布的自动草稿(status='DRAFT'),publish_time 必为空。
+    // 自动触发→自动解除从未经过人工发布,故以生效时刻(create_time,即触发时刻)补写
+    // publish_time,避免该预警因发布时间缺失被效率/多维的窗口整批抹掉;COALESCE 保证
+    // 若已有真实发布时刻绝不覆盖。
     @Update("UPDATE app_user.early_warning SET status='CLOSED', stage_deadline=NULL, "
+            + "publish_time=COALESCE(publish_time, create_time), "
             + "update_by='系统自动', update_time=#{now} WHERE warning_id=#{id} AND status='DRAFT'")
     int closeDraft(@Param("id") String id, @Param("now") LocalDateTime now);