Browse Source

预警规则匹配逻辑2

zhaoxian 5 years ago
parent
commit
a9d465a7de

+ 24 - 1
module-alarm/src/main/java/cn/com/ctop/alarm/modules/constant/MatchLogic.java

@@ -1,10 +1,14 @@
 package cn.com.ctop.alarm.modules.constant;
 
 import cn.com.ctop.alarm.modules.entity.AlarmEventRule;
+import cn.com.ctop.common.module.utils.Check;
+import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import lombok.extern.slf4j.Slf4j;
 
 import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * TODO
@@ -61,7 +65,7 @@ public class MatchLogic {
                     break;
             }
         } else {
-            String[] strings = threshold.split(",");
+            List<String> strings = strToList(threshold);
             if ("contain".equals(condition)) {
                 for (String string : strings) {
                     if (value.contains(string)) {
@@ -104,4 +108,23 @@ public class MatchLogic {
         return msg;
     }
 
+    //拆分逗号拼接数据
+    public static List<String> strToList(String strings) {
+        List<String> ids = null;
+        try {
+            if (Check.isNull(strings)) {
+                return null;
+            }
+            JSONObject job = new JSONObject();
+            job.put("list", strings);
+            ids = new ArrayList<>();
+            JSONArray idList = job.getJSONArray("list");
+            for (Object id : idList) {
+                ids.add(String.valueOf(id));
+            }
+        } catch (Exception e) {
+            log.error("ids格式转换异常", e);
+        }
+        return ids;
+    }
 }

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

@@ -1,6 +1,7 @@
 package cn.com.ctop.alarm.modules.service.impl;
 
 import cn.com.ctop.alarm.modules.constant.MatchLogic;
+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;
@@ -15,12 +16,16 @@ 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.entity.CtopOauthToken;
+import cn.com.ctop.common.module.service.ICtopOauthTokenService;
 import cn.com.ctop.common.module.service.ISendMessageService;
 import cn.com.ctop.common.module.utils.Check;
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouUpdateService;
 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 com.xxl.job.core.enums.NoEn;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -59,6 +64,11 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
     private IRuleAccountTemplateService ruleAccountTemplateService;
     @Autowired
     private ISendMessageService sendMessageService;
+    @Autowired
+    private IKuaiShouUpdateService kuaiShouUpdateService;
+    @Autowired
+    private ICtopOauthTokenService oauthTokenService;
+
 
     @Override
     public void checkRules() {
@@ -69,7 +79,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
             return;
         }
         //查询匹配数据
-        List<JSONObject> dataList = getData();
+        JSONObject dataList = getData();
         if (Check.isNull(dataList)) {
             log.warn("查询匹配数据失败");
             return;
@@ -87,12 +97,8 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
             return;
         }
         for (RuleAccountTemplate templates : ruleAccountTemplates) {
-            for (JSONObject matchData : dataList) {
-                if (templates.getAccountId() == matchData.getLong("accountId")) {
-                    List<RuleGroup> ruleGroups = ruleGroupMap.get(templates.getTemplateId());
-                    matchAlarmRules(ruleGroups, getThreshold(ruleAccountThresholdMapper.selectByAccountId(templates.getAccountId())), matchData, indicators);
-                }
-            }
+            List<RuleGroup> ruleGroups = ruleGroupMap.get(templates.getTemplateId());
+            matchAlarmRules(ruleGroups, getThreshold(ruleAccountThresholdMapper.selectByAccountId(templates.getAccountId())), dataList, indicators, templates.getAccountId());
         }
     }
 
@@ -107,54 +113,142 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
      * @throws
      * @author ZHAOXA
      */
-    private void matchAlarmRules(List<RuleGroup> ruleGroups, JSONObject thresholdObj, JSONObject matchData, JSONObject indicators) {
+    private void matchAlarmRules(List<RuleGroup> ruleGroups, JSONObject thresholdObj, JSONObject matchData, JSONObject indicators, Long accountId) {
+        //TODO 获取数据
+        JSONArray targetData = matchData.getJSONArray("target");
+        JSONArray planData = matchData.getJSONArray("plan");
+        JSONArray accountData = matchData.getJSONArray("account");
+        JSONArray creativeData = matchData.getJSONArray("creative");
+        JSONArray lostCostData = matchData.getJSONArray("lostCost");
         for (RuleGroup ruleGroup : ruleGroups) {
-            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());
+            boolean isBase = "base".equals(ruleGroup.getRuleType());
+            //匹配单规则
+            if (isBase) {
+                RuleBase ruleBase = ruleBaseList.get(0);
                 //指标对象
                 JSONObject indicator = indicators.getJSONObject(ruleBase.getIndicatorCode());
-                //and关系,全部匹配规则
-                if (isAllTrue) {
-                    if (flag) {
-                        flag = MatchLogic.matchCondition(indicator.getString("dataType"), ruleBase.getRuleCondition(), threshold, value);
+                //指标阈值
+                String threshold = thresholdObj.getString(ruleBase.getId().toString());
+                //阈值为空时,或者阈值为“unlimited”(不限),不执行该规则
+                if (Check.isNull(threshold) || "unlimited".equals(threshold)) {
+                    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);
+                        }
+                    }
+                }
+                //匹配组合规则
+            } else {
+                // 是否为and全部匹配
+                boolean isAllTrue = "and".equals(ruleGroup.getRuleRelationship());
+                boolean flag = false;
+                List<JSONObject> targetDatas = new ArrayList<>();
+                List<JSONObject> planDatas = new ArrayList<>();
+                List<JSONObject> creativeDatas = new ArrayList<>();
+                List<JSONObject> lostCostDatas = new ArrayList<>();
+                for (RuleBase ruleBase : ruleBaseList) {
+                    //指标对象
+                    JSONObject indicator = indicators.getJSONObject(ruleBase.getIndicatorCode());
+                    //指标阈值
+                    String threshold = thresholdObj.getString(ruleBase.getId().toString());
+                    //阈值为空时,或者阈值为“unlimited”(不限),不执行该规则
+                    if (Check.isNull(threshold) || "unlimited".equals(threshold)) {
+                        continue;
                     }
-                } else {
-                    //or关系,任一阈值匹配,则跳出循环
-                    flag = MatchLogic.matchCondition(indicator.getString("dataType"), ruleBase.getRuleCondition(), threshold, value);
-                    if (flag) {
-                        break;
+                    //and关系,全部匹配规则
+                    if (isAllTrue) {
+                        JSONArray dimensionData = matchData.getJSONArray(ruleBase.getRuleDimension());
+                        if ("account".equals(ruleBase.getRuleDimension())) {
+                            //and关系时如果账户级不匹配则跳出本规则。
+                            String value = dimensionData.getJSONObject(0).getString(ruleBase.getIndicatorCode());
+                            if (!MatchLogic.matchCondition(indicator.getString("dataType"), ruleBase.getRuleCondition(), threshold, value)) {
+                                break;
+                            }
+                        } else {
+                            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)) {
+//                                    okData.add(obj);
+                                }
+                            }
+                        }
+
+                    } else {
+                        //or关系,任一阈值匹配,则跳出循环
+//                        flag = MatchLogic.matchCondition(indicator.getString("dataType"), ruleBase.getRuleCondition(), threshold, value);
+                        if (flag) {
+                            break;
+                        }
                     }
                 }
+                //执行发送
+                if (flag) {
+                    sendMsg(ruleGroup);
+                }
             }
-            //执行发送
-            if (flag) {
-                boolean isPause = "PAUSE".equals(ruleGroup.getOperate());
-                if (isPause) {
-                    //关停操作 TODO
+        }
+    }
 
-                }
-                //TODO
-                String msg = MatchLogic.getMsg(null, null, null, null);
-                String sendType = ruleGroup.getSendType();
-                if ("SMS".equals(sendType)) {
+    /**
+     * 发送消息
+     *
+     * @param
+     * @return void
+     * @throws
+     * @author ZHAOXA
+     */
+    private void sendMsg(RuleGroup ruleGroup) {
+        boolean isPause = "PAUSE".equals(ruleGroup.getOperate());
+        if (isPause) {
+            //关停操作 TODO
+//                    shutDown();
+        }
+        //TODO
+        String msg = MatchLogic.getMsg(null, null, null, null);
+        String sendType = ruleGroup.getSendType();
+        if ("SMS".equals(sendType)) {
 
-                } else if ("WeChat".equals(sendType)) {
-                    sendMessageService.sendMessage("", msg);
-                } else if ("EMAIL".equals(sendType)) {
+        } else if ("WeChat".equals(sendType)) {
+            sendMessageService.sendMessage("", msg);
+        } else if ("EMAIL".equals(sendType)) {
 
-                } else if ("TEL".equals(sendType)) {
+        } else if ("TEL".equals(sendType)) {
 
-                } else {
+        } else {
 
-                }
+        }
+    }
 
+    /**
+     * 关停操作
+     *
+     * @param
+     * @return boolean
+     * @throws
+     * @author ZHAOXA
+     */
+    private boolean shutDown(AlarmEventRule rule, String level) {
+        CtopOauthToken token = oauthTokenService.getTokenByAccountId(rule.getAccountId());
+        Map<String, Object> updateMap = new HashMap<>();
+        if (!Check.isNull(token)) {
+            if ("CAMPAIGN".equals(level)) {
+                updateMap = kuaiShouUpdateService.updateCampaignStatus(token.getAccessToken(), rule.getAccountId(), rule.getCampaignId(), NoEn.NO2.valueInt(), rule.getUserId());
+                log.info("---------规则关停快手计划:{}", rule.getCampaignId());
+            } else if ("UNIT".equals(level)) {
+                updateMap = kuaiShouUpdateService.updateUnitStatus(token.getAccessToken(), rule.getAccountId(), rule.getUnitId(), NoEn.NO2.valueInt(), rule.getUserId());
+                log.info("---------规则关停快手组:{}", rule.getUnitId());
             }
         }
+        return (boolean) updateMap.get("success");
     }
 
     //整理阈值数据
@@ -195,11 +289,11 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
         Map<Long, List<RuleGroup>> map = new HashMap<>();
         List<RuleTemplate> ruleTemplates = ruleTemplateMapper.selectByMap(null);
         ruleTemplates.forEach(tem -> {
-            List<String> groupIds = strToList(tem.getGroupIds());
+            List<String> groupIds = MatchLogic.strToList(tem.getGroupIds());
             if (!Check.isNull(groupIds)) {
                 List<RuleGroup> ruleGroups = ruleGroupMapper.selectBatchIds(groupIds);
                 for (RuleGroup group : ruleGroups) {
-                    List<String> ruleIds = strToList(group.getRuleIds());
+                    List<String> ruleIds = MatchLogic.strToList(group.getRuleIds());
                     if (!Check.isNull(ruleIds)) {
                         group.setRuleBaseList(ruleBaseMapper.selectBatchIds(ruleIds));
                     }
@@ -210,25 +304,6 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
         return null;
     }
 
-    //拆分逗号拼接数据
-    private List<String> strToList(String strings) {
-        List<String> ids = null;
-        try {
-            if (Check.isNull(strings)) {
-                return null;
-            }
-            JSONObject job = new JSONObject();
-            job.put("list", strings);
-            ids = new ArrayList<>();
-            JSONArray idList = job.getJSONArray("list");
-            for (Object id : idList) {
-                ids.add(String.valueOf(id));
-            }
-        } catch (Exception e) {
-            log.error("ids格式转换异常", e);
-        }
-        return ids;
-    }
 
     /**
      * TODO 获取指标数据
@@ -238,7 +313,9 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
      * @throws
      * @author ZHAOXA
      */
-    private List<JSONObject> getData() {
+    private JSONObject getData() {
+
+
         return null;
     }