Forráskód Böngészése

预警规则匹配逻辑

zhaoxian 4 éve
szülő
commit
61fdadaf1c

+ 107 - 0
module-alarm/src/main/java/cn/com/ctop/alarm/modules/constant/MatchLogic.java

@@ -0,0 +1,107 @@
+package cn.com.ctop.alarm.modules.constant;
+
+import cn.com.ctop.alarm.modules.entity.AlarmEventRule;
+import com.alibaba.fastjson.JSONObject;
+import lombok.extern.slf4j.Slf4j;
+
+import java.math.BigDecimal;
+
+/**
+ * TODO
+ *
+ * @ClassName MatchLogic
+ * @Author ZHAOXA
+ * @date 2020-11-17 10:54
+ */
+@Slf4j
+public class MatchLogic {
+
+    /**
+     * @param type      数据类型
+     * @param condition 条件
+     * @param threshold 阈值
+     * @param value     比对值
+     * @return boolean
+     * @throws
+     * @author ZHAOXA
+     */
+    public static boolean matchCondition(String type, String condition, String threshold, String value) {
+        if ("number".equals(type)) {
+            BigDecimal bigThr = new BigDecimal(threshold);
+            BigDecimal bigVal = new BigDecimal(value);
+            switch (condition) {
+                case "equal":
+                    return bigVal.compareTo(bigThr) == 0;
+                case "not_equal":
+                    return bigVal.compareTo(bigThr) != 0;
+                case "greater":
+                    return bigVal.compareTo(bigThr) > 0;
+                case "less":
+                    return bigVal.compareTo(bigThr) < 0;
+                case "greater_equal":
+                    return bigVal.compareTo(bigThr) >= 0;
+                case "less_equal":
+                    return bigVal.compareTo(bigThr) <= 0;
+                default:
+                    log.warn("关系不匹配");
+                    break;
+            }
+        } else if ("string".equals(type)) {
+            switch (condition) {
+                case "equal":
+                    return value.equals(threshold);
+                case "not_equal":
+                    return !value.equals(threshold);
+                case "contain":
+                    return !value.contains(threshold);
+                case "no_contain":
+                    return !value.contains(threshold);
+                default:
+                    log.warn("关系不匹配");
+                    break;
+            }
+        } else {
+            String[] strings = threshold.split(",");
+            if ("contain".equals(condition)) {
+                for (String string : strings) {
+                    if (value.contains(string)) {
+                        return true;
+                    }
+                }
+            } else {
+                for (String string : strings) {
+                    if (!value.contains(string)) {
+                        return true;
+                    }
+                }
+            }
+        }
+        return false;
+    }
+
+    /**
+     * 整理发送的消息
+     */
+    public static String getMsg(String level, String detail, JSONObject cost, AlarmEventRule rule) {
+        StringBuffer header = new StringBuffer();
+        header.append(rule.getMetricValueName()).append("预警").append("<br/>").
+                append("您的账户:").append(cost.getString("accountId")).append(",授权名称为:").append(rule.getAccountName()).append("<br/>");
+        if ("ACCOUNT".equals(level)) {
+            header.append(detail);
+        } else if ("CAMPAIGN".equals(level)) {
+            header.append("计划ID:").append(rule.getCampaignId()).append(",计划名称为:").append(rule.getCampaignName()).append("<br/>").append(detail);
+        } else {
+            header.append("计划ID:").append(rule.getCampaignId()).append(",计划名称为:").append(rule.getCampaignName()).append("<br/>")
+                    .append("组ID:").append(rule.getUnitId()).append(",组名称为:").append(rule.getUnitName()).append("<br/>").append(detail);
+        }
+        String msg = null;
+        if ("PAUSE".equals(rule.getProcessMethod())) {
+            header.append("现已被关停,请您及时查看并调整!");
+        } else {
+            header.append("请您及时查看并调整!");
+            msg = header.toString();
+        }
+        return msg;
+    }
+
+}

+ 47 - 55
module-alarm/src/main/java/cn/com/ctop/alarm/modules/service/impl/RuleGroupServiceImpl.java

@@ -1,7 +1,18 @@
 package cn.com.ctop.alarm.modules.service.impl;
 
-import cn.com.ctop.alarm.modules.entity.*;
-import cn.com.ctop.alarm.modules.mapper.*;
+import cn.com.ctop.alarm.modules.constant.MatchLogic;
+import cn.com.ctop.alarm.modules.entity.RuleAccountTemplate;
+import cn.com.ctop.alarm.modules.entity.RuleAccountThreshold;
+import cn.com.ctop.alarm.modules.entity.RuleBase;
+import cn.com.ctop.alarm.modules.entity.RuleGroup;
+import cn.com.ctop.alarm.modules.entity.RuleIndicator;
+import cn.com.ctop.alarm.modules.entity.RuleTemplate;
+import cn.com.ctop.alarm.modules.mapper.RuleAccountTemplateMapper;
+import cn.com.ctop.alarm.modules.mapper.RuleAccountThresholdMapper;
+import cn.com.ctop.alarm.modules.mapper.RuleBaseMapper;
+import cn.com.ctop.alarm.modules.mapper.RuleGroupMapper;
+import cn.com.ctop.alarm.modules.mapper.RuleIndicatorMapper;
+import cn.com.ctop.alarm.modules.mapper.RuleTemplateMapper;
 import cn.com.ctop.alarm.modules.service.IRuleAccountTemplateService;
 import cn.com.ctop.alarm.modules.service.IRuleGroupService;
 import cn.com.ctop.common.module.utils.Check;
@@ -9,10 +20,10 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -25,6 +36,7 @@ import java.util.Map;
  * @version V1.0
  * @date 2020-11-15
  */
+@Slf4j
 @Service
 public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup> implements IRuleGroupService {
 
@@ -94,27 +106,51 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
      */
     private void matchAlarmRules(List<RuleGroup> ruleGroups, JSONObject thresholdObj, JSONObject matchData, JSONObject indicators) {
         for (RuleGroup ruleGroup : ruleGroups) {
-            String sendType = ruleGroup.getSendType();
-            boolean isPause = "PAUSE".equals(ruleGroup.getOperate());
             boolean isAllTrue = "and".equals(ruleGroup.getRuleRelationship());
             boolean flag = true;
             List<RuleBase> ruleBaseList = ruleGroup.getRuleBaseList();
             for (RuleBase ruleBase : ruleBaseList) {
+                //指标阈值
+                String threshold = thresholdObj.getString(ruleBase.getId().toString());
+                String value = matchData.getString(ruleBase.getIndicatorCode());
+                //指标对象
+                JSONObject indicator = indicators.getJSONObject(ruleBase.getIndicatorCode());
                 //and关系,全部匹配规则
                 if (isAllTrue) {
                     if (flag) {
-                        //指标阈值
-                        String threshold = thresholdObj.getString(ruleBase.getId().toString());
-                        //指标对象
-                        JSONObject indicator = indicators.getJSONObject(ruleBase.getIndicatorCode());
-                        flag = checkThreshold(threshold, ruleBase, indicator);
+                        flag = MatchLogic.matchCondition(indicator.getString("dataType"), ruleBase.getRuleCondition(), threshold, value);
                     }
                 } else {
+                    //or关系,任一阈值匹配,则跳出循环
+                    flag = MatchLogic.matchCondition(indicator.getString("dataType"), ruleBase.getRuleCondition(), threshold, value);
+                    if (flag) {
+                        break;
+                    }
+                }
+            }
+            //执行发送
+            if (flag) {
+//                MatchLogic.getMsg()
+                boolean isPause = "PAUSE".equals(ruleGroup.getOperate());
+                String sendType = ruleGroup.getSendType();
+                if(isPause){
+                    //关停操作 TODO
+
+                }
+                if ("SMS".equals(sendType)) {
+
+                } else if ("WeChat".equals(sendType)) {
+
+                } else if ("EMAIL".equals(sendType)) {
+
+                } else if ("TEL".equals(sendType)) {
+
+                } else {
 
                 }
+
             }
         }
-
     }
 
     private boolean checkThreshold(String threshold, RuleBase ruleBase, JSONObject indicator) {
@@ -128,50 +164,6 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
         return true;
     }
 
-    private boolean check(String type, String condition, String threshold, String value) {
-        if ("number".equals(type)) {
-            BigDecimal bigThr = new BigDecimal(threshold);
-            BigDecimal bigVal = new BigDecimal(value);
-            if ("equal".equals(condition))
-                return bigVal.compareTo(bigThr) == 0;
-            else if ("not_equal".equals(condition))
-                return bigVal.compareTo(bigThr) != 0;
-            else if ("greater".equals(condition))
-                return bigVal.compareTo(bigThr) > 0;
-            else if ("less".equals(condition))
-                return bigVal.compareTo(bigThr) < 0;
-            else if ("greater_equal".equals(condition))
-                return bigVal.compareTo(bigThr) >= 0;
-            else if ("less_equal".equals(condition))
-                return bigVal.compareTo(bigThr) <= 0;
-        } else if ("string".equals(type)) {
-            if ("equal".equals(condition))
-                return value.equals(threshold);
-            else if ("not_equal".equals(condition))
-                return !value.equals(threshold);
-            else if ("contain".equals(condition))
-                return !value.contains(threshold);
-            else if ("no_contain".equals(condition))
-                return !value.contains(threshold);
-        } else {
-            String[] strings = threshold.split(",");
-            if ("contain".equals(condition)) {
-                for (String string : strings) {
-                    if (value.contains(string)) {
-                        return true;
-                    }
-                }
-            } else {
-                for (String string : strings) {
-                    if (!value.contains(string)) {
-                        return true;
-                    }
-                }
-            }
-        }
-        return false;
-    }
-
 
     //整理阈值数据
     public JSONObject getThreshold(List<RuleAccountThreshold> thresholdList) {