Ver código fonte

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

syh 4 anos atrás
pai
commit
25ff06d7aa

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

@@ -138,6 +138,24 @@ public class MatchLogic {
         return header.toString();
     }
 
+    /**
+     * 整理发送的消息
+     */
+    public static String getMessage(String ruleGroupName, Long accountId, String accountName, String type, String codes, boolean isNull) {
+        StringBuffer buf = new StringBuffer();
+        buf.append(ruleGroupName).append("预警").append("<br/>").
+                append("所属账户:").append(accountId).append(",授权名称为:").append(accountName).append("<br/>");
+        if ("plan".equals(type)) {
+            buf.append("预警的计划:").append(codes).append("<br/>");
+        } else if ("creative".equals(type)) {
+            buf.append("预警的创意:").append(codes).append("<br/>");
+        }
+        if (isNull) {
+            buf.append("该规则未匹配到投放数据").append("<br/>");
+        }
+        return buf.toString();
+    }
+
     //拆分逗号拼接数据
     public static List<String> strToList(String strings) {
         List<String> ids = null;

+ 11 - 1
module-alarm/src/main/java/cn/com/ctop/alarm/modules/entity/vo/TemplateAppliedVo.java

@@ -1,6 +1,7 @@
 package cn.com.ctop.alarm.modules.entity.vo;
 
 public class TemplateAppliedVo {
+    private Long id;
     private Long accountId;
     private String authName;
     private Long templateId;
@@ -10,6 +11,14 @@ public class TemplateAppliedVo {
     private String userId;
     private String userName;
 
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
     public Long getAccountId() {
         return accountId;
     }
@@ -77,7 +86,8 @@ public class TemplateAppliedVo {
     @Override
     public String toString() {
         return "TemplateAppliedVo{" +
-                "accountId=" + accountId +
+                "id=" + id +
+                ", accountId=" + accountId +
                 ", authName='" + authName + '\'' +
                 ", templateId=" + templateId +
                 ", templateName='" + templateName + '\'' +

+ 1 - 0
module-alarm/src/main/java/cn/com/ctop/alarm/modules/mapper/xml/RuleAccountTemplateMapper.xml

@@ -5,6 +5,7 @@
     <select id="getTemplateApplied" parameterType="Map"
             resultType="cn.com.ctop.alarm.modules.entity.vo.TemplateAppliedVo">
         select
+        t1.id id,
         t1.account_id accountId,
         t2.auth_name authName,
         t1.template_id templateId,

+ 4 - 4
module-alarm/src/main/java/cn/com/ctop/alarm/modules/mapper/xml/RuleAccountThresholdMapper.xml

@@ -5,17 +5,17 @@
     <select id="selectByAccountId" resultType="cn.com.ctop.alarm.modules.entity.RuleAccountThreshold">
        SELECT * FROM ctop_rule_account_threshold
        WHERE account_id = #{accountId}
-       AND  batch_no =''
+       AND  (batch_no ='' or batch_no is null)
     </select>
     <select id="selectGroupIdsByAccountId" resultType="java.lang.Long">
        SELECT group_id FROM `ctop_rule_account_threshold`
-        WHERE  batch_no !=''
+        WHERE (batch_no !='' and batch_no is null)
         and  account_id = #{accountId}
         group by group_id
     </select>
     <select id="selectbatchNosByGroupIdId" resultType="java.lang.String">
        SELECT batch_no FROM `ctop_rule_account_threshold`
-        WHERE  batch_no !=''
+        WHERE  (batch_no !='' and batch_no is null)
         and account_id = #{accountId}
         and group_id = #{groupId}
         GROUP BY batch_no
@@ -31,7 +31,7 @@
     select   batch_no from ctop_rule_account_threshold
     where  account_id = #{accountId}
     and group_id = #{groupId}
-    and batch_no !=''
+    and (batch_no !='' and batch_no is null)
     group by batch_no
     </select>
 </mapper>

+ 0 - 3
module-alarm/src/main/java/cn/com/ctop/alarm/modules/service/impl/RuleAccountThresholdServiceImpl.java

@@ -82,9 +82,6 @@ public class RuleAccountThresholdServiceImpl extends ServiceImpl<RuleAccountThre
                             continue;
                         }
                         String threshold = ruleJson.getString("threshold");
-                        if (Check.isNull(threshold)) {
-                            continue;
-                        }
                         RuleAccountThreshold addRuleAccountThreshold = new RuleAccountThreshold();
                         addRuleAccountThreshold.setAccountId(accountId);
                         addRuleAccountThreshold.setGroupId(groupId);

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

@@ -38,6 +38,7 @@ import org.springframework.stereotype.Service;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.StringJoiner;
 import java.util.UUID;
 
 /**
@@ -107,7 +108,8 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
             return;
         }
         for (RuleAccountTemplate templates : ruleAccountTemplates) {
-            matchAlarmRules(ruleGroupMap, indicators, templates);
+            if (templates.getAccountId() == 1681223758914574l)
+                matchAlarmRules(ruleGroupMap, indicators, templates);
         }
         Long endTime = System.currentTimeMillis();
         log.info("------end------共耗时: {} ms", endTime - startTime);
@@ -152,7 +154,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
             accountName = userAllocations.getAuthName();
         }
         try {
-            if (!Check.isNull(notCopyGroups)) {
+            if (!Check.isNull(notCopyGroups) && !Check.isNull(notCopythreshold)) {
                 checkRuleGroups(notCopyGroups, notCopythreshold, indicators, matchData, type, false, accountName);
             }
             if (isCopy && !Check.isNull(isCopythreshold)) {
@@ -179,6 +181,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
      */
     private void checkRuleGroups(List<RuleGroup> ruleGroups, JSONObject thresholdObj, JSONObject indicators, JSONObject matchData, Integer type, boolean isCopy, String accountName) {
         for (RuleGroup ruleGroup : ruleGroups) {
+            JSONArray complianceList = new JSONArray();
             List<RuleBase> ruleBaseList = ruleGroup.getRuleBaseList();
             if (Check.isNull(ruleBaseList)) {
                 continue;
@@ -187,14 +190,16 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
             if (isCopy) {
                 JSONObject batchNo = thresholdObj.getJSONObject(ruleGroup.getId().toString());
                 for (Map.Entry<String, Object> entry : batchNo.entrySet()) {
-                    JSONObject thresholdMap = (JSONObject) entry.getValue();
-                    CombinationRule(ruleGroup, ruleBaseList, thresholdMap, indicators, matchData, type, accountName);
+                    JSONObject thresholdJn = (JSONObject) entry.getValue();
+                    if (!Check.isNull(thresholdJn)) {
+                        CombinationRule(ruleGroup, ruleBaseList, thresholdJn, indicators, matchData, type, accountName, complianceList);
+                    }
                 }
             } else {
                 boolean isGroup = "group".equals(ruleGroup.getRuleType());
                 //匹配组合规则
                 if (isGroup) {
-                    CombinationRule(ruleGroup, ruleBaseList, thresholdObj, indicators, matchData, type, accountName);
+                    CombinationRule(ruleGroup, ruleBaseList, thresholdObj, indicators, matchData, type, accountName, complianceList);
                     //匹配单规则
                 } else {
                     RuleBase ruleBase = ruleBaseList.get(0);
@@ -203,8 +208,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
                     //指标阈值
                     String threshold = thresholdObj.getString(ruleBase.getId().toString());
                     //阈值为空时,或者阈值为“unlimited”(不限),不执行该规则
-                    if (Check.isNull(threshold) || "unlimited".equals(threshold)) {
-                        log.warn("阈值为空/不限,该规则({})不执行", ruleBase.getId());
+                    if (Check.isNull(threshold) || threshold.contains("unlimited")) {
                         continue;
                     }
                     //维度数据
@@ -214,12 +218,15 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
                             JSONObject obj = dimensionData.getJSONObject(i);
                             String value = obj.getString(ruleBase.getIndicatorCode());
                             if (MatchLogic.matchCondition(indicator.getString("dataType"), ruleBase.getRuleCondition(), threshold, value, obj)) {
-                                sendMsg(ruleGroup, obj, type, accountName);
+                                complianceList.add(obj);
+//                                sendMsg(ruleGroup, obj, type, accountName);
                             }
                         }
                     }
                 }
             }
+
+
         }
     }
 
@@ -237,7 +244,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
      * @throws
      * @author ZHAOXA
      */
-    private void CombinationRule(RuleGroup ruleGroup, List<RuleBase> ruleBaseList, JSONObject thresholdObj, JSONObject indicators, JSONObject matchData, Integer type, String accountName) {
+    private void CombinationRule(RuleGroup ruleGroup, List<RuleBase> ruleBaseList, JSONObject thresholdObj, JSONObject indicators, JSONObject matchData, Integer type, String accountName, JSONArray complianceList) {
         //符合规则的数据集
         JSONArray targetDatas = new JSONArray();
         JSONArray planDatas = new JSONArray();
@@ -255,11 +262,11 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
                 continue;
             }
             JSONArray dimensionData = matchData.getJSONArray(ruleBase.getRuleDimension());
+            if (Check.isNull(dimensionData)) {
+                break;
+            }
             //账户维度数据
             if ("account".equals(ruleBase.getRuleDimension())) {
-                if (Check.isNull(dimensionData)) {
-                    return;
-                }
                 JSONObject accountEntity = dimensionData.getJSONObject(0);
                 if (MatchLogic.matchCondition(dataType, ruleBase.getRuleCondition(), threshold, accountEntity.getString(ruleBase.getIndicatorCode()), accountEntity)) {
                     accountDatas.add(accountEntity);
@@ -291,10 +298,11 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
         //获取达标可发送的数据
         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), type, accountName);
-            }
+            complianceList.addAll(sendData);
+//            //执行发送
+////            for (int i = 0; i < sendData.size(); i++) {
+////                sendMsg(ruleGroup, , type, accountName);
+////            }
         }
     }
 
@@ -309,7 +317,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
      * @author ZHAOXA
      */
     private JSONArray getOKData(JSONArray ruleDatas, JSONArray dimensionData, RuleBase ruleBase, String type, String threshold) {
-        if (ruleDatas.size() > 0) {
+        if (!Check.isNull(ruleDatas)) {
             JSONArray okData = new JSONArray();
             for (int i = 0; i < ruleDatas.size(); i++) {
                 JSONObject targetEntity = dimensionData.getJSONObject(i);
@@ -330,6 +338,83 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
         return ruleDatas;
     }
 
+    private void sendMag2(JSONArray complianceList, RuleGroup ruleGroup, String accountName) {
+        Long accountId = complianceList.getJSONObject(0).getLong("accountId");
+        if (Check.isNull(accountId)) {
+            return;
+        }
+        JSONObject user = getUserByAccountId(accountId);
+        StringJoiner creativeJoiner = new StringJoiner(",");
+        StringJoiner planJoiner = new StringJoiner(",");
+        StringJoiner isNullCreativeJoiner = new StringJoiner(",");
+        StringJoiner isNullPlanJoiner = new StringJoiner(",");
+        for (int i = 0; i < complianceList.size(); i++) {
+            JSONObject obj = complianceList.getJSONObject(i);
+            boolean isNull = obj.getBoolean("isNull");
+            if (isNull) {
+                if (!Check.isNull(obj.getString("creativeId"))) {
+                    //字节小于1900时
+                    if (isNullCreativeJoiner.toString().getBytes().length <= 1900) {
+                        isNullCreativeJoiner.add(obj.getString("creativeId"));
+                    } else {
+                        String message = MatchLogic.getMessage(ruleGroup.getGroupName(), accountId, accountName, "creative", isNullCreativeJoiner.toString(), false);
+                        sendMsg(message, ruleGroup, user);
+                        isNullCreativeJoiner = new StringJoiner(",");
+                    }
+                } else if (!Check.isNull(obj.getLong("planId"))) {
+                    if (isNullPlanJoiner.toString().getBytes().length <= 1900) {
+                        isNullPlanJoiner.add(obj.getString("planId"));
+                    } else {
+                        String msg = MatchLogic.getMessage(ruleGroup.getGroupName(), accountId, accountName, "plan", isNullPlanJoiner.toString(), false);
+                        sendMsg(msg, ruleGroup, user);
+                        isNullPlanJoiner = new StringJoiner(",");
+                    }
+                } else {
+                    String msg = MatchLogic.getMessage(ruleGroup.getGroupName(), accountId, accountName, null, null, false);
+                    sendMsg(msg, ruleGroup, user);
+                }
+            } else {
+                if (!Check.isNull(obj.getString("creativeId"))) {
+                    //字节小于1900时
+                    if (creativeJoiner.toString().getBytes().length <= 1900) {
+                        creativeJoiner.add(obj.getString("creativeId"));
+                    } else {
+                        String message = MatchLogic.getMessage(ruleGroup.getGroupName(), accountId, accountName, "creative", creativeJoiner.toString(), false);
+                        sendMsg(message, ruleGroup, user);
+                        creativeJoiner = new StringJoiner(",");
+                    }
+                } else if (!Check.isNull(obj.getLong("planId"))) {
+                    if (planJoiner.toString().getBytes().length <= 1900) {
+                        planJoiner.add(obj.getString("planId"));
+                    } else {
+                        String msg = MatchLogic.getMessage(ruleGroup.getGroupName(), accountId, accountName, "plan", planJoiner.toString(), false);
+                        sendMsg(msg, ruleGroup, user);
+                        planJoiner = new StringJoiner(",");
+                    }
+                } else {
+                    String msg = MatchLogic.getMessage(ruleGroup.getGroupName(), accountId, accountName, null, null, false);
+                    sendMsg(msg, ruleGroup, user);
+                }
+            }
+        }
+        String creativeMsg = MatchLogic.getMessage(ruleGroup.getGroupName(), accountId, "creative", creativeJoiner.toString(), accountName, false);
+        if (!Check.isNull(creativeMsg)) {
+            sendMsg(creativeMsg, ruleGroup, user);
+        }
+        String planMsg = MatchLogic.getMessage(ruleGroup.getGroupName(), accountId, "creative", planJoiner.toString(), accountName, false);
+        if (!Check.isNull(planMsg)) {
+            sendMsg(planMsg, ruleGroup, user);
+        }
+        String isNullCreativeMsg = MatchLogic.getMessage(ruleGroup.getGroupName(), accountId, "creative", isNullCreativeJoiner.toString(), accountName, false);
+        if (!Check.isNull(isNullCreativeMsg)) {
+            sendMsg(isNullCreativeMsg, ruleGroup, user);
+        }
+        String isNullPlanMsg = MatchLogic.getMessage(ruleGroup.getGroupName(), accountId, "creative", isNullPlanJoiner.toString(), accountName, false);
+        if (!Check.isNull(isNullPlanMsg)) {
+            sendMsg(isNullPlanMsg, ruleGroup, user);
+        }
+    }
+
     /**
      * 发送消息
      *
@@ -341,11 +426,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
      * @throws
      * @author ZHAOXA
      */
-    private void sendMsg(RuleGroup ruleGroup, JSONObject obj, Integer type, String accountName) {
-        Long creativeId = obj.getLong("creativeId");
-        Long planId = obj.getLong("planId");
-        Long accountId = obj.getLong("accountId");
-        JSONObject user = getUserByAccountId(accountId);
+    private void sendMsg(String msg, RuleGroup ruleGroup, JSONObject user) {
         //TODO 关停操作
         boolean isPause = false;
           /*      "PAUSE".equals(ruleGroup.getOperate());
@@ -355,7 +436,12 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
             }
             type 1:头条2:快手
         }*/
-        String msg = MatchLogic.getMsg(ruleGroup, user, obj, isPause, accountName);
+//        String msg = MatchLogic.getMsg(ruleGroup, user, obj, isPause, accountName);
+        if (isPause) {
+            msg += "现已被关停,请您及时查看并调整!";
+        } else {
+            msg += "请您及时查看并调整!";
+        }
         String sendType = ruleGroup.getSendType();
         if ("SMS".equals(sendType)) {
 
@@ -370,12 +456,20 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
             sendMessageService.sendMessage("1b3deb8258e84df994f1371a51cfc14a", msg);
             //113dee46c7df464da78c07a985e92cd1 于蒙
         }
-        log.debug("账户{}的{}预警已发送", accountId, ruleGroup.getGroupName());
         if (!Check.isNull(msg)) {
             AlarmEventSend send = new AlarmEventSend();
             send.setAlarmDetail(msg);
             send.setAlarmStatus(NoEn.NO1.valueStr());
             send.setEventRuleId(ruleGroup.getId());
+            if (msg.contains("预警账户")) {
+                send.setMetricValueCode("account");
+                if (msg.contains("预警计划")) {
+                    send.setMetricValueCode("plan");
+                    if (msg.contains("预警创意")) {
+                        send.setMetricValueCode("creative");
+                    }
+                }
+            }
             send.setAlarmCondition(ruleGroup.getGroupName());
             send.setUserId(user.getString("id"));
             alarmEventSendMapper.insert(send);

+ 5 - 0
module-job-bytedance/pom.xml

@@ -69,6 +69,11 @@
         </dependency>
         <dependency>
             <groupId>cn.com.ctop</groupId>
+            <artifactId>module-alarm</artifactId>
+            <version>2.0.2</version>
+        </dependency>
+        <dependency>
+            <groupId>cn.com.ctop</groupId>
             <artifactId>module-kuaishou</artifactId>
             <version>2.0.2</version>
         </dependency>

+ 28 - 0
module-job-bytedance/src/main/java/cn/com/ctop/job/bytedance/handler/AlarmRuleJob.java

@@ -0,0 +1,28 @@
+package cn.com.ctop.job.bytedance.handler;
+
+
+import cn.com.ctop.alarm.modules.service.IRuleGroupService;
+import com.xxl.job.core.biz.model.ReturnT;
+import com.xxl.job.core.handler.annotation.XxlJob;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+public class AlarmRuleJob {
+    @Autowired
+    private IRuleGroupService ruleGroupService;
+
+    /**
+     * 定时任务
+     *
+     * @param
+     * @return
+     * @throws
+     * @author ZHAOXA
+     */
+    @XxlJob("alarmRuleJob")
+    public ReturnT<String> execute(String params) throws Exception {
+        ruleGroupService.checkRules();
+        return ReturnT.SUCCESS;
+    }
+}