feiyu02
2023-11-23 e2392116cd6f875cdc2f46bc04b04d5305f21b56
src/main/java/com/flightfeather/monitor/analysis/dust/exception/ExceptionSlideAverage.kt
@@ -2,6 +2,7 @@
import com.flightfeather.monitor.domain.ds1.entity.DustExceptionSetting
import com.flightfeather.monitor.domain.ds1.entity.DustSiteData
import com.flightfeather.monitor.enumration.dust.DataStatus
import com.flightfeather.monitor.enumration.dust.ExceptionType
import kotlin.math.abs
@@ -12,7 +13,7 @@
    private val historyDataList = mutableListOf<DustSiteData>()
    private val tempDataList = mutableListOf<DustSiteData>()
    private val avgListReverse = mutableListOf<Double>()
    private val avgListReverse = mutableListOf<Pair<Double, Boolean>>()
    private var startData: DustSiteData? = null
    private var lastData: DustSiteData? = null
    private var sIndex = 0
@@ -67,11 +68,17 @@
     */
    private fun calAvg(list: List<DustSiteData>) {
        var total = .0
        var valid = true
        val count = list.size
        if (count == 0) return
        list.forEach { total += it.dustValue }
        list.forEach {
            total += it.dustValue
            if (it.flag != DataStatus.N.value) {
                valid = false
            }
        }
        val avg = total / count
        avgListReverse.add(0, avg)
        avgListReverse.add(0, Pair(avg, valid))
    }
    /**
@@ -84,14 +91,14 @@
            return false
        } else {
            // 滑动均值满足数量时,计算均值之间是否连续超过限定比率
            val rateList = mutableListOf<Double>()
            val rateList = mutableListOf<Pair<Double, Boolean>>()
            for (i in avgListReverse.indices) {
                if (i >= config.changeTrendTimes) break
                val r = calAvgChangeRate(avgListReverse[i], avgListReverse[i + config.changeTrendInterval])
                rateList.add(r)
            }
            for (y in rateList) {
                if (y < config.changeTrendRate) {
                if (!y.second || y.first < config.changeTrendRate) {
                    return false
                }
            }
@@ -102,11 +109,12 @@
    /**
     * 计算滑动均值变化率
     */
    private fun calAvgChangeRate(a1: Double, a2: Double): Double {
        return if (a2 == .0) {
            1.0
    private fun calAvgChangeRate(a1: Pair<Double, Boolean>, a2: Pair<Double, Boolean>): Pair<Double, Boolean> {
        val valid = a1.second && a2.second
        return if (a2.first == .0) {
            Pair(1.0, valid)
        } else {
            abs(a1 - a2) / a2
            Pair(abs(a1.first - a2.first) / a2.first, valid)
        }
    }