Explorar o código

T04 fix: lazyRegister 防污染——auto_created 定义/规则改 status=0(pending) + 垃圾名跳过注册

Xiaogang Liao hai 2 semanas
pai
achega
efe5f5549e

+ 42 - 4
cfc-backend/src/main/java/com/etotem/cfc/service/IndicatorMappingService.java

@@ -285,8 +285,22 @@ public class IndicatorMappingService {
         public void setDefStatus(Integer defStatus) { this.defStatus = defStatus; }
     }
 
-    /** 惰性注册新定义(code 规则:indicator.<cat>.<name> / bacteria.<name> / food.<name>) */
+    /**
+     * 惰性注册新定义(code 规则:indicator.<cat>.<name> / bacteria.<name> / food.<name>)。
+     *
+     * <p><b>防污染(T04)</b>:</p>
+     * <ul>
+     *   <li>疑似垃圾名(数值/范围/百分比等被误当作指标名)直接跳过,不建定义、不建规则,返回 null;
+     *       调用方(NormalizeIndicatorPipeline 等)对 null 已做安全跳过。</li>
+     *   <li>新定义与 auto_created 规则一律按 <b>status=0(pending)</b> 登记,不冒进管理端字典列表
+     *       (与 IndicatorService.listDefinitions 的 Q17 默认 status=1 语义一致),待管理端确认/合并。</li>
+     * </ul>
+     */
     private Long lazyRegister(String sourceType, String rawName) {
+        if (isGarbageName(rawName)) {
+            log.warn("惰性注册跳过(疑似垃圾名/无意义值)sourceType={}, raw={}", sourceType, rawName);
+            return null;
+        }
         DefinitionCode code = buildCode(sourceType, rawName);
         IndicatorDefinition def = new IndicatorDefinition();
         def.setDomain("physical");
@@ -299,15 +313,39 @@ public class IndicatorMappingService {
         def.setValueType(inferValueType(sourceType));
         def.setGoodDirection("neutral");
         def.setSortOrder(0);
-        def.setStatus(1);
+        def.setStatus(0);
         def.setCreatedAt(new Date());
         def.setUpdatedAt(new Date());
         definitionMapper.insert(def);
-        registerRule(sourceType, rawName, def.getId(), "auto_created", 1);
-        log.info("惰性注册指标定义 sourceType={}, raw={} → defId={}", sourceType, rawName, def.getId());
+        registerRule(sourceType, rawName, def.getId(), "auto_created", 0);
+        log.info("惰性注册指标定义(pending 待确认)sourceType={}, raw={} → defId={}",
+                sourceType, rawName, def.getId());
         return def.getId();
     }
 
+    /**
+     * 疑似垃圾名判定:纯数值 / 数值区间 / 百分比串等被误当作指标名的无意义值。
+     * 例:参考区间被解析成 "5.1543-18.1656"、"0-0.0999",覆盖率 "99%"。
+     */
+    private boolean isGarbageName(String rawName) {
+        if (rawName == null) {
+            return true;
+        }
+        String t = rawName.trim();
+        if (t.isEmpty()) {
+            return true;
+        }
+        // 纯数字/带 . - , 的数值或区间串(以数字开头),如 "5.1543-18.1656"、"0-0.0999"
+        if (t.matches("^[0-9][0-9.,\\-]*$")) {
+            return true;
+        }
+        // 百分比,如 "99%"、"12.5%"
+        if (t.matches("^[0-9.,]+%$")) {
+            return true;
+        }
+        return false;
+    }
+
     private void registerRule(String sourceType, String rawName, Long definitionId, String matchType, int status) {
         IndicatorMappingRule rule = new IndicatorMappingRule();
         rule.setSourceType(sourceType);

+ 16 - 3
cfc-backend/src/test/java/com/etotem/cfc/service/IndicatorMappingServiceTest.java

@@ -47,6 +47,7 @@ class IndicatorMappingServiceTest {
     @Test
     void resolveMissLazilyRegistersDefinitionAndRule() {
         when(ruleMapper.selectOne(any())).thenReturn(null);
+        when(ruleMapper.selectList(any())).thenReturn(Collections.emptyList());
         IndicatorDefinition created = new IndicatorDefinition();
         created.setId(99L);
         when(definitionMapper.insert(any(IndicatorDefinition.class))).thenAnswer(inv -> {
@@ -56,9 +57,21 @@ class IndicatorMappingServiceTest {
         });
         Long defId = service.resolve("gut_flora", "新出现的菌X");
         assertEquals(99L, defId);
-        // 惰性注册:定义 + auto_created 规则
-        verify(definitionMapper).insert(any(IndicatorDefinition.class));
-        verify(ruleMapper).insert(any(IndicatorMappingRule.class));
+        // 惰性注册:定义 + auto_created 规则,均按 status=0(pending)登记,不冒进字典列表
+        verify(definitionMapper).insert(argThat(d -> d.getStatus() != null && d.getStatus() == 0));
+        verify(ruleMapper).insert(argThat(r -> r.getMatchType().equals("auto_created")
+                && r.getStatus() != null && r.getStatus() == 0));
+    }
+
+    @Test
+    void resolveMissWithGarbageNameSkipsRegistration() {
+        // 疑似垃圾名(数值区间/百分比):不建定义、不建规则,返回 null
+        when(ruleMapper.selectOne(any())).thenReturn(null);
+        when(ruleMapper.selectList(any())).thenReturn(Collections.emptyList());
+        Long defId = service.resolve("gut_flora", "5.1543-18.1656");
+        assertNull(defId);
+        verify(definitionMapper, never()).insert(any(IndicatorDefinition.class));
+        verify(ruleMapper, never()).insert(any(IndicatorMappingRule.class));
     }
 
     @Test