Jelajahi Sumber

完善站点小时分析的逻辑

andyliu 1 Minggu lalu
induk
melakukan
b153b96094

+ 20 - 1
src/main/java/com/shkpr/service/warncore/bizhandler/CommToolHandler.java

@@ -3,11 +3,15 @@ package com.shkpr.service.warncore.bizhandler;
 import com.shkpr.service.warncore.commtools.CommTool;
 import com.shkpr.service.warncore.dbdao.DBMgrProxy;
 import com.shkpr.service.warncore.dbdao.services.intef.OrdWarnEventInfoDBService;
+import com.shkpr.service.warncore.dbdao.services.intef.OrdWarnPlanInfoDBService;
 import com.shkpr.service.warncore.dbdao.tables.OrdWarnEventInfoTable;
+import com.shkpr.service.warncore.dbdao.tables.OrdWarnPlanInfoTable;
 import com.shkpr.service.warncore.dto.OrdWarnRuleCondition;
 import com.shkpr.service.warncore.dto.ResponseCode;
 import org.springframework.util.StringUtils;
 
+import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 public class CommToolHandler {
@@ -15,6 +19,10 @@ public class CommToolHandler {
         return DBMgrProxy.getInstance().applyXXXApi(OrdWarnEventInfoDBService.class);
     }
 
+    private static OrdWarnPlanInfoDBService getOrdWarnPlanDBService(){
+        return DBMgrProxy.getInstance().applyXXXApi(OrdWarnPlanInfoDBService.class);
+    }
+
     /**
      * 原始值匹配规则条件
      * @param originValue
@@ -63,7 +71,7 @@ public class CommToolHandler {
      *         1 -- 指定事件待处理;
      *         2 -- 指定事件已处理
      */
-    public static int checkEventAction(String eventId){
+    public static int checkOrdWarnEventAction(String eventId){
         int action = -1;
         Map<String, Object> infoDb = getOrdWarnEventDBService().fastGetFiled(OrdWarnEventInfoTable.W_INFO.TABLE
                 , OrdWarnEventInfoTable.W_INFO.ACTION, eventId);
@@ -77,4 +85,15 @@ public class CommToolHandler {
             action = 0;
         return action;
     }
+
+    public static ResponseCode updateOrdWarnPlanTriggerRecord(Map<String, Object> planWhere
+            , Map<String, Object> planData
+            , List<Map<String, Object>> newEvents
+            , List<Map<String, Object>> oldEvents){
+        ResponseCode code = ResponseCode.RESULT_BAD;
+        if (getOrdWarnPlanDBService().updatePlanForTrigger(planWhere, planData, newEvents, oldEvents) > 0){
+            code = ResponseCode.RESULT_NORMAL;
+        }
+        return code;
+    }
 }

+ 90 - 22
src/main/java/com/shkpr/service/warncore/bizhandler/SiteDataWarnHandler.java

@@ -153,7 +153,7 @@ public class SiteDataWarnHandler {
             }
 
             if (!StringUtils.isEmpty(planDetail.getLastEventId())){
-                int action = CommToolHandler.checkEventAction(planDetail.getLastEventId());
+                int action = CommToolHandler.checkOrdWarnEventAction(planDetail.getLastEventId());
                 if (action < 0){
                     step = "Last event check error,do analysis next time.";
                     code = ResponseCode.RESULT_BAD;
@@ -402,7 +402,9 @@ public class SiteDataWarnHandler {
         long curTime = TimeTool.getCurMsUTC();
         final String siteId = rules.get(0).getObjId();
         SiteStatsDataResult siteStatsData = null;
-        Map<String, Long> eventId2NewEndUTC = new HashMap<>();//需要更新预警事件的样本截止时间
+        Map<String, Long> oldEvent2EndUTC = new HashMap<>();//需要更新预警事件的样本截止时间
+        Map<String, JPOrdWarnEventInfo> newEvent2Gen = new HashMap<>();
+
         List<Long> tagTime = new ArrayList<>();//每个整点时刻,用于查询站点的统计数据
         long tmpStepToStep = thisTempStep.getQueryBeginUTC();
         do {
@@ -504,6 +506,7 @@ public class SiteDataWarnHandler {
                     thisTempStep.setFirstMatchSampleTime(0L);
                     thisTempStep.setLastEventId("");
                     thisTempStep.setLastEventTime(0L);
+                    thisTempStep.setLastCompareSampleExplain("This is not matched.");
                     continue;
                 }//本次匹配成功进入下一步
 
@@ -512,10 +515,15 @@ public class SiteDataWarnHandler {
                     thisTempStep.setLastCompareSampleTime(sampleRecordTm);
                     thisTempStep.setLastCompareSampleResult(COMPARE_TURE);
                     thisTempStep.setFirstMatchSampleTime(sampleRecordTm);
+                    thisTempStep.setLastCompareSampleExplain("This is matched, but last is not matched.");
                     if (triggerEventPeriodSwitch <= 1){//触发事件
-                        thisTempStep.setLastEventId("新事件ID");
-                        thisTempStep.setLastEventTime(curTime);
-                        thisTempStep.setLastEventAction(EventAction.DOING);
+                        thisTempStep.setLastEventTime(TimeTool.getCurMsUTC());
+                        thisTempStep.setLastCompareSampleExplain("This is matched and trigger short period, it will gen event.");
+
+                        JPOrdWarnEventInfo newEvent = new JPOrdWarnEventInfo(planDetail, rules, thisTempStep, thisTempStep.getLastEventTime());
+                        newEvent2Gen.put(newEvent.getUid(), newEvent);
+                        thisTempStep.setLastEventId(newEvent.getUid());
+                        thisTempStep.setLastEventAction(newEvent.getAction());
                         thisTempStep.setCycleTriggerTimes(thisTempStep.getCycleTriggerTimes()+1);
                     }
                     continue;
@@ -526,10 +534,15 @@ public class SiteDataWarnHandler {
                     thisTempStep.setLastCompareSampleTime(sampleRecordTm);
                     thisTempStep.setLastCompareSampleResult(COMPARE_TURE);
                     thisTempStep.setFirstMatchSampleTime(sampleRecordTm);
+                    thisTempStep.setLastCompareSampleExplain("This is matched, but two matched sample is too distance.");
                     if (triggerEventPeriodSwitch <= 1){//触发事件
-                        thisTempStep.setLastEventId("新事件ID");
-                        thisTempStep.setLastEventTime(curTime);
-                        thisTempStep.setLastEventAction(EventAction.DOING);
+                        thisTempStep.setLastEventTime(TimeTool.getCurMsUTC());
+                        thisTempStep.setLastCompareSampleExplain("This is matched and trigger short period, it will gen event.");
+
+                        JPOrdWarnEventInfo newEvent = new JPOrdWarnEventInfo(planDetail, rules, thisTempStep, thisTempStep.getLastEventTime());
+                        newEvent2Gen.put(newEvent.getUid(), newEvent);
+                        thisTempStep.setLastEventId(newEvent.getUid());
+                        thisTempStep.setLastEventAction(newEvent.getAction());
                         thisTempStep.setCycleTriggerTimes(thisTempStep.getCycleTriggerTimes()+1);
                     }else {
                         thisTempStep.setLastEventId("");
@@ -542,10 +555,15 @@ public class SiteDataWarnHandler {
                     thisTempStep.setLastCompareSampleTime(sampleRecordTm);
                     thisTempStep.setLastCompareSampleResult(COMPARE_TURE);
                     thisTempStep.setFirstMatchSampleTime(sampleRecordTm);
+                    thisTempStep.setLastCompareSampleExplain("This is matched, but last is not today.");
                     if (triggerEventPeriodSwitch <= 1){//触发事件
-                        thisTempStep.setLastEventId("新事件ID");
-                        thisTempStep.setLastEventTime(curTime);
-                        thisTempStep.setLastEventAction(EventAction.DOING);
+                        thisTempStep.setLastEventTime(TimeTool.getCurMsUTC());
+                        thisTempStep.setLastCompareSampleExplain("This is matched and trigger short period, it will gen event.");
+
+                        JPOrdWarnEventInfo newEvent = new JPOrdWarnEventInfo(planDetail, rules, thisTempStep, thisTempStep.getLastEventTime());
+                        newEvent2Gen.put(newEvent.getUid(), newEvent);
+                        thisTempStep.setLastEventId(newEvent.getUid());
+                        thisTempStep.setLastEventAction(newEvent.getAction());
                         thisTempStep.setCycleTriggerTimes(thisTempStep.getCycleTriggerTimes()+1);
                     }else {
                         thisTempStep.setLastEventId("");
@@ -559,33 +577,47 @@ public class SiteDataWarnHandler {
                     if (matchLen < triggerEventPeriodSwitch){//还得持续等待,达不到触发事件的累计时长
                         thisTempStep.setLastCompareSampleTime(sampleRecordTm);
                         thisTempStep.setLastCompareSampleResult(COMPARE_TURE);
+                        thisTempStep.setLastCompareSampleExplain("This is matched, but need wait the next matched.");
                         if (thisTempStep.getFirstMatchSampleTime() <= 0){
                             thisTempStep.setFirstMatchSampleTime(sampleRecordTm);
                             if (triggerEventPeriodSwitch <= 1){//补充触发事件
-                                thisTempStep.setLastEventId("新事件ID");
-                                thisTempStep.setLastEventTime(curTime);
-                                thisTempStep.setLastEventAction(EventAction.DOING);
+                                thisTempStep.setLastEventTime(TimeTool.getCurMsUTC());
+                                thisTempStep.setLastCompareSampleExplain("This is matched and trigger short period, it will gen event.");
+
+                                JPOrdWarnEventInfo newEvent = new JPOrdWarnEventInfo(planDetail, rules, thisTempStep, thisTempStep.getLastEventTime());
+                                newEvent2Gen.put(newEvent.getUid(), newEvent);
+                                thisTempStep.setLastEventId(newEvent.getUid());
+                                thisTempStep.setLastEventAction(newEvent.getAction());
                                 thisTempStep.setCycleTriggerTimes(thisTempStep.getCycleTriggerTimes()+1);
                             }
                         }
                     }else {//触发一个新的事件
                         thisTempStep.setLastCompareSampleTime(sampleRecordTm);
                         thisTempStep.setLastCompareSampleResult(COMPARE_TURE);
-                        thisTempStep.setLastEventId("新事件ID");
-                        thisTempStep.setLastEventTime(curTime);
-                        thisTempStep.setLastEventAction(EventAction.DOING);
+                        thisTempStep.setLastEventTime(TimeTool.getCurMsUTC());
+                        thisTempStep.setLastCompareSampleExplain("This is matched, and will gen event.");
+
+                        JPOrdWarnEventInfo newEvent = new JPOrdWarnEventInfo(planDetail, rules, thisTempStep, thisTempStep.getLastEventTime());
+                        newEvent2Gen.put(newEvent.getUid(), newEvent);
+                        thisTempStep.setLastEventId(newEvent.getUid());
+                        thisTempStep.setLastEventAction(newEvent.getAction());
                         thisTempStep.setCycleTriggerTimes(thisTempStep.getCycleTriggerTimes()+1);
                         //[!!!]do not change the FirstMatchSampleTime[!!!]
                     }
                 }else {//之前有预警事件
-                    if (planDetail.getMergeToEvent() == 0 || thisTempStep.getLastEventAction() >= 2){//之前的预警事件已处理,则可认为进行重新计数
+                    if (planDetail.getMergeToEvent() == 0 || thisTempStep.getLastEventAction() >= EventAction.DONE){//之前的预警事件已处理,则可认为进行重新计数
                         thisTempStep.setLastCompareSampleTime(sampleRecordTm);
                         thisTempStep.setLastCompareSampleResult(COMPARE_TURE);
                         thisTempStep.setFirstMatchSampleTime(sampleRecordTm);
+                        thisTempStep.setLastCompareSampleExplain("This is matched, but need to wait the next matched.");
                         if (triggerEventPeriodSwitch <= 1){//触发事件
-                            thisTempStep.setLastEventId("新事件ID");
-                            thisTempStep.setLastEventTime(curTime);
-                            thisTempStep.setLastEventAction(EventAction.DOING);
+                            thisTempStep.setLastEventTime(TimeTool.getCurMsUTC());
+                            thisTempStep.setLastCompareSampleExplain("This is matched and trigger short period, it will gen event.");
+
+                            JPOrdWarnEventInfo newEvent = new JPOrdWarnEventInfo(planDetail, rules, thisTempStep, thisTempStep.getLastEventTime());
+                            newEvent2Gen.put(newEvent.getUid(), newEvent);
+                            thisTempStep.setLastEventId(newEvent.getUid());
+                            thisTempStep.setLastEventAction(newEvent.getAction());
                             thisTempStep.setCycleTriggerTimes(thisTempStep.getCycleTriggerTimes()+1);
                         }else {
                             thisTempStep.setLastEventId("");
@@ -594,11 +626,47 @@ public class SiteDataWarnHandler {
                     }else {//之前的预警事件未处理,则更新预警事件的样本截止时刻
                         thisTempStep.setLastCompareSampleTime(sampleRecordTm);
                         thisTempStep.setLastCompareSampleResult(COMPARE_TURE);
-                        eventId2NewEndUTC.put(thisTempStep.getLastEventId(), sampleRecordTm);
+                        if (newEvent2Gen.containsKey(thisTempStep.getLastEventId())){
+                            newEvent2Gen.get(thisTempStep.getLastEventId()).setToTime(sampleRecordTm);
+                        }else
+                            oldEvent2EndUTC.put(thisTempStep.getLastEventId(), sampleRecordTm);
+                        thisTempStep.setLastCompareSampleExplain("This is matched, but need to merge the last event.");
                     }
                 }
             }
         }while (false);
+
+        thisTempStep.setLastAnalyExplain(step);
+        Map<String, Object> updatePlan = thisTempStep.genUpdatePlan();
+        updatePlan.put(OrdWarnPlanInfoTable.W_INFO.VERSION, planDetail.getVersion()+1);
+        updatePlan.put(OrdWarnPlanInfoTable.W_INFO.UPDATE_TIME, System.currentTimeMillis());
+        List<Map<String, Object>> genNewEventObj = null;
+        if (CommTool.mapSize(newEvent2Gen) > 0){
+            genNewEventObj = new ArrayList<>();
+            for (Map.Entry<String, JPOrdWarnEventInfo> entry:newEvent2Gen.entrySet())
+                genNewEventObj.add(FastJsonUtil.obj2Map(entry.getValue(), true));
+        }
+        List<Map<String, Object>> genOldEventObj = null;
+        if (CommTool.mapSize(oldEvent2EndUTC) > 0){
+            genOldEventObj = new ArrayList<>();
+            for (Map.Entry<String, Long> entry:oldEvent2EndUTC.entrySet()){
+                genOldEventObj.add(new HashMap<String, Object>(){{
+                    put(OrdWarnEventInfoTable.W_INFO.UNIQUE_ID, entry.getKey());
+                    put(OrdWarnEventInfoTable.W_INFO.TO_TIME, entry.getValue());
+                    put(OrdWarnEventInfoTable.W_INFO.UPDATE_TIME, TimeTool.getCurMsUTC());
+                }});
+            }
+        }
+        if (getWarnPlanInfoDBService().updatePlanForTrigger(new HashMap<String, Object>(){{
+            put(OrdWarnPlanInfoTable.W_INFO.UNIQUE_ID, planDetail.getUid());
+            put(OrdWarnPlanInfoTable.W_INFO.VERSION, planDetail.getVersion());
+        }}, updatePlan, genNewEventObj, genOldEventObj) <= 0){
+            step = "Update warn plan trigger result to db failed.";
+            code = ResponseCode.RESULT_BAD;
+        }
+        LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, LogFlagBusiType.BUSI_SITE_DATA_WARN_HANDLE.toStrValue(), mStrClassName
+                , String.format("Analysis Data For Report By Hour:{planId=%s,siteId=%s} {step=%s} {%s}...",
+                        planDetail.getUid(), siteId, step, code.toString()));
         return code;
     }
 }

+ 2 - 2
src/main/java/com/shkpr/service/warncore/bizhandler/ZoneDataWarnHandler.java

@@ -106,7 +106,7 @@ public class ZoneDataWarnHandler {
             thisStepTempRes.setConditionMinUTC(conditionMinUTC);
 
             if (!StringUtils.isEmpty(planDetail.getLastEventId())){
-                int action = CommToolHandler.checkEventAction(planDetail.getLastEventId());
+                int action = CommToolHandler.checkOrdWarnEventAction(planDetail.getLastEventId());
                 if (action < 0){
                     step = "Last event check error,do analysis next time.";
                     code = ResponseCode.RESULT_BAD;
@@ -193,7 +193,7 @@ public class ZoneDataWarnHandler {
             thisStepTempRes.setConditionMinUTC(conditionMinUTC);
 
             if (!StringUtils.isEmpty(planDetail.getLastEventId())){
-                int action = CommToolHandler.checkEventAction(planDetail.getLastEventId());
+                int action = CommToolHandler.checkOrdWarnEventAction(planDetail.getLastEventId());
                 if (action < 0){
                     step = "Last event check error,do analysis next time.";
                     code = ResponseCode.RESULT_BAD;