Explorar el Código

Merge remote-tracking branch 'origin/master_rule_engine' into master_rule_engine

yumeng hace 4 años
padre
commit
7d9878a282

+ 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) {

+ 1 - 7
module-common/src/main/java/cn/com/ctop/common/module/entity/RuleDataAccount.java

@@ -3,7 +3,6 @@ package cn.com.ctop.common.module.entity;
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
-import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
@@ -23,17 +22,12 @@ import java.util.Date;
 @TableName("ctop_rule_data_account")
 @EqualsAndHashCode(callSuper = false)
 @Accessors(chain = true)
-@ApiModel(value="ctop_rule_data_account对象", description="规则账户清洗数据")
 public class RuleDataAccount {
 
-	/**id*/
 	@TableId(type = IdType.AUTO)
-    @ApiModelProperty(value = "id")
 	private Long id;
-	/**账户id*/
-	@Excel(name = "账户id", width = 15)
-    @ApiModelProperty(value = "账户id")
 	private Long accountId;
+	private String accountName;
 	/**消耗*/
 	@Excel(name = "消耗", width = 15)
     @ApiModelProperty(value = "消耗")

+ 1 - 10
module-common/src/main/java/cn/com/ctop/common/module/entity/RuleDataCreative.java

@@ -25,20 +25,11 @@ import java.util.Date;
 @ApiModel(value="ctop_rule_data_creative对象", description="规则创意清洗数据")
 public class RuleDataCreative {
 
-	/**id*/
 	@TableId(type = IdType.AUTO)
 	private Long id;
-	/**accountId*/
-	@Excel(name = "accountId", width = 15)
-    @ApiModelProperty(value = "accountId")
 	private Long accountId;
-	/**planId*/
-	@Excel(name = "planId", width = 15)
-    @ApiModelProperty(value = "planId")
 	private Long planId;
-	/**creativeId*/
-	@Excel(name = "creativeId", width = 15)
-    @ApiModelProperty(value = "creativeId")
+	private String planName;
 	private Long creativeId;
 	/**presentedVideo*/
 	@Excel(name = "presentedVideo", width = 15)

+ 1 - 8
module-common/src/main/java/cn/com/ctop/common/module/entity/RuleDataPlan.java

@@ -26,18 +26,11 @@ import java.util.Date;
 @ApiModel(value="ctop_rule_data_plan对象", description="规则计划清洗数据")
 public class RuleDataPlan {
 
-	/**id*/
 	@TableId(type = IdType.AUTO)
-    @ApiModelProperty(value = "id")
 	private Long id;
-	/**accountId*/
-	@Excel(name = "accountId", width = 15)
-    @ApiModelProperty(value = "accountId")
 	private Long accountId;
-	/**planId*/
-	@Excel(name = "planId", width = 15)
-    @ApiModelProperty(value = "planId")
 	private Long planId;
+	private String planName;
 	/**externalUrl*/
 	@Excel(name = "externalUrl", width = 15)
     @ApiModelProperty(value = "externalUrl")

+ 1 - 8
module-common/src/main/java/cn/com/ctop/common/module/entity/RuleDataTarget.java

@@ -25,18 +25,11 @@ import java.util.Date;
 @ApiModel(value="ctop_rule_data_target对象", description="规则定向清洗数据")
 public class RuleDataTarget {
 
-	/**id*/
 	@TableId(type = IdType.AUTO)
-    @ApiModelProperty(value = "id")
 	private Long id;
-	/**accountId*/
-	@Excel(name = "accountId", width = 15)
-    @ApiModelProperty(value = "accountId")
 	private Long accountId;
-	/**planId*/
-	@Excel(name = "planId", width = 15)
-    @ApiModelProperty(value = "planId")
 	private Long planId;
+	private String planName;
 	/**retargetingTagsExclude*/
 	@Excel(name = "retargetingTagsExclude", width = 15)
     @ApiModelProperty(value = "retargetingTagsExclude")

+ 2 - 7
module-common/src/main/java/cn/com/ctop/common/module/entity/RuleDataWasteCost.java

@@ -28,16 +28,11 @@ public class RuleDataWasteCost {
 
 	/**id*/
 	@TableId(type = IdType.AUTO)
-    @ApiModelProperty(value = "id")
 	private Long id;
-	/**accountId*/
-	@Excel(name = "accountId", width = 15)
-    @ApiModelProperty(value = "accountId")
 	private Long accountId;
-	/**planId*/
-	@Excel(name = "planId", width = 15)
-    @ApiModelProperty(value = "planId")
+	private String accountName;
 	private Long planId;
+	private String planName;
 	/**wasteCost*/
 	@Excel(name = "wasteCost", width = 15)
     @ApiModelProperty(value = "wasteCost")