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

fix(cockpit): 预警研判统计排除草稿状态预警

Kazerin 3 недель назад
Родитель
Сommit
67d33e0a6a

+ 11 - 0
pipe-network-service/zksy-system/src/main/resources/mapper/cockpit/CockpitOverviewModulesMapper.xml

@@ -123,6 +123,14 @@
         GROUP BY pb.network_type, LEFT(UPPER(COALESCE(pb.network_code, '')), 2)
     </select>
 
+    <!-- 可统计的预警集合:草稿(DRAFT)还没发布,不属于任何对外口径
+         (维护者 2026-11-12:「预警统计要排除草稿状态的预警」)。
+         今日/昨日新增、区间总数与类型排行共用这一个判据,避免四处各写一份日后漂移。
+         未解除集合另有白名单,见 selectUnresolvedStatusLevelCounts。 -->
+    <sql id="countableWarningFilter">
+        AND UPPER(TRIM(w.status)) &lt;&gt; 'DRAFT'
+    </sql>
+
     <select id="selectWarningCounts" resultType="java.util.LinkedHashMap">
         <include refid="typeTree"/>
         SELECT COALESCE(SUM(CASE WHEN w.create_time &gt;= #{todayStart} AND w.create_time &lt; #{tomorrowStart}
@@ -132,6 +140,7 @@
                MAX(w.update_time) AS source_updated_at
         FROM app_user.early_warning w
         WHERE 1 = 1
+        <include refid="countableWarningFilter"/>
         <include refid="deviceDistrictFilter"/>
     </select>
 
@@ -153,6 +162,7 @@
         SELECT COUNT(*) AS warning_count
         FROM app_user.early_warning w
         WHERE w.create_time &gt;= #{startInclusive} AND w.create_time &lt; #{endExclusive}
+        <include refid="countableWarningFilter"/>
         <include refid="deviceDistrictFilter"/>
     </select>
 
@@ -162,6 +172,7 @@
                COUNT(*) AS type_count
         FROM app_user.early_warning w
         WHERE w.create_time &gt;= #{startInclusive} AND w.create_time &lt; #{endExclusive}
+        <include refid="countableWarningFilter"/>
         <include refid="deviceDistrictFilter"/>
         GROUP BY w.warning_type
         ORDER BY COUNT(*) DESC, w.warning_type ASC

+ 109 - 0
pipe-network-service/zksy-system/src/test/java/com/zksy/cockpit/overview/CockpitOverviewWarningStudyDraftExclusionTest.java

@@ -0,0 +1,109 @@
+package com.zksy.cockpit.overview;
+
+import org.junit.jupiter.api.Test;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * 预警研判统计必须排除草稿(维护者 2026-11-12)。
+ *
+ * <p>维护者原话:「预警统计要排除草稿状态的预警。例:当前的总体概览-运行监测-预警研判中的
+ * 统计没有排除草稿状态的预警。」</p>
+ *
+ * <p>草稿({@code DRAFT})还没发布,不属于任何可统计口径:今日新增、区间总数、类型排行
+ * 与未解除集合一样都要排除。此前的守卫只覆盖未解除集合
+ * ({@code selectUnresolvedStatusLevelCounts} 自带状态白名单),其余三条查询没有任何状态条件
+ * —— 草稿因此直接进了面板读数。</p>
+ *
+ * <p>这是 SQL 契约测试:状态过滤只能写在查询里。服务层拿不到「被 SQL 已排除之外」的信息,
+ * 只在 Java 层再滤一次既改不了「今日新增 / 区间总数」这类聚合值,也会把口径拆成两处。</p>
+ */
+class CockpitOverviewWarningStudyDraftExclusionTest {
+
+    private static final String MAPPER_NAME = "CockpitOverviewModulesMapper.xml";
+    /** 三条统计查询共用同一个片段,避免四面各写一份。 */
+    private static final String SHARED_FILTER_INCLUDE = "<include refid=\"countableWarningFilter\"/>";
+    private static final String DRAFT_FILTER = "UPPER(TRIM(w.status)) &lt;&gt; 'DRAFT'";
+
+    @Test
+    void sharedFilterDefinitionExcludesDraft() throws Exception {
+        String sql = fragmentBody("countableWarningFilter");
+
+        assertTrue(sql.contains(DRAFT_FILTER), "草稿不算可统计预警(与大小写、首尾空白无关)");
+    }
+
+    @Test
+    void todayAndYesterdayCountsExcludeDraft() throws Exception {
+        String sql = selectBody("selectWarningCounts");
+
+        assertTrue(sql.contains(SHARED_FILTER_INCLUDE),
+                "今日/昨日新增必须排除草稿,否则草稿一创建就计入新增");
+    }
+
+    @Test
+    void warningCountInWindowExcludesDraft() throws Exception {
+        String sql = selectBody("selectWarningCountInWindow");
+
+        assertTrue(sql.contains(SHARED_FILTER_INCLUDE),
+                "类型排行的分母(区间总数)必须排除草稿,否则排行占比失真");
+    }
+
+    @Test
+    void warningTypeRankingExcludeDraftRows() throws Exception {
+        String sql = selectBody("selectWarningTypeCountsInWindow");
+
+        assertTrue(sql.contains(SHARED_FILTER_INCLUDE),
+                "类型排行的每一行必须排除草稿,否则草稿会占掉一个类型的计数");
+    }
+
+    @Test
+    void unresolvedStatusCountsKeepTheirExplicitAllowList() throws Exception {
+        String sql = selectBody("selectUnresolvedStatusLevelCounts");
+
+        assertTrue(sql.contains("IN ('PENDING', 'RECEIVED', 'PROCESSING', 'HANDLED')"),
+                "未解除集合保持白名单四态(草稿与已解除都不在列)");
+        assertFalse(sql.contains(SHARED_FILTER_INCLUDE),
+                "白名单已经排除草稿,不得再叠一条冗余条件");
+    }
+
+    /** 取 `<sql id=...>` 片段体。 */
+    private static String fragmentBody(String id) throws Exception {
+        return between("<sql id=\"" + id + "\">", "</sql>");
+    }
+
+    /** 取某个 select 的整段 XML(含 where / include),用于断言引用存在。 */
+    private static String selectBody(String id) throws Exception {
+        return between("<select id=\"" + id + "\"", "</select>");
+    }
+
+    private static String between(String openMarker, String closeMarker) throws Exception {
+        String xml = mapperXml();
+        int start = xml.indexOf(openMarker);
+        assertTrue(start >= 0, "找不到 " + openMarker);
+        int end = xml.indexOf(closeMarker, start);
+        assertTrue(end > start, openMarker + " 未闭合");
+        return xml.substring(start, end);
+    }
+
+    /** 模块目录与仓库根目录两种工作目录下都要能找到 **源码** mapper。
+     *
+     *  刻意只找 `src/main/resources`:`target/classes` 是构建副本,可能比源码旧
+     *  (本测试就是先在旧副本上假红了一次),拿它当真值等于没测到改动手上。 */
+    private static String mapperXml() throws Exception {
+        Path cwd = Path.of(System.getProperty("user.dir")).toAbsolutePath();
+        for (Path candidate : new Path[]{
+                cwd.resolve("src/main/resources/mapper/cockpit/" + MAPPER_NAME),
+                cwd.resolve("zksy-system/src/main/resources/mapper/cockpit/" + MAPPER_NAME),
+                cwd.resolve("pipe-network-service/zksy-system/src/main/resources/mapper/cockpit/" + MAPPER_NAME)}) {
+            Path normalized = candidate.normalize();
+            if (Files.exists(normalized)) {
+                return Files.readString(normalized);
+            }
+        }
+        throw new AssertionError("找不到源码 mapper:" + MAPPER_NAME + "(工作目录 " + cwd + ")");
+    }
+}