|
|
@@ -3,6 +3,7 @@ package com.shkpr.service.warncore.bizhandler;
|
|
|
import com.shkpr.service.warncore.commtools.CommTool;
|
|
|
import com.shkpr.service.warncore.commtools.TimeTool;
|
|
|
import com.shkpr.service.warncore.constants.CommFieldStatus;
|
|
|
+import com.shkpr.service.warncore.constants.FrequencyUnit;
|
|
|
import com.shkpr.service.warncore.dbdao.DBMgrProxy;
|
|
|
import com.shkpr.service.warncore.dbdao.services.intef.OrdWarnPlanInfoDBService;
|
|
|
import com.shkpr.service.warncore.dbdao.services.intef.OrdWarnPlanRulesDBService;
|
|
|
@@ -24,6 +25,80 @@ public class ZoneDataWarnHandler {
|
|
|
return DBMgrProxy.getInstance().applyXXXApi(OrdWarnPlanRulesDBService.class);
|
|
|
}
|
|
|
|
|
|
+ public static ResponseCode handlerDataByMonth(OrdWarnPlanDetail planDetail, List<OrdWarnPlanRules> rules){
|
|
|
+ ResponseCode code = ResponseCode.RESULT_NORMAL;
|
|
|
+ String step = "Start to pre analysis.";
|
|
|
+ if (getWarnPlanInfoDBService().existsLineEx(OrdWarnPlanInfoTable.R_INFO.TABLE
|
|
|
+ , new HashMap<String, Object>(){{put(OrdWarnPlanInfoTable.R_INFO.STATUS, CommFieldStatus.ENABLE);
|
|
|
+ put(OrdWarnPlanInfoTable.R_INFO.UNIQUE_ID, planDetail.getUid());
|
|
|
+ put(OrdWarnPlanInfoTable.R_INFO.VERSION, planDetail.getVersion());
|
|
|
+ }}, null, "") <= 0){
|
|
|
+ step = "Plan is changed,do analysis next time";
|
|
|
+ code = ResponseCode.RESULT_BAD;
|
|
|
+ }
|
|
|
+
|
|
|
+ final long curUTCTm = TimeTool.getCurMsUTC();
|
|
|
+ final long lastMonthThisTm = TimeTool.getTimeChgMonth(curUTCTm, -1);//前一个月的当前时间
|
|
|
+ final int preMonth = TimeTool.getMonthIndex(lastMonthThisTm);//月分析时,分析前一月
|
|
|
+ final int preMonthInYear = TimeTool.getYearIndex(lastMonthThisTm);//月分析时,分析前一月所在的年份
|
|
|
+ long queryBeginUTC = 0L, conditionMinUTC = 0L;
|
|
|
+ long queryEndUTC = TimeTool.getMonthBeginUTC(curUTCTm);
|
|
|
+ OrdWarnPlanTempStep thisStepTempRes = new OrdWarnPlanTempStep(planDetail);
|
|
|
+ do {
|
|
|
+ int findStartRange = -1;
|
|
|
+ for (int i=rules.size()-1;i>=0;i--){
|
|
|
+ if (preMonth >= rules.get(i).getStart() && preMonth <= rules.get(i).getEnd()){
|
|
|
+ findStartRange = rules.get(i).getStart();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (findStartRange < 0){
|
|
|
+ step = "Not in time range,do analysis next time";
|
|
|
+ code = ResponseCode.RESULT_BAD;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ findStartRange = findStartRange-1;
|
|
|
+ for (int i=rules.size()-1;i>=0;i--){
|
|
|
+ if (findStartRange <= 0)
|
|
|
+ break;
|
|
|
+ if (findStartRange == rules.get(i).getEnd())
|
|
|
+ findStartRange = rules.get(i).getStart()-1;
|
|
|
+ }
|
|
|
+ queryBeginUTC = TimeTool.convertDateStr2BeginUTC(String.format("%d-%2d-01 00:00:00", preMonthInYear, findStartRange+1));
|
|
|
+ conditionMinUTC = queryBeginUTC;
|
|
|
+ long before3MonthUTC = TimeTool.getTimeChgMonth(queryEndUTC,-3);//最多只往前分析3个月
|
|
|
+ if (before3MonthUTC > queryBeginUTC)
|
|
|
+ queryBeginUTC = before3MonthUTC;
|
|
|
+
|
|
|
+ if (thisStepTempRes.getLastCompareSampleTime() <= 0L
|
|
|
+ || thisStepTempRes.getLastCompareSampleTime() < queryBeginUTC) {//从未分析过则重新计数)
|
|
|
+ thisStepTempRes.resetData();
|
|
|
+ }else if (thisStepTempRes.getLastCompareSampleTime() >= queryBeginUTC) {
|
|
|
+ queryBeginUTC = TimeTool.getTimeChgMonth(thisStepTempRes.getLastCompareSampleTime(), 1);
|
|
|
+ }
|
|
|
+ if (queryBeginUTC >= queryEndUTC){
|
|
|
+ step = "Time is invalid,do analysis next time";
|
|
|
+ code = ResponseCode.RESULT_BAD;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ thisStepTempRes.setDoForTodayBefore(true);
|
|
|
+ thisStepTempRes.setQueryBeginUTC(queryBeginUTC);
|
|
|
+ thisStepTempRes.setQueryEndUTC(queryEndUTC);
|
|
|
+ thisStepTempRes.setConditionMinUTC(conditionMinUTC);
|
|
|
+
|
|
|
+ if (!StringUtils.isEmpty(planDetail.getLastEventId())){
|
|
|
+ //向数据库中快速查询事件最近处理阶段action的值
|
|
|
+ //并赋值给thisTempStep
|
|
|
+ }
|
|
|
+ }while (false);
|
|
|
+
|
|
|
+ if (code == ResponseCode.RESULT_NORMAL){
|
|
|
+ code = analysisDataByDayOrMonth(planDetail, rules, thisStepTempRes);
|
|
|
+ }else {//单独更新预警方案的分析时间、次数、说明
|
|
|
+ }
|
|
|
+ return code;
|
|
|
+ }
|
|
|
+
|
|
|
public static ResponseCode handlerDataByDay(OrdWarnPlanDetail planDetail, List<OrdWarnPlanRules> rules){
|
|
|
ResponseCode code = ResponseCode.RESULT_NORMAL;
|
|
|
String step = "Start to pre analysis.";
|
|
|
@@ -39,7 +114,6 @@ public class ZoneDataWarnHandler {
|
|
|
final long curUTCTm = TimeTool.getCurMsUTC();
|
|
|
final int preDayInYear = TimeTool.getYearIndex(curUTCTm-TimeTool.MS_ONE_DAY);//日分析时,分析前一日所在的年份
|
|
|
final int preDayInMonth = TimeTool.getMonthIndex(curUTCTm-TimeTool.MS_ONE_DAY);//日分析时,分析前一日所在的月份
|
|
|
-
|
|
|
long queryBeginUTC = 0L, conditionMinUTC = 0L;
|
|
|
long queryEndUTC = TimeTool.getTodayBeginUTC();
|
|
|
OrdWarnPlanTempStep thisStepTempRes = new OrdWarnPlanTempStep(planDetail);
|
|
|
@@ -65,9 +139,9 @@ public class ZoneDataWarnHandler {
|
|
|
|
|
|
queryBeginUTC = TimeTool.convertDateStr2BeginUTC(String.format("%d-%2d-01 00:00:00", preDayInYear, findStartRange+1));
|
|
|
conditionMinUTC = queryBeginUTC;
|
|
|
- long before3Day = queryEndUTC-TimeTool.MS_ONE_DAY*3;//最多只往前分析3天
|
|
|
- if (before3Day > queryBeginUTC)
|
|
|
- queryBeginUTC = before3Day;
|
|
|
+ long before3DayUTC = queryEndUTC-TimeTool.MS_ONE_DAY*3;//最多只往前分析3天
|
|
|
+ if (before3DayUTC > queryBeginUTC)
|
|
|
+ queryBeginUTC = before3DayUTC;
|
|
|
|
|
|
if (thisStepTempRes.getLastCompareSampleTime() <= 0L
|
|
|
|| thisStepTempRes.getLastCompareSampleTime() < queryBeginUTC) {//从未分析过则重新计数)
|
|
|
@@ -90,24 +164,26 @@ public class ZoneDataWarnHandler {
|
|
|
//向数据库中快速查询事件最近处理阶段action的值
|
|
|
//并赋值给thisTempStep
|
|
|
}
|
|
|
-
|
|
|
- return analysisDataByDay(planDetail, rules, thisStepTempRes);
|
|
|
}while (false);
|
|
|
+
|
|
|
+ if (code == ResponseCode.RESULT_NORMAL){
|
|
|
+ code = analysisDataByDayOrMonth(planDetail, rules, thisStepTempRes);
|
|
|
+ }else {//单独更新预警方案的分析时间、次数、说明
|
|
|
+ }
|
|
|
return code;
|
|
|
}
|
|
|
|
|
|
- private static ResponseCode analysisDataByDay(OrdWarnPlanDetail planDetail, List<OrdWarnPlanRules> rules, OrdWarnPlanTempStep thisTempStep){
|
|
|
- ResponseCode code = ResponseCode.RESULT_NORMAL;
|
|
|
+ private static ResponseCode analysisDataByDayOrMonth(OrdWarnPlanDetail planDetail, List<OrdWarnPlanRules> rules, OrdWarnPlanTempStep thisTempStep){
|
|
|
String step = "Start to deep analysis";
|
|
|
+ ResponseCode code = ResponseCode.RESULT_NORMAL;
|
|
|
long curTime = TimeTool.getCurMsUTC();
|
|
|
final String zoneId = rules.get(0).getObjId();
|
|
|
List<DateKeyValue> zoneStatsData = null;//要求数据按date升序
|
|
|
Map<String, Long> eventId2NewEndUTC = new HashMap<>();//需要更新预警事件的样本截止时间
|
|
|
-
|
|
|
do {
|
|
|
String[] storeTagAndBackTag = rules.get(0).getConditions().get(0).getFormat().split("@");
|
|
|
if (storeTagAndBackTag == null || storeTagAndBackTag.length < 2){
|
|
|
- step = String.format("Format param valid in rules");
|
|
|
+ step = String.format("Format param invalid in rules");
|
|
|
code = ResponseCode.RESULT_BAD;
|
|
|
break;
|
|
|
}
|
|
|
@@ -168,7 +244,7 @@ public class ZoneDataWarnHandler {
|
|
|
continue;
|
|
|
}//本次匹配成功进入下一步
|
|
|
|
|
|
- long triggerEventPeriodSwitch = planDetail.getFrequency()*planDetail.getThreshold();//触发预警事件的最小时长(单位:天)
|
|
|
+ long triggerEventPeriodSwitch = planDetail.getFrequency()*planDetail.getThreshold();//触发预警事件的最小时长(单位:天/月)
|
|
|
if (thisTempStep.getLastCompareSampleResult() <= 0){//上一次没有匹配成功
|
|
|
thisTempStep.setLastCompareSampleTime(sampleRecordTm);
|
|
|
thisTempStep.setLastCompareSampleResult(1);
|
|
|
@@ -181,8 +257,12 @@ public class ZoneDataWarnHandler {
|
|
|
continue;
|
|
|
}//上一次匹配成功了进入下一步
|
|
|
|
|
|
- long diffMatch = (sampleRecordTm-thisTempStep.getLastCompareSampleTime())/TimeTool.MS_ONE_DAY;//两个最近匹配样本之间的采集时间差(单位:天)
|
|
|
- if (diffMatch > triggerEventPeriodSwitch){//两个最近匹配样本间缺失的数据太多、或跨了天、或跨了时段,可认为重新计数
|
|
|
+ long diffMatch = 0L;//两个最近匹配样本之间的采集时间差
|
|
|
+ if (FrequencyUnit.MONTH.equals(planDetail.getFrequencyUnit()))
|
|
|
+ diffMatch = TimeTool.getMonthCountsBetweenTm(sampleRecordTm, thisTempStep.getLastCompareSampleTime());//换算成月
|
|
|
+ else
|
|
|
+ diffMatch = (sampleRecordTm-thisTempStep.getLastCompareSampleTime())/TimeTool.MS_ONE_DAY;//换算成天
|
|
|
+ if (diffMatch > triggerEventPeriodSwitch){//两个最近匹配样本间缺失的数据太多、或跨了天/月、或跨了时段,可认为重新计数
|
|
|
thisTempStep.setLastCompareSampleTime(sampleRecordTm);
|
|
|
thisTempStep.setLastCompareSampleResult(1);
|
|
|
thisTempStep.setFirstMatchSampleTime(sampleRecordTm);
|
|
|
@@ -197,7 +277,13 @@ public class ZoneDataWarnHandler {
|
|
|
continue;
|
|
|
}//两个最近匹配样本间隔很近,可认为是持续匹配,则进入下一步
|
|
|
|
|
|
- long matchLen = (thisTempStep.getFirstMatchSampleTime()<=0)?0:((sampleRecordTm-thisTempStep.getFirstMatchSampleTime())/TimeTool.MS_ONE_DAY);//样本匹配的持续时长(单位:天)
|
|
|
+ long matchLen = 0L;//样本匹配的持续时长
|
|
|
+ if (thisTempStep.getFirstMatchSampleTime() > 0L){
|
|
|
+ if (FrequencyUnit.MONTH.equals(planDetail.getFrequencyUnit()))
|
|
|
+ matchLen = TimeTool.getMonthCountsBetweenTm(sampleRecordTm, thisTempStep.getFirstMatchSampleTime())+1;//换算成月
|
|
|
+ else
|
|
|
+ matchLen = ((sampleRecordTm-thisTempStep.getFirstMatchSampleTime())/TimeTool.MS_ONE_DAY)+1;//换算成天
|
|
|
+ }
|
|
|
if (StringUtils.isEmpty(thisTempStep.getLastEventId())){//之前无预警事件
|
|
|
if (matchLen < triggerEventPeriodSwitch){//还得持续等待,达不到触发事件的累计时长
|
|
|
thisTempStep.setLastCompareSampleTime(sampleRecordTm);
|