瀏覽代碼

预警规则匹配逻辑——发送数据

zhaoxian 4 年之前
父節點
當前提交
115cf6c291

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

@@ -1,9 +1,6 @@
 package cn.com.ctop.alarm.modules.service.impl;
 
 import cn.com.ctop.alarm.modules.constant.MatchLogic;
-import cn.com.ctop.alarm.modules.entity.*;
-import cn.com.ctop.alarm.modules.mapper.*;
-import cn.com.ctop.alarm.modules.entity.AlarmEventRule;
 import cn.com.ctop.alarm.modules.entity.RuleAccountTemplate;
 import cn.com.ctop.alarm.modules.entity.RuleAccountThreshold;
 import cn.com.ctop.alarm.modules.entity.RuleBase;
@@ -122,82 +119,86 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
             return;
         }
         log.info("已获取账户({})的数据,开始匹配规则", accountId);
-        for (RuleGroup ruleGroup : ruleGroups) {
-            List<RuleBase> ruleBaseList = ruleGroup.getRuleBaseList();
-            if (Check.isNull(ruleBaseList)) {
-                continue;
-            }
-            boolean isBase = "base".equals(ruleGroup.getRuleType());
-            //匹配单规则
-            if (isBase) {
-                RuleBase ruleBase = ruleBaseList.get(0);
-                //指标对象
-                JSONObject indicator = indicators.getJSONObject(ruleBase.getIndicatorCode());
-                //指标阈值
-                String threshold = thresholdObj.getString(ruleBase.getId().toString());
-                //阈值为空时,或者阈值为“unlimited”(不限),不执行该规则
-                if (Check.isNull(threshold) || "unlimited".equals(threshold)) {
-                    log.warn("阈值为空/不限,该规则({})不执行", ruleBase.getId());
+        try {
+            for (RuleGroup ruleGroup : ruleGroups) {
+                List<RuleBase> ruleBaseList = ruleGroup.getRuleBaseList();
+                if (Check.isNull(ruleBaseList)) {
                     continue;
                 }
-                //维度数据
-                JSONArray dimensionData = matchData.getJSONArray(ruleBase.getRuleDimension());
-                if (!Check.isNull(dimensionData)) {
-                    for (int i = 0; i < dimensionData.size(); i++) {
-                        JSONObject obj = dimensionData.getJSONObject(i);
-                        String value = obj.getString(ruleBase.getIndicatorCode());
-                        if (MatchLogic.matchCondition(indicator.getString("dataType"), ruleBase.getRuleCondition(), threshold, value)) {
-                            sendMsg(ruleGroup, obj);
-                        }
-                    }
-                }
-                //匹配组合规则
-            } else {
-                boolean flag = false;
-                //符合规则的数据集
-                JSONArray targetDatas = new JSONArray();
-                JSONArray planDatas = new JSONArray();
-                JSONArray creativeDatas = new JSONArray();
-                JSONArray accountDatas = new JSONArray();
-                for (RuleBase ruleBase : ruleBaseList) {
+                boolean isBase = "base".equals(ruleGroup.getRuleType());
+                //匹配单规则
+                if (isBase) {
+                    RuleBase ruleBase = ruleBaseList.get(0);
                     //指标对象
                     JSONObject indicator = indicators.getJSONObject(ruleBase.getIndicatorCode());
-                    //阈值类型
-                    String dataType = indicator.getString("dataType");
                     //指标阈值
                     String threshold = thresholdObj.getString(ruleBase.getId().toString());
                     //阈值为空时,或者阈值为“unlimited”(不限),不执行该规则
                     if (Check.isNull(threshold) || "unlimited".equals(threshold)) {
+                        log.warn("阈值为空/不限,该规则({})不执行", ruleBase.getId());
                         continue;
                     }
+                    //维度数据
                     JSONArray dimensionData = matchData.getJSONArray(ruleBase.getRuleDimension());
-                    //账户维度数据
-                    if ("account".equals(ruleBase.getRuleDimension())) {
-                        JSONObject accountEntity = dimensionData.getJSONObject(0);
-                        if (MatchLogic.matchCondition(dataType, ruleBase.getRuleCondition(), threshold, accountEntity.getString(ruleBase.getIndicatorCode()))) {
-                            accountDatas.add(accountEntity);
-                        } else {
-                            log.warn("判断规则组({}),账户规则不符合", ruleGroup.getId());
-                            break;
+                    if (!Check.isNull(dimensionData)) {
+                        for (int i = 0; i < dimensionData.size(); i++) {
+                            JSONObject obj = dimensionData.getJSONObject(i);
+                            String value = obj.getString(ruleBase.getIndicatorCode());
+                            if (MatchLogic.matchCondition(indicator.getString("dataType"), ruleBase.getRuleCondition(), threshold, value)) {
+                                sendMsg(ruleGroup, obj);
+                            }
                         }
-                    } else if ("plan".equals(ruleBase.getRuleDimension())) {
-                        planDatas = getOKData(planDatas, dimensionData, ruleBase, dataType, threshold);
-                    } else if ("creative".equals(ruleBase.getRuleDimension())) {
-                        creativeDatas = getOKData(creativeDatas, dimensionData, ruleBase, dataType, threshold);
-                    } else if ("target".equals(ruleBase.getRuleDimension())) {
-                        targetDatas = getOKData(targetDatas, dimensionData, ruleBase, dataType, threshold);
                     }
+                    //匹配组合规则
+                } else {
+                    boolean flag = false;
+                    //符合规则的数据集
+                    JSONArray targetDatas = new JSONArray();
+                    JSONArray planDatas = new JSONArray();
+                    JSONArray creativeDatas = new JSONArray();
+                    JSONArray accountDatas = new JSONArray();
+                    for (RuleBase ruleBase : ruleBaseList) {
+                        //指标对象
+                        JSONObject indicator = indicators.getJSONObject(ruleBase.getIndicatorCode());
+                        //阈值类型
+                        String dataType = indicator.getString("dataType");
+                        //指标阈值
+                        String threshold = thresholdObj.getString(ruleBase.getId().toString());
+                        //阈值为空时,或者阈值为“unlimited”(不限),不执行该规则
+                        if (Check.isNull(threshold) || "unlimited".equals(threshold)) {
+                            continue;
+                        }
+                        JSONArray dimensionData = matchData.getJSONArray(ruleBase.getRuleDimension());
+                        //账户维度数据
+                        if ("account".equals(ruleBase.getRuleDimension())) {
+                            JSONObject accountEntity = dimensionData.getJSONObject(0);
+                            if (MatchLogic.matchCondition(dataType, ruleBase.getRuleCondition(), threshold, accountEntity.getString(ruleBase.getIndicatorCode()))) {
+                                accountDatas.add(accountEntity);
+                            } else {
+                                log.warn("判断规则组({}),账户规则不符合", ruleGroup.getId());
+                                break;
+                            }
+                        } else if ("plan".equals(ruleBase.getRuleDimension())) {
+                            planDatas = getOKData(planDatas, dimensionData, ruleBase, dataType, threshold);
+                        } else if ("creative".equals(ruleBase.getRuleDimension())) {
+                            creativeDatas = getOKData(creativeDatas, dimensionData, ruleBase, dataType, threshold);
+                        } else if ("target".equals(ruleBase.getRuleDimension())) {
+                            targetDatas = getOKData(targetDatas, dimensionData, ruleBase, dataType, threshold);
+                        }
 
-                }
-                //获取达标可发送的数据
-                JSONArray sendData = MatchLogic.getSendData(accountDatas, planDatas, creativeDatas, targetDatas);
-                if (!Check.isNull(sendData)) {
-                    //执行发送
-                    for (int i = 0; i < sendData.size(); i++) {
-                        sendMsg(ruleGroup, sendData.getJSONObject(i));
+                    }
+                    //获取达标可发送的数据
+                    JSONArray sendData = MatchLogic.getSendData(accountDatas, planDatas, creativeDatas, targetDatas);
+                    if (!Check.isNull(sendData)) {
+                        //执行发送
+                        for (int i = 0; i < sendData.size(); i++) {
+                            sendMsg(ruleGroup, sendData.getJSONObject(i));
+                        }
                     }
                 }
             }
+        } catch (Exception e) {
+            log.error("匹配规则逻辑异常", e);
         }
     }
 
@@ -246,12 +247,14 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
         Long planId = obj.getLong("planId");
         Long accountId = obj.getLong("accountId");
         JSONObject user = getUserByAccountId(accountId);
-        boolean isPause = "PAUSE".equals(ruleGroup.getOperate());
+        //TODO 关停操作
+        boolean isPause = false;
+          /*      "PAUSE".equals(ruleGroup.getOperate());
         if (isPause) {
             if (shutDown(accountId, planId, creativeId, user.getString("id"))) {
                 isPause = true;
             }
-        }
+        }*/
         String msg = MatchLogic.getMsg(ruleGroup, user, accountId, planId, creativeId, isPause);
         String sendType = ruleGroup.getSendType();
         if ("SMS".equals(sendType)) {
@@ -303,22 +306,31 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
         if (Check.isNull(thresholdList)) {
             return null;
         }
-        JSONObject obj = new JSONObject();
-        for (RuleAccountThreshold threshold : thresholdList) {
-            obj.put(String.valueOf(threshold.getRuleId()), threshold.getThreshoId());
+        JSONObject obj = null;
+        try {
+            obj = new JSONObject();
+            for (RuleAccountThreshold threshold : thresholdList) {
+                obj.put(String.valueOf(threshold.getRuleId()), threshold.getThreshoId());
+            }
+        } catch (Exception e) {
+            log.error("获取账户阈值数据集失败", e);
         }
         return obj;
     }
 
     //整理指标数据
     public JSONObject getRuleIndicator() {
-        List<RuleIndicator> indicators = ruleIndicatorMapper.selectByMap(null);
-        if (Check.isNull(indicators)) {
-            return null;
-        }
         JSONObject obj = new JSONObject();
-        for (RuleIndicator indicator : indicators) {
-            obj.put(indicator.getCode(), indicators);
+        try {
+            List<RuleIndicator> indicators = ruleIndicatorMapper.selectByMap(null);
+            if (Check.isNull(indicators)) {
+                return null;
+            }
+            for (RuleIndicator indicator : indicators) {
+                obj.put(indicator.getCode(), indicator);
+            }
+        } catch (Exception e) {
+            log.error("获取指标数据集异常", e);
         }
         return obj;
     }
@@ -333,20 +345,25 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
      * @author ZHAOXA
      */
     private Map<Long, List<RuleGroup>> getRuleGroupMap() {
-        Map<Long, List<RuleGroup>> map = new HashMap<>();
-        List<RuleTemplate> ruleTemplates = ruleTemplateMapper.selectByMap(null);
-        ruleTemplates.forEach(tem -> {
-            QueryWrapper<RuleGroup> groupWrapper = new QueryWrapper<>();
-            groupWrapper.eq("template_id", tem.getId());
-            List<RuleGroup> ruleGroups = ruleGroupMapper.selectList(groupWrapper);
-            for (RuleGroup group : ruleGroups) {
-                QueryWrapper<RuleBase> baseWrapper = new QueryWrapper<>();
-                baseWrapper.eq("group_id", group.getId());
-                baseWrapper.orderByDesc("is_unlimited");
-                group.setRuleBaseList(ruleBaseMapper.selectList(baseWrapper));
-            }
-            map.put(tem.getId(), ruleGroups);
-        });
+        try {
+            Map<Long, List<RuleGroup>> map = new HashMap<>();
+            List<RuleTemplate> ruleTemplates = ruleTemplateMapper.selectByMap(null);
+            ruleTemplates.forEach(tem -> {
+                QueryWrapper<RuleGroup> groupWrapper = new QueryWrapper<>();
+                groupWrapper.eq("template_id", tem.getId());
+                List<RuleGroup> ruleGroups = ruleGroupMapper.selectList(groupWrapper);
+                for (RuleGroup group : ruleGroups) {
+                    QueryWrapper<RuleBase> baseWrapper = new QueryWrapper<>();
+                    baseWrapper.eq("group_id", group.getId());
+                    baseWrapper.orderByDesc("is_unlimited");
+                    group.setRuleBaseList(ruleBaseMapper.selectList(baseWrapper));
+                }
+                map.put(tem.getId(), ruleGroups);
+            });
+            return map;
+        } catch (Exception e) {
+            log.error("获取模板规则组集合异常", e);
+        }
         return null;
     }
 
@@ -360,7 +377,12 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
      * @author ZHAOXA
      */
     private JSONObject getRuleData(Long accountId) {
-        return ruleDataAccountService.getRuleDataByAccountId(accountId);
+        try {
+            return ruleDataAccountService.getRuleDataByAccountId(accountId);
+        } catch (Exception e) {
+            log.error("获取匹配数据集异常", e);
+        }
+        return null;
     }
 
     /**

+ 13 - 5
module-common/src/main/java/cn/com/ctop/common/module/service/impl/RuleDataAccountServiceImpl.java

@@ -1,6 +1,9 @@
 package cn.com.ctop.common.module.service.impl;
 
 import cn.com.ctop.common.module.entity.RuleDataAccount;
+import cn.com.ctop.common.module.entity.RuleDataCreative;
+import cn.com.ctop.common.module.entity.RuleDataPlan;
+import cn.com.ctop.common.module.entity.RuleDataTarget;
 import cn.com.ctop.common.module.mapper.RuleDataAccountMapper;
 import cn.com.ctop.common.module.mapper.RuleDataCreativeMapper;
 import cn.com.ctop.common.module.mapper.RuleDataPlanMapper;
@@ -13,6 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -42,11 +46,15 @@ public class RuleDataAccountServiceImpl extends ServiceImpl<RuleDataAccountMappe
         JSONObject obj = new JSONObject();
         Map<String, Object> map = new HashMap<>();
         map.put("account_id", accountId);
-        map.put("status", NoEn.NO1.valueStr());
-        obj.put("account", ruleDataAccountMapper.selectByMap(map));
-        obj.put("plan", ruleDataPlanMapper.selectByMap(map));
-        obj.put("target", ruleDataTargetMapper.selectByMap(map));
-        obj.put("creative", ruleDataCreativeMapper.selectByMap(map));
+        map.put("status", NoEn.NO1.valueInt());
+        List<RuleDataAccount> ruleDataAccounts = ruleDataAccountMapper.selectByMap(map);
+        obj.put("account", ruleDataAccounts);
+        List<RuleDataPlan> ruleDataPlans = ruleDataPlanMapper.selectByMap(map);
+        obj.put("plan", ruleDataPlans);
+        List<RuleDataTarget> ruleDataTargets = ruleDataTargetMapper.selectByMap(map);
+        obj.put("target", ruleDataTargets);
+        List<RuleDataCreative> ruleDataCreatives = ruleDataCreativeMapper.selectByMap(map);
+        obj.put("creative", ruleDataCreatives);
         return obj;
     }
 }