Riku
2025-05-14 cf160e28026ed1ed8bea82701d66e79a1085c503
src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/BaseExceptionContinuous.kt
@@ -1,15 +1,18 @@
package com.flightfeather.uav.biz.dataanalysis
import com.flightfeather.uav.biz.FactorFilter
import com.flightfeather.uav.biz.dataanalysis.model.ExceptionTag
import com.flightfeather.uav.domain.entity.BaseRealTimeData
import com.flightfeather.uav.lightshare.eunm.ExceptionStatusType
import com.flightfeather.uav.socket.eunm.FactorType
import java.time.Duration
/**
 * 连续类型的异常分析基类,适用于当前数据与相邻数据之间有关联关系的情况
 */
abstract class BaseExceptionContinuous<V : BaseAnalysisConfig, Y : BaseExceptionResult>(config: V) :
    BaseExceptionAnalysis<V, Y>(config) {
abstract class BaseExceptionContinuous<T : ExceptionTag, V : BaseAnalysisConfig, Y : BaseExceptionResult>(
    config: V, private val tagClz: Class<T>
) : BaseExceptionAnalysis<V, Y>(config) {
    companion object {
        // 记录异常数据段时,分别向起始前和末尾后额外记录的数据个数偏移量
@@ -33,17 +36,26 @@
        var exceptionData = mutableListOf<BaseRealTimeData>()
        // 是否存在异常
        var existException = false
        var exceptionExisted = false
        fun refreshAfterCheckResult(data: BaseRealTimeData) {
        // 异常结果是否创建
        var exceptionCreated = false
        fun addExceptionData(data: BaseRealTimeData) {
            exceptionExisted = true
            exceptionData.add(data)
        }
        fun refreshWithNextException(data: BaseRealTimeData) {
            sIndex = eIndex
            startData = data
            exceptionData.clear()
//            exceptionData.add(data)
            exceptionExisted = false
            exceptionCreated = false
        }
    }
    protected val tagMap = mutableMapOf<FactorType, Tag>()
    protected val tagMap = mutableMapOf<FactorType, T>()
    // 起始数据与末尾数据间隔
    open var durationCount = 1
@@ -71,14 +83,14 @@
     * 判断异常出现的连续个数是否满足条件
     * @param tag 异常数据对象
     */
    abstract fun judgeExceptionCount(tag: Tag): Boolean
    abstract fun judgeExceptionCount(tag: T): Boolean
    /**
     * 异常数据的截取判断
     * 是否需要限制一组异常数据的长度
     * @return 默认不需要截取
     */
    open fun needCut(tag: Tag): Boolean {
    open fun needCut(tag: T): Boolean {
        return false
    }
@@ -87,7 +99,7 @@
        lastData = null
        tagMap.clear()
        config.factorFilter.mainList().forEach { f ->
            tagMap[f] = Tag()
            tagMap[f] = tagClz.newInstance()
        }
    }
@@ -101,25 +113,18 @@
                // 起始数据
                it.endData = data
                if (it.startData == null) {
                    it.refreshAfterCheckResult(data)
                    it.refreshWithNextException(data)
                }
                // 判断相邻数据是否连续并且是否满足异常判断
                if (!isContinue || needCut(it)) {
                    checkResult(s)
                    // 数据不连续时,记录异常情况
                    if (it.eIndex - it.sIndex >= durationCount) {
                        it.refreshAfterCheckResult(data)
                    }
                    recordException(s, it, data)
                } else {
                    if (hasException[f] == true) {
                        it.existException = true
                        it.exceptionData.add(data)
                        it.addExceptionData(data)
                    } else {
                        // 异常不再重复出现时,记录异常情况
                        checkResult(s)
                        if (it.eIndex - it.sIndex >= durationCount) {
                            it.refreshAfterCheckResult(data)
                        }
                        recordException(s, it, data)
                    }
                }
            }
@@ -128,54 +133,62 @@
    }
    override fun onDone() {
        checkResult()
        checkResult(exceptionStatus = ExceptionStatusType.Ended)
    }
    /**
     * 异常结束,记录异常
     */
    fun recordException(factor: FactorFilter.SelectedFactor, tag: T, data: BaseRealTimeData) {
        checkResult(factor, ExceptionStatusType.Ended)
//        if (tag.eIndex - tag.sIndex >= durationCount) {
        tag.refreshWithNextException(data)
//        }
    }
    /**
     * 检查连续异常结束时,是否符合异常存储条件
     */
    open fun checkResult(factor: FactorFilter.SelectedFactor? = null) {
    open fun checkResult(
        factor: FactorFilter.SelectedFactor? = null,
        exceptionStatus: ExceptionStatusType = ExceptionStatusType.InProgress
    ) {
        val tag = tagMap[factor?.main]
        if (factor != null && tag != null) {
            if (tag.existException && judgeExceptionCount(tag)) {
                onNewException(tag, factor)
//                tag.startData?.let {
//                    resultList.add(newResult(it, lastData, factor, tag.exceptionData))
//                }
//                tag.existException = false
            if (tag.exceptionExisted && judgeExceptionCount(tag)) {
                onNewException(tag, factor, exceptionStatus)
            }
        } else {
            config.factorFilter.selectedList.forEach { f ->
                val tag1 = tagMap[f.main] ?: return@forEach
                if (tag1.existException && judgeExceptionCount(tag1)) {
                    onNewException(tag1, f)
//                    tag1.startData?.let {
//                        resultList.add(newResult(it, lastData, f, tag1.exceptionData))
//                    }
//                    tag1.existException = false
                if (tag1.exceptionExisted && judgeExceptionCount(tag1)) {
                    onNewException(tag1, f, exceptionStatus)
                }
            }
        }
    }
    /**
     * 新增一条异常
     * 新增或更新一条异常
     */
    open fun onNewException(tag:Tag, factor: FactorFilter.SelectedFactor) {
        tag.startData?.let {
            resultList.add(newResult(it, lastData, factor, tag.exceptionData))
    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) {
            // 将最新的异常的guid赋值给ex
            val lastEx = tag.exceptionResult.last()
            ex.guid = lastEx.guid
            tag.exceptionResult.removeLast()
            tag.exceptionResult.add(ex)
        }
        tag.existException = false
        // 异常未创建时,新建异常信息
        else {
            tag.exceptionResult.add(ex)
//            resultList.add(ex)
            tag.exceptionCreated = true
        }
    }
    /**
     * 生成一条异常分析结果
     */
    abstract fun newResult(
        start: BaseRealTimeData,
        end: BaseRealTimeData?,
        factor: FactorFilter.SelectedFactor,
        exceptionData: List<BaseRealTimeData>,
    ): Y
}