From aa94ef4e1f060b184e6ea9fb37254b376a1eb60e Mon Sep 17 00:00:00 2001
From: feiyu02 <risaku@163.com>
Date: 星期二, 14 十一月 2023 10:49:06 +0800
Subject: [PATCH] 1. 新增风险值的自动运行逻辑;

---
 src/main/java/com/flightfeather/monitor/scheduledtasks/TaskController.kt               |    8 +
 src/main/java/com/flightfeather/monitor/scheduledtasks/dust/RiskDailyAnalysisTask.kt   |   58 ++++++++++++++
 src/main/java/com/flightfeather/monitor/analysis/dust/risk/DustRiskAnalysis.kt         |   30 ++++---
 src/main/java/com/flightfeather/monitor/analysis/dust/StatisticAnalysisController.kt   |    4 
 src/test/java/com/flightfeather/monitor/analysis/dust/RiskAnalysisControllerTest.kt    |   15 ++-
 src/main/java/com/flightfeather/monitor/scheduledtasks/dust/BaseDustTimingTask.kt      |    2 
 src/main/java/com/flightfeather/monitor/scheduledtasks/dust/RiskMonthlyAnalysisTask.kt |   46 +++++++++++
 src/main/java/com/flightfeather/monitor/analysis/dust/risk/DustRiskMonthAnalysis.kt    |   20 +++--
 src/main/java/com/flightfeather/monitor/analysis/dust/ExceptionAnalysisController.kt   |    2 
 src/main/java/com/flightfeather/monitor/analysis/dust/RiskAnalysisController.kt        |    8 +-
 10 files changed, 154 insertions(+), 39 deletions(-)

diff --git a/src/main/java/com/flightfeather/monitor/analysis/dust/ExceptionAnalysisController.kt b/src/main/java/com/flightfeather/monitor/analysis/dust/ExceptionAnalysisController.kt
index b941cda..d2bb044 100644
--- a/src/main/java/com/flightfeather/monitor/analysis/dust/ExceptionAnalysisController.kt
+++ b/src/main/java/com/flightfeather/monitor/analysis/dust/ExceptionAnalysisController.kt
@@ -24,7 +24,7 @@
     private val dustSiteStatusRep: DustSiteStatusRep,
 ) {
 
-    private var running = false
+    var running = false
 
     private val taskList = mutableListOf<BaseDustExceptionAnalysis>()
 
diff --git a/src/main/java/com/flightfeather/monitor/analysis/dust/RiskAnalysisController.kt b/src/main/java/com/flightfeather/monitor/analysis/dust/RiskAnalysisController.kt
index afc6bc1..fb01c98 100644
--- a/src/main/java/com/flightfeather/monitor/analysis/dust/RiskAnalysisController.kt
+++ b/src/main/java/com/flightfeather/monitor/analysis/dust/RiskAnalysisController.kt
@@ -22,8 +22,8 @@
     private val dustStatisticValueRep: DustStatisticsValueRep,
 ) {
 
-    private var running1 = false
-    private var running2 = false
+    var running1 = false
+    var running2 = false
 
     private var taskDaily: DustRiskAnalysis? = null
     private var taskMonthly: DustRiskMonthAnalysis? = null
@@ -75,7 +75,7 @@
         // 鑾峰彇鎵�鏈夊綋鍓嶄笂绾垮拰鍋滆繍鐨勮澶�
         dustSiteStatusRep.select(listOf(DeviceStatus.ONLINE, DeviceStatus.STOP)).forEach { s ->
             s?.let {
-                taskDaily?.roundInit()
+                taskDaily?.roundInit(s.mnCode, date)
                 val dustSiteDataList = dustSiteDataRep.select(s.mnCode, date)
                 val dustExceptionDataList = dustExceptionDataRep.select(s.mnCode, date)
                 val dustStatisticValueList = dustStatisticValueRep.select(s.mnCode, date)
@@ -95,7 +95,7 @@
         taskMonthly?.init()
         dustSiteStatusRep.select(listOf(DeviceStatus.ONLINE, DeviceStatus.STOP)).forEach { s ->
             s?.let {
-                taskMonthly?.roundInit()
+                taskMonthly?.roundInit(s.mnCode, date)
                 val riskValueList = riskValueRep.select(s.mnCode, date)
                 taskMonthly?.roundCal(riskValueList)
             }
diff --git a/src/main/java/com/flightfeather/monitor/analysis/dust/StatisticAnalysisController.kt b/src/main/java/com/flightfeather/monitor/analysis/dust/StatisticAnalysisController.kt
index e453da4..9a23541 100644
--- a/src/main/java/com/flightfeather/monitor/analysis/dust/StatisticAnalysisController.kt
+++ b/src/main/java/com/flightfeather/monitor/analysis/dust/StatisticAnalysisController.kt
@@ -13,8 +13,8 @@
 @Component
 class StatisticAnalysisController(private val dustStatisticsValueRep: DustStatisticsValueRep) {
 
-    private var running1 = false
-    private var running2 = false
+    var running1 = false
+    var running2 = false
 
     /**
      *
diff --git a/src/main/java/com/flightfeather/monitor/analysis/dust/risk/DustRiskAnalysis.kt b/src/main/java/com/flightfeather/monitor/analysis/dust/risk/DustRiskAnalysis.kt
index df364e2..3e05493 100644
--- a/src/main/java/com/flightfeather/monitor/analysis/dust/risk/DustRiskAnalysis.kt
+++ b/src/main/java/com/flightfeather/monitor/analysis/dust/risk/DustRiskAnalysis.kt
@@ -2,6 +2,9 @@
 
 import com.flightfeather.monitor.domain.ds1.entity.*
 import com.flightfeather.monitor.enumration.dust.ExceptionType
+import java.time.LocalDate
+import java.time.ZoneId
+import java.util.*
 
 /**
  * 鎵皹鐩戞祴鏁版嵁椋庨櫓鍒嗘瀽鍩虹被
@@ -21,8 +24,12 @@
     /**
      * 姣忚疆娆$殑鍒濆鍖�
      */
-    fun roundInit() {
-        resultList.add(RiskValue())
+    fun roundInit(mnCode: String, date: LocalDate) {
+        resultList.add(RiskValue().apply {
+            this.mnCode = mnCode
+            lst = Date.from(date.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant())
+            type = "day"
+        })
     }
 
     /**
@@ -37,7 +44,6 @@
         riskExceedRate(res, list)
         riskExceptionRepetitionRate(res, dList)
         riskExceptionType(res, dList)
-        onDone(res)
     }
 
     /**
@@ -66,7 +72,7 @@
         var v2 = 0
         // 鏃ヤ复杩戣秴鏍囨鏁�/7
         var v3 = 0
-        list.forEach {d ->
+        list.forEach { d ->
             if (d == null) return@forEach
             when {
                 d.dustValue >= 2.0 -> v2++
@@ -92,7 +98,7 @@
         var v1 = 0
         var v2 = 0
         var v3 = 0
-        list.forEach { d->
+        list.forEach { d ->
             if (d == null) return@forEach
             when (d.exceptionType) {
                 ExceptionType.TYPE4.value -> v1++
@@ -100,10 +106,10 @@
                 ExceptionType.TYPE6.value -> v3++
             }
         }
-        val e1 = upperLimit((v1 - 1) / 4.0)
-        val e2 = upperLimit((v2 - 1) / 3.0)
-        val e3 = upperLimit((v3 - 1) / 2.0)
-        riskValue.typicalExceptionRepetitionRate = upperLimit(e1 + e2 + e3)
+        val e1 = settingRange((v1 - 1) / 4.0)
+        val e2 = settingRange((v2 - 1) / 3.0)
+        val e3 = settingRange((v3 - 1) / 2.0)
+        riskValue.typicalExceptionRepetitionRate = settingRange(e1 + e2 + e3)
     }
 
     /**
@@ -125,9 +131,5 @@
         riskValue.exceptionTypeAggregation = exceptionTypes.size / 6.0
     }
 
-    fun onDone(riskValue: RiskValue) {
-        riskValue.type = "day"
-    }
-
-    private fun upperLimit(d:Double):Double = if (d > 1) 1.0  else d
+    private fun settingRange(d: Double): Double = if (d > 1) 1.0 else if (d < 0) .0 else d
 }
\ No newline at end of file
diff --git a/src/main/java/com/flightfeather/monitor/analysis/dust/risk/DustRiskMonthAnalysis.kt b/src/main/java/com/flightfeather/monitor/analysis/dust/risk/DustRiskMonthAnalysis.kt
index 4ba6551..e2efa14 100644
--- a/src/main/java/com/flightfeather/monitor/analysis/dust/risk/DustRiskMonthAnalysis.kt
+++ b/src/main/java/com/flightfeather/monitor/analysis/dust/risk/DustRiskMonthAnalysis.kt
@@ -1,6 +1,9 @@
 package com.flightfeather.monitor.analysis.dust.risk
 
 import com.flightfeather.monitor.domain.ds1.entity.*
+import java.time.LocalDate
+import java.time.ZoneId
+import java.util.*
 
 class DustRiskMonthAnalysis(val config: DustExceptionSetting) {
 
@@ -17,8 +20,12 @@
     /**
      * 姣忚疆娆$殑鍒濆鍖�
      */
-    fun roundInit() {
-        resultList.add(RiskValue())
+    fun roundInit(mnCode: String, date: LocalDate) {
+        resultList.add(RiskValue().apply {
+            this.mnCode = mnCode
+            lst = Date.from(date.withDayOfMonth(1).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant())
+            type = "month"
+        })
     }
 
     /**
@@ -34,7 +41,6 @@
         riskExceedRate(res, list)
         riskExceptionRepetitionRate(res, list)
         riskExceptionType(res, list)
-        onDone(res)
     }
 
     fun riskOnlineRate(riskValue: RiskValue, list: List<RiskValue?>) {
@@ -57,16 +63,14 @@
         riskValue.exceptionTypeAggregation = avg(list) { it.exceptionTypeAggregation }
     }
 
-    fun onDone(riskValue: RiskValue) {
-        riskValue.type = "month"
-    }
 
-    private fun avg(list: List<RiskValue?>, onNext: (value: RiskValue) -> Double): Double {
+    private fun avg(list: List<RiskValue?>, onNext: (value: RiskValue) -> Double?): Double {
         var total = .0
         var count = 0
         list.forEach {
             if (it == null) return@forEach
-            total += onNext(it)
+            val a = onNext(it) ?: return@forEach
+            total += a
             count++
         }
         return if (count == 0) .0 else total / count
diff --git a/src/main/java/com/flightfeather/monitor/scheduledtasks/TaskController.kt b/src/main/java/com/flightfeather/monitor/scheduledtasks/TaskController.kt
index e104113..99108fe 100644
--- a/src/main/java/com/flightfeather/monitor/scheduledtasks/TaskController.kt
+++ b/src/main/java/com/flightfeather/monitor/scheduledtasks/TaskController.kt
@@ -1,8 +1,6 @@
 package com.flightfeather.monitor.scheduledtasks
 
-import com.flightfeather.monitor.scheduledtasks.dust.DustDailyAnalysisTask
-import com.flightfeather.monitor.scheduledtasks.dust.DustExceptionAnalysisTask
-import com.flightfeather.monitor.scheduledtasks.dust.DustMonthlyAnalysisTask
+import com.flightfeather.monitor.scheduledtasks.dust.*
 import org.slf4j.Logger
 import org.slf4j.LoggerFactory
 import org.springframework.stereotype.Component
@@ -21,6 +19,8 @@
     dustExceptionAnalysisTask: DustExceptionAnalysisTask,
     dustDailyAnalysisTask: DustDailyAnalysisTask,
     dustMonthlyAnalysisTask: DustMonthlyAnalysisTask,
+    riskDailyAnalysisTask: RiskDailyAnalysisTask,
+    riskMonthlyAnalysisTask: RiskMonthlyAnalysisTask,
 ) {
 
     companion object {
@@ -41,6 +41,8 @@
         timeTask.add(dustExceptionAnalysisTask)
         timeTask.add(dustDailyAnalysisTask)
         timeTask.add(dustMonthlyAnalysisTask)
+        timeTask.add(riskDailyAnalysisTask)
+        timeTask.add(riskMonthlyAnalysisTask)
         log.info("娣诲姞瀹氭椂浠诲姟瀹屾垚锛屼换鍔℃�昏${timeTask.size}涓�")
     }
 
diff --git a/src/main/java/com/flightfeather/monitor/scheduledtasks/dust/BaseDustTimingTask.kt b/src/main/java/com/flightfeather/monitor/scheduledtasks/dust/BaseDustTimingTask.kt
index dde110c..7ab4b84 100644
--- a/src/main/java/com/flightfeather/monitor/scheduledtasks/dust/BaseDustTimingTask.kt
+++ b/src/main/java/com/flightfeather/monitor/scheduledtasks/dust/BaseDustTimingTask.kt
@@ -19,7 +19,7 @@
     /**
      * 褰撲换鍔″紑濮嬫墽琛屾椂锛屽垽鏂换鍔$姸鎬侊紝鍐冲畾鏄惁寤惰繜鎵ц
      */
-    protected fun isTaskDelay(localtime: LocalDateTime): Boolean {
+    open fun isTaskDelay(localtime: LocalDateTime): Boolean {
         val task = requestTaskRep.findLatestTask(localtime)
         // 浠诲姟姝e湪鎵ц锛岃烦杩囨娆″垎鏋愶紝鍦ㄤ笅涓�鍛ㄦ湡缁х画鍒ゆ柇锛岀瓑寰呬换鍔″畬鎴愬悗杩涜鍒嗘瀽
         return if (task == null || task.runningStatus) {
diff --git a/src/main/java/com/flightfeather/monitor/scheduledtasks/dust/RiskDailyAnalysisTask.kt b/src/main/java/com/flightfeather/monitor/scheduledtasks/dust/RiskDailyAnalysisTask.kt
new file mode 100644
index 0000000..1f1e24f
--- /dev/null
+++ b/src/main/java/com/flightfeather/monitor/scheduledtasks/dust/RiskDailyAnalysisTask.kt
@@ -0,0 +1,58 @@
+package com.flightfeather.monitor.scheduledtasks.dust
+
+import com.flightfeather.monitor.analysis.dust.ExceptionAnalysisController
+import com.flightfeather.monitor.analysis.dust.RiskAnalysisController
+import com.flightfeather.monitor.analysis.dust.StatisticAnalysisController
+import com.flightfeather.monitor.domain.ds1.repository.DustExceptionDataRep
+import com.flightfeather.monitor.domain.ds1.repository.DustStatisticsValueRep
+import com.flightfeather.monitor.domain.ds1.repository.RequestTaskRep
+import org.springframework.stereotype.Component
+import java.time.LocalDate
+import java.time.LocalDateTime
+import java.time.ZoneId
+
+/**
+ * 鎵皹鏃ラ闄╁垎鏋愪换鍔�
+ */
+@Component
+class RiskDailyAnalysisTask(
+    private val riskAnalysisController: RiskAnalysisController,
+    private val exceptionAnalysisController: ExceptionAnalysisController,
+    private val statisticAnalysisController: StatisticAnalysisController,
+    private val dustExceptionDataRep: DustExceptionDataRep,
+    private val dustStatisticsValueRep: DustStatisticsValueRep,
+    requestTaskRep: RequestTaskRep,
+) : BaseDustTimingTask(requestTaskRep) {
+
+    override fun doTask(localtime: LocalDateTime) {
+        if (taskDelay || (localtime.hour == 10 && localtime.minute == 0)) {
+            taskDelay = isTaskDelay(localtime)
+            if (!taskDelay) {
+                log.info("鏃ラ闄╁垎鏋愭墽琛�")
+                riskAnalysisController.autoRunDaily()
+            }
+        }
+    }
+
+    /**
+     * 褰撴槰鏃ョ殑寮傚父鍒嗘瀽鍜屾棩缁熻浠诲姟閮藉畬鎴愬悗锛屾墠鎵ц鏃ラ闄╁垎鏋愪换鍔★紝鍚﹀垯寤惰繜鎵ц
+     * @param localtime 褰撳墠鏃堕棿
+     * @return 鏄惁寤惰繜鎵ц
+     */
+    override fun isTaskDelay(localtime: LocalDateTime): Boolean {
+        val yesterday = LocalDate.now().minusDays(1)
+        // 妫�鏌ュ紓甯稿垎鏋愯褰曠殑鏈�鏂版椂闂村拰寮傚父鍒嗘瀽浠诲姟鐨勮繍琛岀姸鎬�
+        val eData = dustExceptionDataRep.findLatestData() ?: return true
+        val eDataTime = LocalDateTime.ofInstant(eData.endTime.toInstant(), ZoneId.systemDefault()).toLocalDate()
+        val b1 = yesterday.isEqual(eDataTime)
+        val b2 = exceptionAnalysisController.running
+        // 妫�鏌ユ棩缁熻鍒嗘瀽璁板綍鐨勬渶鏂版椂闂村拰鏃ョ粺璁″垎鏋愪换鍔$殑杩愯鐘舵��
+        val sData = dustStatisticsValueRep.findLatestData("day") ?: return true
+        val sDataTime = LocalDateTime.ofInstant(sData.lst.toInstant(), ZoneId.systemDefault()).toLocalDate()
+        val b3 = yesterday.isEqual(sDataTime)
+        val b4 = statisticAnalysisController.running1
+
+        return !(b1 && !b2 && b3 && !b4)
+
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/com/flightfeather/monitor/scheduledtasks/dust/RiskMonthlyAnalysisTask.kt b/src/main/java/com/flightfeather/monitor/scheduledtasks/dust/RiskMonthlyAnalysisTask.kt
new file mode 100644
index 0000000..fa70d2e
--- /dev/null
+++ b/src/main/java/com/flightfeather/monitor/scheduledtasks/dust/RiskMonthlyAnalysisTask.kt
@@ -0,0 +1,46 @@
+package com.flightfeather.monitor.scheduledtasks.dust
+
+import com.flightfeather.monitor.analysis.dust.RiskAnalysisController
+import com.flightfeather.monitor.domain.ds1.repository.RequestTaskRep
+import com.flightfeather.monitor.domain.ds1.repository.RiskValueRep
+import org.springframework.stereotype.Component
+import java.time.LocalDate
+import java.time.LocalDateTime
+import java.time.ZoneId
+
+/**
+ * 鎵皹鏈堥闄╁垎鏋愪换鍔�
+ */
+@Component
+class RiskMonthlyAnalysisTask(
+    private val riskAnalysisController: RiskAnalysisController,
+    private val riskValueRep: RiskValueRep,
+    requestTaskRep: RequestTaskRep,
+) : BaseDustTimingTask(requestTaskRep) {
+
+    override fun doTask(localtime: LocalDateTime) {
+        if (taskDelay || (localtime.dayOfMonth == 1 && localtime.hour == 11 && localtime.minute == 0)) {
+            taskDelay = isTaskDelay(localtime)
+            if (!taskDelay) {
+                log.info("鏈堥闄╁垎鏋愭墽琛�")
+                riskAnalysisController.autoRunMonthly()
+            }
+        }
+    }
+
+    /**
+     * 褰撲笂鏈堝簳鐨勬棩椋庨櫓鍒嗘瀽瀹屾垚鍚庯紝鎵嶆墽琛屾湀椋庨櫓鍒嗘瀽浠诲姟锛屽惁鍒欏欢杩熸墽琛�
+     * @param localtime 褰撳墠鏃堕棿
+     * @return 鏄惁寤惰繜鎵ц
+     */
+    override fun isTaskDelay(localtime: LocalDateTime): Boolean {
+        val lastMonth = LocalDate.now().withDayOfMonth(1).minusDays(1)
+        val data = riskValueRep.findLatestData("day") ?: return true
+        val dataTime = LocalDateTime.ofInstant(data.lst.toInstant(), ZoneId.systemDefault()).toLocalDate()
+        val b1 = lastMonth.isEqual(dataTime)
+        val b2 = riskAnalysisController.running1
+
+        return !(b1 && !b2)
+
+    }
+}
\ No newline at end of file
diff --git a/src/test/java/com/flightfeather/monitor/analysis/dust/RiskAnalysisControllerTest.kt b/src/test/java/com/flightfeather/monitor/analysis/dust/RiskAnalysisControllerTest.kt
index 27e3781..839524d 100644
--- a/src/test/java/com/flightfeather/monitor/analysis/dust/RiskAnalysisControllerTest.kt
+++ b/src/test/java/com/flightfeather/monitor/analysis/dust/RiskAnalysisControllerTest.kt
@@ -20,16 +20,19 @@
 
     @Test
     fun autoRunDaily() {
-        val s = LocalDate.of(2023, 10, 1)
-        val e = LocalDate.of(2023, 10, 31)
-        val du = DateUtil.findDurationDate(s, e)
-        du.forEach {
-            riskAnalysisController.runDaily(it)
-        }
+        riskAnalysisController.init()
+//        val s = LocalDate.of(2023, 10, 1)
+//        val e = LocalDate.of(2023, 10, 31)
+//        val du = DateUtil.findDurationDate(s, e)
+//        du.forEach {
+//            riskAnalysisController.runDaily(it)
+//        }
+        riskAnalysisController.autoRunDaily()
     }
 
     @Test
     fun autoRunMonthly() {
+        riskAnalysisController.init()
         riskAnalysisController.autoRunMonthly()
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3