Browse Source

针对分区支持复合对比公式

andyliu 6 days ago
parent
commit
52686961f7

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

@@ -79,9 +79,9 @@ public class CommToolHandler {
         int matched = -1;
         try {
             int nRes = 0;
-            if ("int".equals(condition.getType())
-                    || "integer".equals(condition.getType())
-                    || "long".equals(condition.getType()))
+            if ("int".equalsIgnoreCase(condition.getType())
+                    || "integer".equalsIgnoreCase(condition.getType())
+                    || "long".equalsIgnoreCase(condition.getType()))
                 nRes = CommTool.compareNumber(originValue, condition.getThreshold());
             else
                 nRes = CommTool.compareFloat(originValue, condition.getThreshold());
@@ -119,9 +119,9 @@ public class CommToolHandler {
         int matched = -1;
         try {
             int nRes = 0;
-            if ("int".equals(condition.getType())
-                    || "integer".equals(condition.getType())
-                    || "long".equals(condition.getType())){
+            if ("int".equalsIgnoreCase(condition.getType())
+                    || "integer".equalsIgnoreCase(condition.getType())
+                    || "long".equalsIgnoreCase(condition.getType())){
                 if (StringUtils.isEmpty(percent))
                     nRes = CommTool.compareNumber(originValue, matchedValue);
                 else{

+ 6 - 0
src/main/java/com/shkpr/service/warncore/bizhandler/SiteDataWarnHandler.java

@@ -458,6 +458,7 @@ public class SiteDataWarnHandler {
                     if (condition.getThreshold().contains("@")){
                         JPMixComplexCondition jp = CommToolHandler.parseConditions2TimeRange(condition.getThreshold(), sampleRecordTm, 0L);
                         if (jp != null){
+                            jp.setDataType(condition.getType());
                             String mixKey = jp.genKey();
                             String matchedValue = mixComplexKey2Value.get(mixKey);
                             if (matchedValue == null){
@@ -760,6 +761,7 @@ public class SiteDataWarnHandler {
                     if (condition.getThreshold().contains("@")){
                         JPMixComplexCondition jp = CommToolHandler.parseConditions2TimeRange(condition.getThreshold(), sampleRecordTm, 0L);
                         if (jp != null){
+                            jp.setDataType(condition.getType());
                             String mixKey = jp.genKey();
                             String matchedValue = mixComplexKey2Value.get(mixKey);
                             if (matchedValue == null){
@@ -1160,6 +1162,7 @@ public class SiteDataWarnHandler {
                     if (condition.getThreshold().contains("@")){
                         JPMixComplexCondition jp = CommToolHandler.parseConditions2TimeRange(condition.getThreshold(), sampleRecordTm, 0L);
                         if (jp != null){
+                            jp.setDataType(condition.getType());
                             String mixKey = jp.genKey();
                             String matchedValue = mixComplexKey2Value.get(mixKey);
                             if (matchedValue == null){
@@ -1472,6 +1475,7 @@ public class SiteDataWarnHandler {
                     }
                 }
             }
+            siteStatsData.clearData();
         }while (false);
         return value;
     }
@@ -1517,6 +1521,7 @@ public class SiteDataWarnHandler {
 
             if (!StringUtils.isEmpty(value) && "avg".equals(jp.getMethod()))
                 value = String.valueOf(CastUtil.castDouble(value)/nSampleLines);
+            reportRes.clearData();
         }while (false);
         return value;
     }
@@ -1567,6 +1572,7 @@ public class SiteDataWarnHandler {
             int diffLines = needLines-realLines;//缺失条数(实际条数比理论条数大时为负)
             double diffRate = diffLines<=offsetLines?0.0:(diffLines*100)/needLines;
             value = String.valueOf(diffRate);
+            reportRes.clearData();
         }while (false);
         return value;
     }

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

@@ -15,6 +15,7 @@ import com.shkpr.service.warncore.dbdao.tables.OrdWarnEventInfoTable;
 import com.shkpr.service.warncore.dbdao.tables.OrdWarnPlanInfoTable;
 import com.shkpr.service.warncore.dbdao.tables.OrdWarnTriggerSampleHisTable;
 import com.shkpr.service.warncore.dto.*;
+import com.shkpr.service.warncore.jsonbean.JPMixComplexCondition;
 import com.shkpr.service.warncore.jsonbean.JPOrdWarnEventInfo;
 import org.springframework.util.StringUtils;
 
@@ -466,6 +467,7 @@ public class ZoneDataWarnHandler {
         Map<String, List<DateKeyValue>> ownerId2TriggerSample = new HashMap<>();//触发规则的样本数据
         String eventIdAcceptPlanHisTriggerSam = "";//将预案的历史触发样本数据迁移到该事件
         boolean clearPlanHisTriggerSam = false;//是否需要清空预案的历史触发样本数据
+        Map<String, String> mixComplexKey2Value = new HashMap<>();//复合公式下的{key->value}
 
         do {
             final String doField = rules.get(0).getConditions().get(0).getFormat();
@@ -525,7 +527,28 @@ public class ZoneDataWarnHandler {
                     if (backTag.equals(sampleItem.getKey()))
                         originValue = sampleItem.getValue();
 
-                    int pkRes = CommToolHandler.matchedToRuleItem(originValue, condition);
+                    int pkRes = -1;
+                    if (condition.getThreshold().contains("@")){
+                        JPMixComplexCondition jp = CommToolHandler.parseConditions2TimeRange(condition.getThreshold(), sampleRecordTm, 0L);
+                        if (jp != null){
+                            jp.setDataType(condition.getType());
+                            String mixKey = jp.genKey();
+                            String matchedValue = mixComplexKey2Value.get(mixKey);
+                            if (matchedValue == null){
+                                matchedValue = getMixComplexDataFromDB(fieldUnit, planDetail.getFrequencyUnit(), planDetail.getItemKey()
+                                        , zoneId, metricId , threshold, planDetail.getDoDb(), doField, planDetail.getDoTime(), jp);
+                                if (matchedValue != null)
+                                    mixComplexKey2Value.put(mixKey, matchedValue);
+                            }
+                            pkRes = CommToolHandler.matchedToRuleItemEx(originValue, matchedValue, jp.getPercent(), condition);
+                        }else{
+                            step = String.format("Mix complex condition invalid.");
+                            code = ResponseCode.RESULT_BAD;
+                            break;
+                        }
+                    }else
+                        pkRes = CommToolHandler.matchedToRuleItem(originValue, condition);
+
                     if (pkRes == -1){
                         step = String.format("Compare value exception error");
                         code = ResponseCode.RESULT_BAD;
@@ -1015,4 +1038,56 @@ public class ZoneDataWarnHandler {
         oRes.checkRes();
         return oRes;
     }
+
+    private static String getMixComplexDataFromDB(String fieldUnit, String frequencyUnit, String itemKey, String objId, String metricId, String threshold, String doDb, String doField, String doTime, JPMixComplexCondition jp){
+        String value = null;
+        TRecordRes<DateKeyValue> recordRes = null;
+        do {
+            recordRes = getSampleDataFromDB(fieldUnit, frequencyUnit, itemKey, objId, jp.getBeginTime(), jp.getEndTime(), metricId, threshold, doDb, doField, doTime);
+            if (recordRes == null || recordRes.getCode() != ResponseCode.RESULT_NORMAL.toInt())
+                break;
+
+            int nSampleLines = CommTool.listSize(recordRes.getData());
+            if (nSampleLines <= 0)
+                break;
+
+            for (DateKeyValue item:recordRes.getData()){
+                if (StringUtils.isEmpty(value)){
+                    value = item.getValue();
+                    continue;
+                }
+
+                if ("int".equalsIgnoreCase(jp.getDataType())
+                        || "integer".equalsIgnoreCase(jp.getDataType())
+                        || "long".equalsIgnoreCase(jp.getDataType())){
+                    if ("min".equals(jp.getMethod())){
+                        value = String.valueOf(Math.min(CastUtil.castInt(value, 0), CastUtil.castInt(item.getValue(), 0)));
+                    }else if ("max".equals(jp.getMethod())){
+                        value = String.valueOf(Math.max(CastUtil.castInt(value, 0), CastUtil.castInt(item.getValue(), 0)));
+                    }else {
+                        value = String.valueOf(CastUtil.castInt(value, 0)+CastUtil.castInt(item.getValue(), 0));
+                    }
+                }else {
+                    if ("min".equals(jp.getMethod())){
+                        value = String.valueOf(Math.min(CastUtil.castDouble(value, 0), CastUtil.castDouble(item.getValue(), 0)));
+                    }else if ("max".equals(jp.getMethod())){
+                        value = String.valueOf(Math.max(CastUtil.castDouble(value, 0), CastUtil.castDouble(item.getValue(), 0)));
+                    }else {
+                        value = String.valueOf(CastUtil.castDouble(value, 0)+CastUtil.castDouble(item.getValue(), 0));
+                    }
+                }
+            }
+
+            if (!StringUtils.isEmpty(value) && "avg".equals(jp.getMethod())){
+                if ("int".equalsIgnoreCase(jp.getDataType())
+                        || "integer".equalsIgnoreCase(jp.getDataType())
+                        || "long".equalsIgnoreCase(jp.getDataType()))
+                    value = String.valueOf(CastUtil.castInt(value, 0)/nSampleLines);
+                else
+                    value = String.valueOf(CastUtil.castDouble(value, 0)/nSampleLines);
+            }
+            recordRes.clearData();
+        }while (false);
+        return value;
+    }
 }

+ 6 - 0
src/main/java/com/shkpr/service/warncore/dto/LinesXYReportRes.java

@@ -13,4 +13,10 @@ public class LinesXYReportRes {
 
     public LinesXYReportRes() {
     }
+
+    public void clearData(){
+        if (data != null)
+            data.clear();
+        data = null;
+    }
 }

+ 6 - 0
src/main/java/com/shkpr/service/warncore/dto/SiteStatsDataResult.java

@@ -13,4 +13,10 @@ public class SiteStatsDataResult {
 
     public SiteStatsDataResult() {
     }
+
+    public void clearData(){
+        if (data != null)
+            data.clear();
+        data = null;
+    }
 }

+ 6 - 0
src/main/java/com/shkpr/service/warncore/dto/WaterXYReportRes.java

@@ -13,4 +13,10 @@ public class WaterXYReportRes {
 
     public WaterXYReportRes() {
     }
+
+    public void clearData(){
+        if (data != null)
+            data.clear();
+        data = null;
+    }
 }

+ 2 - 1
src/main/java/com/shkpr/service/warncore/jsonbean/JPMixComplexCondition.java

@@ -11,7 +11,8 @@ public class JPMixComplexCondition {
     private String mode = "";//获取方式:last/prev/near/this
     private String method = "";//时间段内取值方法:avg/sum/dif/min/max/val
     private String percent = "";//百分率
-    private String dateUnit = "";//日期单位
+    private String dateUnit = "";//日期单位:h/d/w/n/y
+    private String dataType = "";//目标值的取值类型(int/long/float/double)
 
     public JPMixComplexCondition() {
     }