From 8fc27dba6719041402e3e3c099e2f3e01d9d52c7 Mon Sep 17 00:00:00 2001
From: feiyu02 <risaku@163.com>
Date: 星期三, 16 七月 2025 17:30:56 +0800
Subject: [PATCH] 2025.7.16 1. 修改动态溯源异常判断逻辑

---
 src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/BaseExceptionContinuous.kt |  175 +++++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 141 insertions(+), 34 deletions(-)

diff --git a/src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/BaseExceptionContinuous.kt b/src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/BaseExceptionContinuous.kt
index 658c621..733641e 100644
--- a/src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/BaseExceptionContinuous.kt
+++ b/src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/BaseExceptionContinuous.kt
@@ -5,13 +5,14 @@
 import com.flightfeather.uav.domain.entity.BaseRealTimeData
 import com.flightfeather.uav.lightshare.eunm.ExceptionStatusType
 import com.flightfeather.uav.socket.eunm.FactorType
+import org.springframework.beans.BeanUtils
 import java.time.Duration
 
 /**
  * 杩炵画绫诲瀷鐨勫紓甯稿垎鏋愬熀绫�,閫傜敤浜庡綋鍓嶆暟鎹笌鐩搁偦鏁版嵁涔嬮棿鏈夊叧鑱斿叧绯荤殑鎯呭喌
  */
 abstract class BaseExceptionContinuous<T : ExceptionTag, V : BaseAnalysisConfig, Y : BaseExceptionResult>(
-    config: V, private val tagClz: Class<T>
+    config: V, private val tagClz: Class<T>,
 ) : BaseExceptionAnalysis<V, Y>(config) {
 
     companion object {
@@ -27,17 +28,23 @@
     // 鏈熬鏁版嵁瀵硅薄
     protected var lastData: BaseRealTimeData? = null
 
+    // 鏈�鏂扮殑涓�缁勫紓甯革紝璁板綍鍗曞洜瀛愬紓甯�
+    protected val latestExceptions = mutableListOf<Pair<FactorFilter.SelectedFactor, T>>()
+
     /**
-     * 鍚庣疆鍒ゆ柇锛氬綋鐩搁偦鏁版嵁鏃堕棿涓嶈繛缁椂锛屾垨鑰呮弧瓒宠嚜瀹氫箟鏉′欢鏃讹紝瀵逛箣鍓嶅凡鏈夌殑寮傚父杩涜璁板綍
+     * 鏈�鏂扮殑涓�缁勫悎骞跺紓甯革紝鏍规嵁閰嶇疆鍙傛暟浠嶽latestExceptions]鍗曞洜瀛愬紓甯镐腑锛屽悎骞跺紓甯�
      */
-    open fun afterExcCheck(isContinue: Boolean, tag: T, hasException: Boolean?): Boolean {
-        return !isContinue || needCut(tag, hasException)
-    }
+    protected val latestCombinedExc = mutableListOf<List<Pair<FactorFilter.SelectedFactor, T>>>()
+
+    /**
+     * 寮傚父缁撴灉
+     */
+    protected val result = mutableListOf<Y>()
 
     /**
      * 绔嬪嵆鍒ゆ柇锛氬綋鍑虹幇寮傚父鏃讹紝缂撳瓨寮傚父鏁版嵁鐨勫悓鏃讹紝绔嬪嵆瀵瑰凡鏈夊紓甯歌繘琛屽垽鏂槸鍚︽弧瓒冲紓甯哥粨鏋滆姹�
      */
-    open fun immeExcCheck(tag: T): Boolean {
+    open fun immeExcCheck(tag: T, factorType: FactorType): Boolean {
         return false
     }
 
@@ -53,6 +60,16 @@
     }
 
     /**
+     * 鍒ゆ柇鏁版嵁閲忕骇鍦ㄥ紓甯稿垽鏂殑鑼冨洿鍐�
+     * 榛樿鎵�鏈夐噺绾ч兘鍦ㄥ紓甯稿垽鏂殑鑼冨洿鍐�
+     */
+    open fun judgeDataScale(p: BaseRealTimeData?, n: BaseRealTimeData): MutableMap<FactorType, Boolean> {
+        val res = mutableMapOf<FactorType, Boolean>()
+        config.factorFilter.mainList().forEach { f -> res[f] = true }
+        return res
+    }
+
+    /**
      * 鍒ゆ柇鍓嶅悗鏁版嵁鏄惁婊¤冻寮傚父鏉′欢
      */
     abstract fun judgeException(p: BaseRealTimeData?, n: BaseRealTimeData): MutableMap<FactorType, Boolean>
@@ -61,13 +78,26 @@
      * 鍒ゆ柇寮傚父鍑虹幇鐨勮繛缁釜鏁版槸鍚︽弧瓒虫潯浠�
      * @param tag 寮傚父鏁版嵁瀵硅薄
      */
-    abstract fun judgeExceptionCount(tag: T): Boolean
+    abstract fun judgeExceptionCount(tag: T, factorType: FactorType?): Boolean
+
+    /**
+     * 鍒ゆ柇鐩戞祴鍥犲瓙鏄惁鍑虹幇寮傚父
+     */
+    open fun judge(p: BaseRealTimeData?, n: BaseRealTimeData): MutableMap<FactorType, Boolean> {
+        val jds = judgeDataScale(p, n)
+        val jex = judgeException(p, n)
+        val res = mutableMapOf<FactorType, Boolean>()
+        jds.forEach { (t, u) ->
+            res[t] = u && jex[t] ?: false
+        }
+        return res
+    }
 
     /**
      * 寮傚父鏁版嵁鐨勬埅鍙栧垽鏂�
-     * @return 榛樿涓嶉渶瑕佹埅鍙�
+     * @return
      */
-    open fun needCut(tag: T, hasException: Boolean?): Boolean {
+    open fun needCut(tag: T, hasException: Boolean?, data: BaseRealTimeData): Boolean {
         // 榛樿鍒ゆ柇鏉′欢涓� 褰撳紓甯镐笉鍐嶉噸澶嶅嚭鐜版椂锛屽舰鎴愬紓甯哥粨鏋�
         return tag.exceptionExisted && hasException == false
     }
@@ -83,10 +113,12 @@
 
     override fun onNextData(data: BaseRealTimeData) {
         val isContinue = isContinuous(lastData, data)
-        val hasException = judgeException(lastData, data)
+        val hasException = judge(lastData, data)
         config.factorFilter.selectedList.forEach { s ->
             val f = s.main
             tagMap[f]?.let {
+                it.addHistoryData(data)
+
                 it.eIndex++
                 // 璧峰鏁版嵁
                 it.endData = data
@@ -96,22 +128,35 @@
 
                 // 瀵逛簬寮傚父鐨勭敓鎴愬垎鍒墽琛屽悗缃垽鏂�佸拰绔嬪嵆鍒ゆ柇
                 // 1. 鍚庣疆鍒ゆ柇锛氬綋鐩搁偦鏁版嵁鏃堕棿涓嶈繛缁椂锛屾垨鑰呮弧瓒宠嚜瀹氫箟鏉′欢鏃讹紝瀵逛箣鍓嶅凡鏈夌殑寮傚父杩涜璁板綍锛屽舰鎴愬紓甯哥粨鏋�
-                if (afterExcCheck(isContinue, it, hasException[f])) {
-                    // 鏁版嵁涓嶈繛缁椂鎴栬�呮弧瓒充富鍔ㄦ埅鏂潯浠舵椂锛岃褰曞紓甯告儏鍐�
-                    recordException(s, it, data)
-                }
+//                if (afterExcCheck(isContinue, it, hasException[f])) {
+//                    // 鏁版嵁涓嶈繛缁椂鎴栬�呮弧瓒充富鍔ㄦ埅鏂潯浠舵椂锛岃褰曞紓甯告儏鍐�
+//                    recordException(s, it, data)
+//                }
                 // 2. 绔嬪嵆鍒ゆ柇锛氬綋鍑虹幇寮傚父鏃讹紝缂撳瓨寮傚父鏁版嵁鐨勫悓鏃讹紝绔嬪嵆瀵瑰凡鏈夊紓甯歌繘琛屽垽鏂槸鍚︽弧瓒冲紓甯哥粨鏋滆姹�
-                else if (hasException[f] == true) {
+                if (hasException[f] == true) {
+
+//                    afterExcCheck(isContinue, it, hasException[f])
+                    if (needCut(it, hasException[f], data)) {
+                        it.refreshWithNextException(data)
+                    }
                     // 鏈夊紓甯稿嚭鐜版椂锛岃褰曞紓甯告暟鎹�
                     it.addExceptionData(data)
                     // 褰撶珛鍗冲垽鏂�氳繃鏃讹紝褰㈡垚寮傚父缁撴灉
-                    if (immeExcCheck(it)) {
+                    if (immeExcCheck(it, f)) {
                         recordException(s, it, data)
                     }
                 }
+                // 3. 鏁版嵁姝e父锛屾棤浠讳綍寮傚父鏃禿
+                // TODO("2025.6.3锛氬叾浠栧瓙绫荤殑姝ゅ鍒锋柊閫昏緫寰呭畬鎴愨��)
+//                else {
+//                    it.refreshWithNextException(data)
+//                }
             }
         }
         lastData = data
+
+        mergeExceptionResult()
+        clearExceptions(data)
     }
 
     override fun onDone() {
@@ -124,7 +169,7 @@
      */
     fun recordException(factor: FactorFilter.SelectedFactor, tag: T, data: BaseRealTimeData) {
         checkResult(factor, ExceptionStatusType.Ended)
-        tag.refreshWithNextException(data)
+//        tag.refreshWithNextException(data)
     }
 
     /**
@@ -132,17 +177,17 @@
      */
     open fun checkResult(
         factor: FactorFilter.SelectedFactor? = null,
-        exceptionStatus: ExceptionStatusType = ExceptionStatusType.InProgress
+        exceptionStatus: ExceptionStatusType = ExceptionStatusType.InProgress,
     ) {
         val tag = tagMap[factor?.main]
         if (factor != null && tag != null) {
-            if (tag.exceptionExisted && judgeExceptionCount(tag)) {
+            if (tag.exceptionExisted && judgeExceptionCount(tag, factor.main)) {
                 onNewException(tag, factor, exceptionStatus)
             }
         } else {
             config.factorFilter.selectedList.forEach { f ->
                 val tag1 = tagMap[f.main] ?: return@forEach
-                if (tag1.exceptionExisted && judgeExceptionCount(tag1)) {
+                if (tag1.exceptionExisted && judgeExceptionCount(tag1, f.main)) {
                     onNewException(tag1, f, exceptionStatus)
                 }
             }
@@ -154,22 +199,84 @@
      */
     open fun onNewException(tag: T, factor: FactorFilter.SelectedFactor, exceptionStatus: ExceptionStatusType) {
         if (tag.startData == null) return
-        val ex = newResult(tag.startData!!, lastData, factor, tag.exceptionData)
-            .apply { status = exceptionStatus.value }
-        // 寮傚父宸插垱寤烘椂锛屾洿鏂板紓甯镐俊鎭�
-        if (tag.exceptionCreated) {
-            // 灏嗘渶鏂扮殑寮傚父鐨刧uid璧嬪�肩粰ex
-            val lastEx = tag.exceptionResult.last()
-            ex.guid = lastEx.guid
-            tag.exceptionResult.removeLast()
-            tag.exceptionResult.add(ex)
+//        val ex = newResult(tag.startData!!, tag.endData, factor, tag.exceptionData)
+//        val ex = newResult(tag, factor)
+//            .apply { status = exceptionStatus.value }
+//        // 寮傚父宸插垱寤烘椂锛屾洿鏂板紓甯镐俊鎭�
+//        if (tag.exceptionCreated) {
+//            // 灏嗘渶鏂扮殑寮傚父鐨刧uid璧嬪�肩粰ex
+//            val lastEx = tag.exceptionResult.last()
+//            ex.guid = lastEx.guid
+//            tag.exceptionResult.removeLast()
+//            tag.exceptionResult.add(ex)
+//        }
+//        // 寮傚父鏈垱寤烘椂锛屾柊寤哄紓甯镐俊鎭�
+//        else {
+//            tag.exceptionResult.add(ex)
+//            tag.exceptionCreated = true
+//        }
+//        val tagClone = tagClz.newInstance()
+//        BeanUtils.copyProperties(tag, tagClone)
+        latestExceptions.add(factor to tag)
+    }
+
+    /**
+     * 鍚堝苟寮傚父
+     */
+    open fun mergeExceptionResult() {
+        // 閬嶅巻鎵�鏈夌殑鍥犲瓙缁勫悎
+        config.factorFilter.combination.forEach { c ->
+            val res = mutableListOf<Pair<FactorFilter.SelectedFactor, T>>()
+            var exist = true
+            // 鏌ョ湅缁勫悎鍐呯殑鎵�鏈夊洜瀛愭槸鍚﹂兘鍚屾椂鍑虹幇寮傚父
+            c.forEach { f ->
+                val r = latestExceptions.find { e ->
+                    e.first.main == f
+                }
+                if (r != null) {
+                    res.add(r)
+                } else {
+                    exist = false
+                }
+            }
+            // 濡傛灉缁勫悎鍐呯殑鎵�鏈夊洜瀛愰兘瀛樺湪寮傚父锛屽垯瀛樺偍涓哄悎骞跺紓甯�
+            if (exist) {
+                // 灏嗗悎骞跺紓甯镐粠鍗曚釜寮傚父闆嗗悎涓幓闄�
+                res.forEach { r ->
+                    latestExceptions.removeIf { e -> e.first.main == r.first.main }
+                }
+                // 灏嗗悎骞跺紓甯稿瓨鍌�
+                latestCombinedExc.add(res)
+            }
         }
-        // 寮傚父鏈垱寤烘椂锛屾柊寤哄紓甯镐俊鎭�
-        else {
-            tag.exceptionResult.add(ex)
-//            resultList.add(ex)
-            tag.exceptionCreated = true
+        // 瀛樺偍寮傚父缁撴灉
+        latestExceptions.forEach {
+            result.add(newResult(listOf(it)))
+        }
+        latestCombinedExc.forEach {
+            result.add(newResult(it))
         }
     }
 
+    private fun clearExceptions(data: BaseRealTimeData) {
+        latestExceptions.forEach {
+            it.second.refreshWithNextException(data)
+        }
+        latestExceptions.clear()
+        latestCombinedExc.forEach {
+            it.forEach { e ->
+                e.second.refreshWithNextException(data)
+            }
+        }
+        latestCombinedExc.clear()
+        result.clear()
+    }
+
+    /**
+     * 鐢熸垚涓�鏉″紓甯稿垎鏋愮粨鏋�
+     */
+    abstract fun newResult(tag: T, factor: FactorFilter.SelectedFactor): Y
+
+    abstract fun newResult(exceptions: List<Pair<FactorFilter.SelectedFactor, ExceptionTag>>): Y
+
 }
\ No newline at end of file

--
Gitblit v1.9.3