Bläddra i källkod

触发预警改为集群

zhaoxian 4 år sedan
förälder
incheckning
09de557ade

+ 13 - 0
module-alarm/src/main/java/cn/com/ctop/alarm/modules/controller/RuleGroupController.java

@@ -210,6 +210,19 @@ public class RuleGroupController {
         return result;
     }
 
+    @GetMapping(value = "/checkRulesByAccountId")
+    public Result<RuleGroup> checkRulesByAccountId(Long accountId) {
+        Result<RuleGroup> result = new Result<>();
+        try {
+            ruleGroupService.checkRulesByAccountId(accountId);
+            result.success("成功!");
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+            result.error500("操作失败");
+        }
+        return result;
+    }
+
     /**
      * 编辑
      *

+ 2 - 0
module-alarm/src/main/java/cn/com/ctop/alarm/modules/service/IRuleGroupService.java

@@ -12,6 +12,8 @@ import com.baomidou.mybatisplus.extension.service.IService;
  */
 public interface IRuleGroupService extends IService<RuleGroup> {
 
+    public void checkRulesByAccountId(Long accountId);
+
     void checkRules();
 
     JSONObject createRuleGroup(JSONObject requestJson);

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

@@ -111,37 +111,67 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
     static ExecutorService executorService = Executors.newFixedThreadPool(5);
 
     @Override
-    public void checkRules() {
-        log.info("------start------");
-        Long startTime = System.currentTimeMillis();
-        //查询账户绑定过的规则模板
-        Map<String, Object> map = new HashMap<>();
-        //0状态为启动
-        map.put("template_status", 0);
-        List<RuleAccountTemplate> ruleAccountTemplates = ruleAccountTemplateMapper.selectByMap(map);
-        if (Check.isNull(ruleAccountTemplates)) {
-            log.error("查询账户绑定过的规则模板失败");
-            return;
-        }
-        //获取规则集
-        Map<Long, Object> ruleGroupMap = getRuleGroupMap();
-        if (Check.isNull(ruleGroupMap)) {
-            log.error("获取规则集失败");
-            return;
-        }
-        //获取指标内容
-        JSONObject indicators = getRuleIndicator();
-        if (Check.isNull(indicators)) {
-            log.error("获取指标内容失败");
-            return;
+    public void checkRulesByAccountId(Long accountId) {
+        try {
+            QueryWrapper<RuleAccountTemplate> wrapper = new QueryWrapper<>();
+            //0状态为启动
+            wrapper.eq("account_id", accountId);
+            wrapper.eq("template_status", 0);
+            wrapper.last("limit 1");
+            RuleAccountTemplate ruleAccountTemplate = ruleAccountTemplateMapper.selectOne(wrapper);
+            if (Check.isNull(ruleAccountTemplate)) {
+                log.error("查询账户绑定过的规则模板失败");
+                return;
+            }
+            //获取规则集
+            Map<Long, Object> ruleGroupMap = getRuleGroupMapById(ruleAccountTemplate.getTemplateId());
+            if (Check.isNull(ruleGroupMap)) {
+                log.error("获取规则集失败");
+                return;
+            }
+            //获取指标内容
+            JSONObject indicators = getRuleIndicator();
+            if (Check.isNull(indicators)) {
+                log.error("获取指标内容失败");
+                return;
+            }
+            matchAlarmRules(ruleGroupMap, indicators, ruleAccountTemplate);
+        } catch (Exception e) {
+            log.error("预警触发异常", e);
         }
+    }
 
-        ruleAccountTemplates.forEach(templates -> executorService.submit(() -> {
-            matchAlarmRules(ruleGroupMap, indicators, templates);
-        }));
+    @Override
+    public void checkRules() {
+        try {
+            //查询账户绑定过的规则模板
+            Map<String, Object> map = new HashMap<>();
+            //0状态为启动
+            map.put("template_status", 0);
+            List<RuleAccountTemplate> ruleAccountTemplates = ruleAccountTemplateMapper.selectByMap(map);
+            if (Check.isNull(ruleAccountTemplates)) {
+                log.error("查询账户绑定过的规则模板失败");
+                return;
+            }
+            //获取规则集
+            Map<Long, Object> ruleGroupMap = getRuleGroupMap();
+            if (Check.isNull(ruleGroupMap)) {
+                log.error("获取规则集失败");
+                return;
+            }
+            //获取指标内容
+            JSONObject indicators = getRuleIndicator();
+            if (Check.isNull(indicators)) {
+                log.error("获取指标内容失败");
+                return;
+            }
 
-        Long endTime = System.currentTimeMillis();
-        log.info("------end------共耗时: {} ms", endTime - startTime);
+            ruleAccountTemplates.forEach(templates -> executorService.submit(() -> {
+                matchAlarmRules(ruleGroupMap, indicators, templates);
+            }));
+        } catch (Exception e) {
+            log.error("预警触发异常", e);
+        }
     }
 
     /**
@@ -201,7 +231,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
     }
 
     /**
-     * 组合规则匹配逻辑
+     * 规则匹配逻辑
      *
      * @param ruleGroups   规则组集
      * @param thresholdObj 阈值数据
@@ -217,6 +247,8 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
         for (RuleGroup ruleGroup : ruleGroups) {
             JSONArray complianceList = new JSONArray();
             List<RuleBase> ruleBaseList = ruleGroup.getRuleBaseList();
+            //存储触发预警的数据
+            List<AlarmRuleRecord> recordList = new ArrayList<>();
             if (Check.isNull(ruleBaseList)) {
                 continue;
             }
@@ -226,14 +258,14 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
                 for (Map.Entry<String, Object> entry : batchNo.entrySet()) {
                     JSONObject thresholdJn = (JSONObject) entry.getValue();
                     if (!Check.isNull(thresholdJn)) {
-                        CombinationRule(ruleGroup, thresholdJn, indicators, matchData, complianceList, type);
+                        CombinationRule(ruleGroup, thresholdJn, indicators, matchData, complianceList, type, recordList);
                     }
                 }
             } else {
                 boolean isGroup = "group".equals(ruleGroup.getRuleType());
                 //匹配组合规则
                 if (isGroup) {
-                    CombinationRule(ruleGroup, thresholdObj, indicators, matchData, complianceList, type);
+                    CombinationRule(ruleGroup, thresholdObj, indicators, matchData, complianceList, type, recordList);
                     //匹配单规则
                 } else {
                     RuleBase ruleBase = ruleBaseList.get(0);
@@ -255,7 +287,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
                             JSONObject obj = dimensionData.getJSONObject(i);
                             String value = obj.getString(ruleBase.getIndicatorCode());
                             if (MatchLogic.matchCondition(indicator, ruleBase.getRuleCondition(), threshold, value, obj, ruleBase.getIndicatorCode())) {
-                                alarmRuleRecord(obj, ruleGroup, ruleBase, indicator, type, threshold);
+                                alarmRuleRecord(obj, ruleGroup, ruleBase, indicator, type, threshold, recordList);
                                 complianceList.add(obj);
                             }
                         }
@@ -264,6 +296,13 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
             }
             if (!Check.isNull(complianceList)) {
                 getAndSendMessag(complianceList, ruleGroup, accountName, type);
+                Thread thread = new Thread() {
+                    @Override
+                    public void run() {
+                        alarmRuleRecordService.saveBatch(recordList);
+                    }
+                };
+                thread.start();
             }
         }
     }
@@ -276,7 +315,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
      * @throws
      * @author ZHAOXA
      */
-    private void alarmRuleRecord(JSONObject obj, RuleGroup ruleGroup, RuleBase ruleBase, JSONObject indicator, Integer mediaType, String threshold) {
+    private void alarmRuleRecord(JSONObject obj, RuleGroup ruleGroup, RuleBase ruleBase, JSONObject indicator, Integer mediaType, String threshold, List<AlarmRuleRecord> recordList) {
         try {
             Long planId = obj.getLong("planId");
             Long unidId = null;
@@ -304,7 +343,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
             alarmRuleRecord.setValueTime(obj.getDate("createTime"));
             alarmRuleRecord.setOperate("PAUSE".equals(ruleGroup.getOperate()) ? "关停" : "仅发送");
             alarmRuleRecord.setMediaType(mediaTypeName);
-            alarmRuleRecordService.save(alarmRuleRecord);
+            recordList.add(alarmRuleRecord);
         } catch (Exception e) {
             log.error("insert ctop_alarm_rule_record error ", e);
         }
@@ -324,7 +363,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
      * @throws
      * @author ZHAOXA
      */
-    private void CombinationRule(RuleGroup ruleGroup, JSONObject thresholdObj, JSONObject indicators, JSONObject matchData, JSONArray complianceList, Integer type) {
+    private void CombinationRule(RuleGroup ruleGroup, JSONObject thresholdObj, JSONObject indicators, JSONObject matchData, JSONArray complianceList, Integer type, List<AlarmRuleRecord> recordList) {
         List<RuleBase> ruleBaseList = ruleGroup.getRuleBaseList();
         //符合规则的数据集
         JSONArray groupDatas = new JSONArray();
@@ -380,7 +419,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
                 JSONObject accountEntity = dimensionData.getJSONObject(0);
                 if (MatchLogic.matchCondition(indicator, ruleBase.getRuleCondition(), threshold, accountEntity.getString(ruleBase.getIndicatorCode()), accountEntity, ruleBase.getIndicatorCode())) {
                     if (Check.isNull(accountDatas)) {
-                        alarmRuleRecord(accountEntity, ruleGroup, ruleBase, indicator, type, threshold);
+                        alarmRuleRecord(accountEntity, ruleGroup, ruleBase, indicator, type, threshold, recordList);
                         accountDatas.add(accountEntity);
                     }
                 } else {
@@ -402,7 +441,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
                     break;
                 }
             } else if (PLAN.equals(ruleBase.getRuleDimension())) {
-                planDatas = getOKData(planDatas, dimensionData, ruleBase, indicator, threshold, ruleGroup, type);
+                planDatas = getOKData(planDatas, dimensionData, ruleBase, indicator, threshold, ruleGroup, type, recordList);
                 if (Check.isNull(planDatas)) {
                     if (!Check.isNull(accountDatas)) {
                         accountDatas = null;
@@ -422,7 +461,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
                     break;
                 }
             } else if (CREATIVE.equals(ruleBase.getRuleDimension())) {
-                creativeDatas = getOKData(creativeDatas, dimensionData, ruleBase, indicator, threshold, ruleGroup, type);
+                creativeDatas = getOKData(creativeDatas, dimensionData, ruleBase, indicator, threshold, ruleGroup, type, recordList);
                 if (Check.isNull(creativeDatas)) {
                     if (!Check.isNull(accountDatas)) {
                         accountDatas = null;
@@ -442,7 +481,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
                     break;
                 }
             } else if ("target".equals(ruleBase.getRuleDimension())) {
-                targetDatas = getOKData(targetDatas, dimensionData, ruleBase, indicator, threshold, ruleGroup, type);
+                targetDatas = getOKData(targetDatas, dimensionData, ruleBase, indicator, threshold, ruleGroup, type, recordList);
                 if (Check.isNull(targetDatas)) {
                     if (!Check.isNull(accountDatas)) {
                         accountDatas = null;
@@ -462,7 +501,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
                     break;
                 }
             } else if (UNIT.equals(ruleBase.getRuleDimension())) {
-                groupDatas = getOKData(groupDatas, dimensionData, ruleBase, indicator, threshold, ruleGroup, type);
+                groupDatas = getOKData(groupDatas, dimensionData, ruleBase, indicator, threshold, ruleGroup, type, recordList);
                 if (Check.isNull(groupDatas)) {
                     if (!Check.isNull(accountDatas)) {
                         accountDatas = null;
@@ -505,14 +544,14 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
      * @throws
      * @author ZHAOXA
      */
-    private JSONArray getOKData(JSONArray ruleDatas, JSONArray dimensionData, RuleBase ruleBase, JSONObject indicator, String threshold, RuleGroup ruleGroup, Integer type) {
+    private JSONArray getOKData(JSONArray ruleDatas, JSONArray dimensionData, RuleBase ruleBase, JSONObject indicator, String threshold, RuleGroup ruleGroup, Integer type, List<AlarmRuleRecord> recordList) {
         if (!Check.isNull(ruleDatas)) {
             JSONArray okData = new JSONArray();
             for (int i = 0; i < ruleDatas.size(); i++) {
                 JSONObject obj = ruleDatas.getJSONObject(i);
                 String value = obj.getString(ruleBase.getIndicatorCode());
                 if (MatchLogic.matchCondition(indicator, ruleBase.getRuleCondition(), threshold, value, obj, ruleBase.getIndicatorCode())) {
-                    alarmRuleRecord(obj, ruleGroup, ruleBase, indicator, type, threshold);
+                    alarmRuleRecord(obj, ruleGroup, ruleBase, indicator, type, threshold, recordList);
                     okData.add(obj);
                 }
             }
@@ -522,7 +561,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
             JSONObject obj = dimensionData.getJSONObject(i);
             String value = obj.getString(ruleBase.getIndicatorCode());
             if (MatchLogic.matchCondition(indicator, ruleBase.getRuleCondition(), threshold, value, obj, ruleBase.getIndicatorCode())) {
-                alarmRuleRecord(obj, ruleGroup, ruleBase, indicator, type, threshold);
+                alarmRuleRecord(obj, ruleGroup, ruleBase, indicator, type, threshold, recordList);
                 ruleDatas.add(obj);
             }
         }
@@ -858,6 +897,45 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
         return obj;
     }
 
+    private Map<Long, Object> getRuleGroupMapById(Long id) {
+        try {
+            Map<Long, Object> returnMap = new HashMap<>();
+            Map<String, Object> parMap = new HashMap<>();
+            parMap.put("id", id);
+            List<RuleTemplate> ruleTemplates = ruleTemplateMapper.selectByMap(parMap);
+            for (RuleTemplate tem : ruleTemplates) {
+                Map<String, Object> map = new HashMap<>();
+                QueryWrapper<RuleGroup> groupWrapper = new QueryWrapper<>();
+                groupWrapper.eq("template_id", tem.getId());
+                groupWrapper.eq("is_copy", NoEn.NO1.valueInt());
+                List<RuleGroup> copyGroups = ruleGroupMapper.selectList(groupWrapper);
+                for (RuleGroup group : copyGroups) {
+                    QueryWrapper<RuleBase> baseWrapper = new QueryWrapper<>();
+                    baseWrapper.eq("group_id", group.getId());
+                    baseWrapper.orderByDesc("is_unlimited");
+                    group.setRuleBaseList(ruleBaseMapper.selectList(baseWrapper));
+                }
+                map.put("isCopy", copyGroups);
+                groupWrapper = new QueryWrapper<>();
+                groupWrapper.eq("template_id", tem.getId());
+                groupWrapper.eq("is_copy", NoEn.NO0.valueInt());
+                List<RuleGroup> notCopyGroups = ruleGroupMapper.selectList(groupWrapper);
+                for (RuleGroup group : notCopyGroups) {
+                    QueryWrapper<RuleBase> baseWrapper = new QueryWrapper<>();
+                    baseWrapper.eq("group_id", group.getId());
+                    baseWrapper.orderByDesc("is_unlimited");
+                    group.setRuleBaseList(ruleBaseMapper.selectList(baseWrapper));
+                }
+                map.put("notCopy", notCopyGroups);
+                map.put("type", tem.getMediaType());
+                returnMap.put(tem.getId(), map);
+            }
+            return returnMap;
+        } catch (Exception e) {
+            log.error("获取模板规则组集合异常", e);
+        }
+        return null;
+    }
 
     /**
      * 整理规则模板的数据,根据模板ID获取模板具体规则内容

+ 24 - 2
module-job-bytedance/src/main/java/cn/com/ctop/job/bytedance/handler/AlarmRuleJob.java

@@ -1,17 +1,29 @@
 package cn.com.ctop.job.bytedance.handler;
 
 
+import cn.com.ctop.alarm.modules.entity.RuleAccountTemplate;
+import cn.com.ctop.alarm.modules.mapper.RuleAccountTemplateMapper;
 import cn.com.ctop.alarm.modules.service.IRuleGroupService;
-import com.xxl.job.core.biz.model.ReturnT;
+import cn.com.ctop.common.module.utils.Check;
+import cn.com.ctop.common.module.utils.HttpUtils;
 import com.xxl.job.core.handler.annotation.XxlJob;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 @Component
 public class AlarmRuleJob {
     @Autowired
     private IRuleGroupService ruleGroupService;
 
+    @Autowired
+    private RuleAccountTemplateMapper ruleAccountTemplateMapper;
+
+    String URLPREFIX = "http://api.tjyourong.com.cn/jeecg-boot/ruleGroup/checkRulesByAccountId?accountId=";
+
     /**
      * 定时任务
      *
@@ -22,6 +34,16 @@ public class AlarmRuleJob {
      */
     @XxlJob("alarmRuleJob")
     public void execute() throws Exception {
-        ruleGroupService.checkRules();
+        //查询账户绑定过的规则模板
+        Map<String, Object> map = new HashMap<>();
+        //0状态为启动
+        map.put("template_status", 0);
+        List<RuleAccountTemplate> ruleAccountTemplates = ruleAccountTemplateMapper.selectByMap(map);
+        if (!Check.isNull(ruleAccountTemplates)) {
+            ruleAccountTemplates.forEach(template -> {
+                String url = URLPREFIX + template.getAccountId();
+                HttpUtils.httpGet(url, null, null);
+            });
+        }
     }
 }