| | |
| | | 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) { |
| | | |
| | | enum class JudgeMethod(val des: String) { |
| | | M1("在一定的空间和时间范围内,数据累计出现N次异常后,认为该异常成立"), |
| | | M2("要求数据不间断连续出现N次异常后,认为该异常成立"), |
| | | } |
| | | |
| | | companion object { |
| | | // 记录异常数据段时,分别向起始前和末尾后额外记录的数据个数偏移量 |
| | |
| | | // 末尾数据对象 |
| | | 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 var excludedFactor: List<FactorType> = emptyList() |
| | | |
| | | abstract var judgeMethod: JudgeMethod |
| | | |
| | | /** |
| | | * 立即判断:当出现异常时,缓存异常数据的同时,立即对已有异常进行判断是否满足异常结果要求 |
| | |
| | | * 异常数据的截取判断 |
| | | * @return |
| | | */ |
| | | open fun needCut(tag: T, hasException: Boolean?): Boolean { |
| | | // 默认判断条件为 当异常不再重复出现时,形成异常结果 |
| | | open fun needCut(tag: T, hasException: Boolean?, data: BaseRealTimeData): Boolean { |
| | | // 默认判断条件为 当异常不再重复出现时 |
| | | return tag.exceptionExisted && hasException == false |
| | | } |
| | | |
| | |
| | | val hasException = judge(lastData, data) |
| | | config.factorFilter.selectedList.forEach { s -> |
| | | val f = s.main |
| | | tagMap[f]?.let { |
| | | it.addHistoryData(data) |
| | | // 排除此异常类型不适用的监测因子 |
| | | if (excludedFactor.contains(f)) return@forEach |
| | | |
| | | tagMap[f]?.let { |
| | | it.eIndex++ |
| | | // 起始数据 |
| | | it.endData = data |
| | |
| | | it.refreshWithNextException(data) |
| | | } |
| | | |
| | | // 对于异常的生成分别执行后置判断、和立即判断 |
| | | // 1. 后置判断:当相邻数据时间不连续时,或者满足自定义条件时,对之前已有的异常进行记录,形成异常结果 |
| | | if (afterExcCheck(isContinue, it, hasException[f])) { |
| | | // 数据不连续时或者满足主动截断条件时,记录异常情况 |
| | | recordException(s, it, data) |
| | | // 按照不同的方式进行异常判断 |
| | | when (judgeMethod) { |
| | | JudgeMethod.M1 -> judgeMethod1(hasException, f, it, data, s) |
| | | JudgeMethod.M2 -> judgeMethod2(isContinue, hasException, f, it, data, s) |
| | | } |
| | | // 2. 立即判断:当出现异常时,缓存异常数据的同时,立即对已有异常进行判断是否满足异常结果要求 |
| | | else if (hasException[f] == true) { |
| | | // 有异常出现时,记录异常数据 |
| | | it.addExceptionData(data) |
| | | // 当立即判断通过时,形成异常结果 |
| | | if (immeExcCheck(it, f)) { |
| | | recordException(s, it, data) |
| | | } |
| | | } |
| | | // 3. 数据正常,无任何异常时d |
| | | // TODO("2025.6.3:其他子类的此处刷新逻辑待完成“) |
| | | else { |
| | | it.refreshWithNextException(data) |
| | | } |
| | | |
| | | it.addHistoryData(data) |
| | | } |
| | | } |
| | | lastData = data |
| | | |
| | | mergeExceptionResult() |
| | | clearExceptions(data) |
| | | } |
| | | |
| | | override fun onDone() { |
| | | checkResult(exceptionStatus = ExceptionStatusType.Ended) |
| | | } |
| | | |
| | | /** |
| | | * 数据异常判断方式一 |
| | | * 在一定的空间和时间范围内,数据累计出现N次异常后,认为该异常成立 |
| | | */ |
| | | private fun judgeMethod1( |
| | | hasException: MutableMap<FactorType, Boolean>, |
| | | f: FactorType, |
| | | tag: T, |
| | | data: BaseRealTimeData, |
| | | s: FactorFilter.SelectedFactor, |
| | | ) { |
| | | // 出现异常 |
| | | if (hasException[f] == true) { |
| | | // 判断数据在空间和时间变化上是否超出限定范围,若超出则删除遗留的异常记录,刷新起始点数据 |
| | | if (needCut(tag, hasException[f], data)) { |
| | | tag.refreshWithNextException(data) |
| | | } |
| | | // 记录异常数据 |
| | | tag.addExceptionData(data) |
| | | // 当立即判断通过时,形成异常结果 |
| | | if (immeExcCheck(tag, f)) { |
| | | recordException(s, tag, data) |
| | | } |
| | | } |
| | | // 数据正常,并且没有历史异常数据时,刷新起始点数据 |
| | | else if (!tag.exceptionExisted) { |
| | | tag.refreshWithNextException(data) |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 数据异常判断方式二 |
| | | * 要求数据不间断连续出现N次异常后,认为该异常成立 |
| | | */ |
| | | private fun judgeMethod2( |
| | | isContinue: Boolean, |
| | | hasException: MutableMap<FactorType, Boolean>, |
| | | f: FactorType, |
| | | tag: T, |
| | | data: BaseRealTimeData, |
| | | s: FactorFilter.SelectedFactor, |
| | | ) { |
| | | // 当相邻数据时间不连续时,刷新起始点数据,移除历史异常记录 |
| | | if (!isContinue) { |
| | | tag.refreshWithNextException(data) |
| | | } |
| | | // 出现异常 |
| | | else if (hasException[f] == true) { |
| | | // 有异常出现时,记录异常数据 |
| | | tag.addExceptionData(data) |
| | | // 当立即判断通过时,形成异常结果 |
| | | if (immeExcCheck(tag, f)) { |
| | | recordException(s, tag, data) |
| | | } |
| | | } |
| | | // 数据正常,刷新起始点数据,移除历史异常记录 |
| | | else { |
| | | tag.refreshWithNextException(data) |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | fun recordException(factor: FactorFilter.SelectedFactor, tag: T, data: BaseRealTimeData) { |
| | | checkResult(factor, ExceptionStatusType.Ended) |
| | | tag.refreshWithNextException(data) |
| | | // tag.refreshWithNextException(data) |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | 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) { |
| | |
| | | open fun onNewException(tag: T, factor: FactorFilter.SelectedFactor, exceptionStatus: ExceptionStatusType) { |
| | | if (tag.startData == null) return |
| | | // val ex = newResult(tag.startData!!, tag.endData, factor, tag.exceptionData) |
| | | val ex = newResult(tag, factor) |
| | | .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) |
| | | // val ex = newResult(tag, factor) |
| | | // .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) |
| | | // } |
| | | // // 异常未创建时,新建异常信息 |
| | | // 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(tag: T, factor: FactorFilter.SelectedFactor): Y |
| | | |
| | | abstract fun newResult(exceptions: List<Pair<FactorFilter.SelectedFactor, ExceptionTag>>): Y |
| | | |
| | | } |