| | |
| | | package com.flightfeather.uav.biz.dataanalysis |
| | | |
| | | import com.flightfeather.uav.biz.FactorFilter |
| | | import com.flightfeather.uav.biz.dataanalysis.model.DataAnalysisConfig |
| | | import com.flightfeather.uav.domain.entity.BaseRealTimeData |
| | | import com.flightfeather.uav.socket.eunm.FactorType |
| | | import java.time.Duration |
| | | |
| | | /** |
| | | * 连续类型的异常分析基类,适用于当前数据与相邻数据之间有关联关系的情况 |
| | | */ |
| | | abstract class BaseExceptionContinuous(config: DataAnalysisConfig) : BaseExceptionAnalysis(config) { |
| | | abstract class BaseExceptionContinuous<V : BaseAnalysisConfig, Y : BaseExceptionResult>(config: V) : |
| | | BaseExceptionAnalysis<V, Y>(config) { |
| | | |
| | | companion object { |
| | | // 记录异常数据段时,分别向起始前和末尾后额外记录的数据个数偏移量 |
| | |
| | | var sIndex = 0 |
| | | |
| | | // 起始数据对象 |
| | | var startData :BaseRealTimeData? = null |
| | | var startData: BaseRealTimeData? = null |
| | | |
| | | // 末尾数据下标 |
| | | var eIndex = -1 |
| | |
| | | sIndex = eIndex |
| | | startData = data |
| | | exceptionData.clear() |
| | | exceptionData.add(data) |
| | | // exceptionData.add(data) |
| | | } |
| | | } |
| | | |
| | | protected val tagMap = mutableMapOf<FactorType, Tag>() |
| | | |
| | | // // 起始数据下标 |
| | | // protected var sIndex = mutableListOf<Int>() |
| | | // |
| | | // // 起始数据对象 |
| | | // protected var startData = mutableListOf<BaseRealTimeData?>() |
| | | // |
| | | // // 末尾数据下标 |
| | | // protected var eIndex = mutableListOf<Int>() |
| | | // |
| | | // // 异常数据段 |
| | | // protected var exceptionData = mutableListOf<MutableList<BaseRealTimeData>>() |
| | | |
| | | // protected var existException = mutableListOf<Boolean>() |
| | | |
| | | // 起始数据与末尾数据间隔 |
| | | open var durationCount = 1 |
| | | |
| | | // 末尾数据对象 |
| | | protected var lastData: BaseRealTimeData? = null |
| | | |
| | | /** |
| | | * 判断相邻数据是否连续 |
| | | */ |
| | | open fun isContinuous(d1: BaseRealTimeData?, d2: BaseRealTimeData?): Boolean { |
| | | if (d1 == null || d2 == null) return true |
| | | |
| | | val t1 = d1.dataTime |
| | | val t2 = d2.dataTime |
| | | return Duration.between(t1?.toInstant(), t2?.toInstant()).toMillis() <= (20 * 1000) |
| | | } |
| | | |
| | | /** |
| | | * 判断是否满足异常条件 |
| | |
| | | abstract fun judgeException(p: BaseRealTimeData?, n: BaseRealTimeData): MutableMap<FactorType, Boolean> |
| | | |
| | | /** |
| | | * 判断异常出现的连续时长是否满足条件 |
| | | * 判断异常出现的连续个数是否满足条件 |
| | | * @param tag 异常数据对象 |
| | | */ |
| | | abstract fun judgeDuration(sIndex: Int, eIndex: Int): Boolean |
| | | abstract fun judgeExceptionCount(tag: Tag): Boolean |
| | | |
| | | /** |
| | | * 异常数据的截取判断 |
| | | * 是否需要限制一组异常数据的长度 |
| | | * @return 默认不需要截取 |
| | | */ |
| | | open fun needCut(tag: Tag): Boolean { |
| | | return false |
| | | } |
| | | |
| | | override fun init() { |
| | | super.init() |
| | | lastData = null |
| | | // repeat(config.factorCount) { |
| | | // startData.add(null) |
| | | // sIndex.add(0) |
| | | // eIndex.add(-1) |
| | | // existException.add(false) |
| | | // exceptionData.add(mutableListOf()) |
| | | // } |
| | | config.factorFilter.mainList().forEach {f-> |
| | | tagMap.clear() |
| | | config.factorFilter.mainList().forEach { f -> |
| | | tagMap[f] = Tag() |
| | | } |
| | | } |
| | |
| | | override fun onNextData(data: BaseRealTimeData) { |
| | | val isContinue = isContinuous(lastData, data) |
| | | val hasException = judgeException(lastData, data) |
| | | config.factorFilter.mainList().forEach {f-> |
| | | config.factorFilter.selectedList.forEach { s -> |
| | | val f = s.main |
| | | tagMap[f]?.let { |
| | | it.eIndex++ |
| | | // 起始数据 |
| | | if (it.endData == null) { |
| | | it.endData = data |
| | | if (it.startData == null) { |
| | | it.refreshAfterCheckResult(data) |
| | | } |
| | | // 判断相邻数据是否连续并且是否满足异常判断 |
| | | if (!isContinue) { |
| | | checkResult() |
| | | if (!isContinue || needCut(it)) { |
| | | // 数据不连续时,记录异常情况 |
| | | if (it.eIndex - it.sIndex >= durationCount) { |
| | | it.refreshAfterCheckResult(data) |
| | | } |
| | | recordException(s, it, data) |
| | | // checkResult(s) |
| | | // if (it.eIndex - it.sIndex >= durationCount) { |
| | | // it.refreshAfterCheckResult(data) |
| | | // } |
| | | } else { |
| | | if (hasException[f] == true) { |
| | | it.existException = true |
| | | it.exceptionData.add(data) |
| | | } else { |
| | | // 异常不再重复出现时,记录异常情况 |
| | | checkResult() |
| | | if (it.eIndex - it.sIndex >= durationCount) { |
| | | it.refreshAfterCheckResult(data) |
| | | } |
| | | recordException(s, it, data) |
| | | // checkResult(s) |
| | | // if (it.eIndex - it.sIndex >= durationCount) { |
| | | // it.refreshAfterCheckResult(data) |
| | | // } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | // repeat(config.factorCount) { i -> |
| | | // eIndex[i]++ |
| | | // // 起始数据 |
| | | // if (lastData == null) { |
| | | // refreshAfterCheckResult(i, data) |
| | | // } |
| | | // // 判断相邻数据是否连续并且是否满足异常判断 |
| | | // if (!isContinue) { |
| | | // checkResult() |
| | | // // 数据不连续时,记录异常情况 |
| | | // if (eIndex[i] - sIndex[i] >= durationCount) { |
| | | // refreshAfterCheckResult(i, data) |
| | | // } |
| | | // } else { |
| | | // if (hasException[i]) { |
| | | // existException[i] = true |
| | | // exceptionData[i].add(data) |
| | | // } else { |
| | | // // 异常不再重复出现时,记录异常情况 |
| | | // checkResult() |
| | | // if (eIndex[i] - sIndex[i] >= durationCount) { |
| | | // refreshAfterCheckResult(i, data) |
| | | // } |
| | | // } |
| | | // } |
| | | // } |
| | | lastData = data |
| | | } |
| | | |
| | |
| | | checkResult() |
| | | } |
| | | |
| | | // fun refreshAfterCheckResult(i:Int, data: BaseRealTimeData) { |
| | | // sIndex[i] = eIndex[i] |
| | | // startData[i] = data |
| | | // exceptionData[i].clear() |
| | | // exceptionData[i].add(data) |
| | | // } |
| | | fun recordException(factor: FactorFilter.SelectedFactor, tag: Tag, data: BaseRealTimeData) { |
| | | checkResult(factor) |
| | | if (tag.eIndex - tag.sIndex >= durationCount) { |
| | | tag.refreshAfterCheckResult(data) |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 检查连续异常结束时,是否符合异常存储条件 |
| | |
| | | open fun checkResult(factor: FactorFilter.SelectedFactor? = null) { |
| | | val tag = tagMap[factor?.main] |
| | | if (factor != null && tag != null) { |
| | | if (tag.existException && judgeDuration(tag.sIndex, tag.eIndex)) { |
| | | tag.startData?.let { |
| | | resultList.add(newResult(it, lastData, factor, tag.exceptionData)) |
| | | } |
| | | tag.existException = false |
| | | if (tag.existException && judgeExceptionCount(tag)) { |
| | | onNewException(tag, factor) |
| | | } |
| | | } else { |
| | | config.factorFilter.selectedList.forEach { f -> |
| | | val tag1 = tagMap[f.main] ?: return@forEach |
| | | if (tag1.existException && judgeDuration(tag1.sIndex, tag1.eIndex)) { |
| | | tag1.startData?.let { |
| | | resultList.add(newResult(it, lastData, f, tag1.exceptionData)) |
| | | } |
| | | tag1.existException = false |
| | | if (tag1.existException && judgeExceptionCount(tag1)) { |
| | | onNewException(tag1, f) |
| | | } |
| | | } |
| | | // repeat(config.factorCount) { i -> |
| | | // if (existException[i] && judgeDuration(sIndex[i], eIndex[i])) { |
| | | // startData[i]?.let { |
| | | // resultList.add(newResult(it, lastData, i, exceptionData[i])) |
| | | // } |
| | | // existException[i] = false |
| | | // } |
| | | // } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 新增一条异常 |
| | | */ |
| | | open fun onNewException(tag:Tag, factor: FactorFilter.SelectedFactor) { |
| | | tag.startData?.let { |
| | | resultList.add(newResult(it, lastData, factor, tag.exceptionData)) |
| | | } |
| | | tag.existException = false |
| | | } |
| | | |
| | | /** |
| | | * 生成一条异常分析结果 |
| | | */ |
| | | abstract fun newResult( |
| | | start: BaseRealTimeData, |
| | | end: BaseRealTimeData?, |
| | | factor: FactorFilter.SelectedFactor, |
| | | exceptionData: List<BaseRealTimeData>, |
| | | ): Y |
| | | |
| | | } |