|  | @@ -2,7 +2,9 @@ package cn.com.ctop.alarm.modules.service.impl;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  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;
 | 
	
	
		
			
				|  | @@ -17,6 +19,7 @@ import org.json.JSONObject;
 | 
	
		
			
				|  |  |  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;
 | 
	
	
		
			
				|  | @@ -47,23 +50,156 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      public void checkRules() {
 | 
	
		
			
				|  |  | -        //账户绑定的规则模板
 | 
	
		
			
				|  |  | +        //查询账户绑定过的规则模板
 | 
	
		
			
				|  |  |          List<RuleAccountTemplate> ruleAccountTemplates = ruleAccountTemplateMapper.selectByMap(null);
 | 
	
		
			
				|  |  | -        //查询指标数据
 | 
	
		
			
				|  |  | +        if (Check.isNull(ruleAccountTemplates)) {
 | 
	
		
			
				|  |  | +            log.warn("查询账户绑定过的规则模板失败");
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //查询匹配数据
 | 
	
		
			
				|  |  |          List<JSONObject> dataList = getData();
 | 
	
		
			
				|  |  | +        if (Check.isNull(dataList)) {
 | 
	
		
			
				|  |  | +            log.warn("查询匹配数据失败");
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |          //获取规则集
 | 
	
		
			
				|  |  |          Map<Long, List<RuleGroup>> ruleGroupMap = getRuleGroupMap();
 | 
	
		
			
				|  |  | +        if (Check.isNull(ruleGroupMap)) {
 | 
	
		
			
				|  |  | +            log.warn("获取规则集失败");
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //获取指标内容
 | 
	
		
			
				|  |  | +        JSONObject indicators = getRuleIndicator();
 | 
	
		
			
				|  |  | +        if (Check.isNull(indicators)) {
 | 
	
		
			
				|  |  | +            log.warn("获取指标内容失败");
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |          for (RuleAccountTemplate templates : ruleAccountTemplates) {
 | 
	
		
			
				|  |  | -            for (JSONObject data : dataList) {
 | 
	
		
			
				|  |  | -                if (templates.getAccountId() == data.getLong("accountId")) {
 | 
	
		
			
				|  |  | +            for (JSONObject matchData : dataList) {
 | 
	
		
			
				|  |  | +                if (templates.getAccountId() == matchData.getLong("accountId")) {
 | 
	
		
			
				|  |  |                      List<RuleGroup> ruleGroups = ruleGroupMap.get(templates.getTemplateId());
 | 
	
		
			
				|  |  | -                    List<RuleAccountThreshold> list = ruleAccountThresholdMapper.selectByAccountId(templates.getAccountId());
 | 
	
		
			
				|  |  | +                    matchAlarmRules(ruleGroups, getThreshold(ruleAccountThresholdMapper.selectByAccountId(templates.getAccountId())), matchData, indicators);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 匹配预警规则
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param ruleGroups   规则集
 | 
	
		
			
				|  |  | +     * @param thresholdObj 规则id阈值
 | 
	
		
			
				|  |  | +     * @param matchData    匹配数据
 | 
	
		
			
				|  |  | +     * @param indicators   指标码
 | 
	
		
			
				|  |  | +     * @return void
 | 
	
		
			
				|  |  | +     * @throws
 | 
	
		
			
				|  |  | +     * @author ZHAOXA
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    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) {
 | 
	
		
			
				|  |  | +                //and关系,全部匹配规则
 | 
	
		
			
				|  |  | +                if (isAllTrue) {
 | 
	
		
			
				|  |  | +                    if (flag) {
 | 
	
		
			
				|  |  | +                        //指标阈值
 | 
	
		
			
				|  |  | +                        String threshold = thresholdObj.getString(ruleBase.getId().toString());
 | 
	
		
			
				|  |  | +                        //指标对象
 | 
	
		
			
				|  |  | +                        JSONObject indicator = indicators.getJSONObject(ruleBase.getIndicatorCode());
 | 
	
		
			
				|  |  | +                        flag = checkThreshold(threshold, ruleBase, indicator);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                } else {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private boolean checkThreshold(String threshold, RuleBase ruleBase, JSONObject indicator) {
 | 
	
		
			
				|  |  | +        //条件
 | 
	
		
			
				|  |  | +        String condition = ruleBase.getRuleCondition();
 | 
	
		
			
				|  |  | +        //指标
 | 
	
		
			
				|  |  | +        String indicatorCode = ruleBase.getIndicatorCode();
 | 
	
		
			
				|  |  | +        //维度
 | 
	
		
			
				|  |  | +        String ruleDimension = ruleBase.getRuleDimension();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        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) {
 | 
	
		
			
				|  |  | +        if (Check.isNull(thresholdList)) {
 | 
	
		
			
				|  |  | +            return null;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        JSONObject obj = new JSONObject();
 | 
	
		
			
				|  |  | +        for (RuleAccountThreshold threshold : thresholdList) {
 | 
	
		
			
				|  |  | +            obj.put(String.valueOf(threshold.getRuleId()), threshold.getThreshoId());
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        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);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return obj;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * 整理规则模板的数据,根据模板ID获取模板具体规则内容
 |