feiyu02
2023-12-01 c6842e8498c2d9b469890b38cd9f0d714392c445
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.flightfeather.monitor.analysis.dust
 
import com.flightfeather.monitor.analysis.dust.exception.*
import com.flightfeather.monitor.domain.ds1.entity.DustExceptionData
import com.flightfeather.monitor.domain.ds1.repository.DustExceptionDataRep
import com.flightfeather.monitor.domain.ds1.repository.DustExceptionSettingRep
import com.flightfeather.monitor.domain.ds1.repository.DustSiteDataRep
import com.flightfeather.monitor.domain.ds1.repository.DustSiteStatusRep
import com.flightfeather.monitor.enumration.dust.DeviceStatus
import com.flightfeather.monitor.utils.DateUtil
import org.springframework.stereotype.Component
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
 
/**
 * 数据异常分析控制器
 */
@Component
class ExceptionAnalysisController(
    private val dustExceptionDataRep: DustExceptionDataRep,
    private val dustExceptionSettingRep: DustExceptionSettingRep,
    private val dustSiteDataRep: DustSiteDataRep,
    private val dustSiteStatusRep: DustSiteStatusRep,
) {
 
    var running = false
 
    private val taskList = mutableListOf<BaseDustExceptionAnalysis>()
 
    fun init() {
        dustExceptionSettingRep.findLatestSetting("金山区")?.let {
            taskList.clear()
            taskList.apply {
                add(ExceptionDataMissing(it))
                add(ExceptionNoFluctuation(it))
                add(ExceptionApproachExceeding(it))
                add(ExceptionExceedingTimes(it))
                add(ExceptionSlideAverage(it))
                add(ExceptionValueMutation(it))
                add(ExceptionDataLowValue(it))
                add(ExceptionDataExceed(it))
                add(ExceptionValidRate(it))
            }
        }
    }
 
    /**
     * 自动运行时的处理逻辑
     * 分析昨天的数据
     */
    fun autoRun() {
        val data = dustExceptionDataRep.findLatestData()
        val yesterday = LocalDate.now().minusDays(1)
        if (data == null) {
            // FIXME: 2023/10/23 应该是将所有数据进行统计
            run(yesterday)
        } else {
            val date = LocalDateTime.ofInstant(data.endTime.toInstant(), ZoneId.systemDefault())
            val sT = if (date.hour == 0 && date.minute == 0 && date.second == 0) {
                date.toLocalDate()
            } else {
                date.plusDays(1).toLocalDate()
            }
            val du = DateUtil.findDurationDate(sT, yesterday)
            du.forEach {
                run(it)
            }
        }
    }
 
    fun run(date: LocalDate) {
        running = true
        // 判断对应日期的异常分析是否存在
//        if (dustExceptionDataRep.findDataExist(date)) return
        // 获取所有当前上线的设备
        val result = mutableListOf<DustExceptionData>()
        dustSiteStatusRep.select(listOf(DeviceStatus.ONLINE, DeviceStatus.STOP)).forEach { s ->
            s?.let {
                taskList.forEach { it.init() }
                // 轮询数据,计算各个异常
                dustSiteDataRep.select(s.mnCode, date).takeIf { it.isNotEmpty() }?.forEach {d ->
                    d?.let { taskList.forEach { it.onNextData(d) } }
                }
                // 各个异常分析分别结束
                taskList.forEach { it.onDone() }
                // 存储分析结果
                taskList.forEach {
                    result.addAll(it.resultList)
                }
            }
        }
        // 所有分析结果入库
        if (result.isNotEmpty()) {
            dustExceptionDataRep.insert(result)
        }
        running = false
    }
 
    fun debugRun() {
        val result = mutableListOf<DustExceptionData>()
        val date = LocalDate.of(2023, 11, 22)
        taskList.forEach { it.init() }
        // 轮询数据,计算各个异常
        val list = dustSiteDataRep.select("SHKJ0JS0150917", date)
        list.takeIf { it.isNotEmpty() }?.forEach { d ->
            d?.let { taskList.forEach { it.onNextData(d) } }
        }
        // 各个异常分析分别结束
        taskList.forEach { it.onDone() }
        // 存储分析结果
        taskList.forEach { result.addAll(it.resultList) }
        println(result)
    }
}