Browse Source

增量同步报警

xujiaheng 3 years ago
parent
commit
aabf2e31b7

+ 1 - 1
AlarmDataStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmdata/model/dto/AlarmCondition.java

@@ -39,7 +39,7 @@ public class AlarmCondition {
     /**
      * 存储用户自定义的配置
      */
-    private JSONArray configs;
+    private JSONObject configs;
     /**
      * 报警规则
      */

+ 66 - 4
AlarmEngineStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmengine/jms/AlarmEngineMsgHandler.java

@@ -1,8 +1,11 @@
 package com.persagy.apm.energyalarmstarter.alarmengine.jms;
 
+import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.json.JSONUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.persagy.apm.energyalarmstarter.alarmdata.model.vo.AlarmConfigItem;
 import com.persagy.apm.energyalarmstarter.alarmdata.mq.model.DmpMessage;
 import com.persagy.apm.energyalarmstarter.alarmengine.constants.RedisConstants;
 import com.persagy.apm.energyalarmstarter.alarmengine.feign.AlarmCondition;
@@ -11,15 +14,17 @@ import com.persagy.apm.energyalarmstarter.alarmengine.jms.model.ItemCodeConditio
 import com.persagy.apm.energyalarmstarter.alarmengine.jms.model.ObjConditionInfo;
 import com.persagy.apm.energyalarmstarter.alarmengine.redis.RedisUtil4EngineStater;
 import com.persagy.apm.energyalarmstarter.alarmengine.service.AlarmRecordMsgHandler;
+import io.micrometer.core.instrument.util.JsonUtils;
 import io.netty.channel.ChannelHandler;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import javax.annotation.Resource;
+
 import org.springframework.util.CollectionUtils;
 
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 /**
  * @description: 报警引擎消息处理类
@@ -113,7 +118,8 @@ public class AlarmEngineMsgHandler {
      */
     public void syncUpdatedCondition(DmpMessage msg) {
         AlarmCondition alarmCondition = JSONObject.parseObject(msg.getStr1(), AlarmCondition.class);
-        redisUtil.put(RedisConstants.ALARM_CONDITIONS, alarmCondition.getId(), JSON.toJSONString(alarmCondition));
+        // TODO: 2021/11/16  更新redis
+        redisUtil.put("ALARM_CONDITION", alarmCondition.getId(), JSON.toJSONString(alarmCondition));
     }
 
     /**
@@ -232,9 +238,65 @@ public class AlarmEngineMsgHandler {
             // 需要根据报警定义中的报警类型,从设备与报警条件的关联关系中找到对应的报警条件id。
             // 删除报警条件
             // 删除关联关系
+            List<AlarmConfigItem> alarmConfigItems = deletedConfigUniques.toJavaList(AlarmConfigItem.class);
+            for (AlarmConfigItem alarmConfigItem : alarmConfigItems) {
+                //从redis拿到设备的报警条件信息
+                ObjConditionInfo objConditionInfo = redisUtil.get(RedisConstants.OBJ_CONDITION_REL, alarmConfigItem.getObjId(), ObjConditionInfo.class);
+                LinkedList<ItemCodeCondition> conditions = objConditionInfo.getConditions();
+                LinkedList<ItemCodeCondition> conditionsRemain = objConditionInfo.getConditions();
+                for (ItemCodeCondition condition : conditions) {
+                    if (alarmConfigItem.getItemCode().equals(condition.getItemCode())) {
+                        //删除报警条件
+                        redisUtil.delete(RedisConstants.ALARM_CONDITIONS, condition.getConditionId());
+                    } else {
+                        conditionsRemain.add(condition);
+                    }
+                }
+                if (CollectionUtil.isEmpty(conditionsRemain)) {
+                    redisUtil.delete(RedisConstants.OBJ_CONDITION_REL, alarmConfigItem.getObjId());
+                } else {
+                    redisUtil.put(RedisConstants.OBJ_CONDITION_REL, alarmConfigItem.getObjId(), JSON.toJSONString(conditionsRemain));
+                }
+            }
         }
         if (!CollectionUtils.isEmpty(createdConfigUniques)) {
-            // 处理新增的报警定义 // TODO: 2021/11/20
+            List<AlarmConfigItem> alarmConfigItems = createdConfigUniques.toJavaList(AlarmConfigItem.class);
+            List<ObjConditionRel> createdRelList = new ArrayList<>();
+            for (AlarmConfigItem alarmConfigItem : alarmConfigItems) {
+                // 数据中台报警条件转化为业务中台报警条件和设备与条件的关联关系
+                AlarmCondition alarmCondition = avgAlarmDmpCondition(alarmConfigItem);
+                // 存储报警条件
+                redisUtil.put(RedisConstants.ALARM_CONDITIONS, alarmCondition.getId(), JSON.toJSONString(alarmCondition));
+                // 存储设备与条件的关联关系
+                ObjConditionRel objConditionRel = new ObjConditionRel();
+                objConditionRel.setConditionId(alarmCondition.getId());
+                objConditionRel.setObjId(alarmConfigItem.getObjId());
+                objConditionRel.setItemCode(alarmConfigItem.getItemCode());
+                objConditionRel.setProjectId(alarmConfigItem.getProjectId());
+                createdRelList.add(objConditionRel);
+            }
+            syncCreatedObjConditionRelList2Redis(createdRelList);
+        }
+    }
+
+    private  AlarmCondition  avgAlarmDmpCondition(AlarmConfigItem alarmConfigItem){
+        AlarmCondition alarmCondition = new AlarmCondition();
+        alarmCondition.setId(alarmConfigItem.getId());
+        List<String> infoCodes = new ArrayList<>();
+        for (String infoCode : alarmConfigItem.getCondition().getInfoCode()) {
+            infoCodes.add(infoCode.replaceAll(alarmConfigItem.getObjId()+"_",""));
         }
+        alarmCondition.setInfoCodes(infoCodes);
+        alarmCondition.setConfigs(alarmConfigItem.getCondition().getConfigs());
+        alarmCondition.setTriggerBackend(alarmConfigItem.getCondition().getTrigger().replaceAll(alarmConfigItem.getObjId()+"_",""));
+        alarmCondition.setTriggerUphold(alarmConfigItem.getCondition().getTriggerUphold()/60);
+        alarmCondition.setAlarmLevel(alarmConfigItem.getLevel());
+        alarmCondition.setEndUphold(alarmConfigItem.getCondition().getEndUphold()/60);
+        //alarmCondition.setTriggerRules(alarmDmpCondition.getRules());
+        alarmCondition.setEndBackend(alarmConfigItem.getCondition().getEnd().replaceAll(alarmConfigItem.getObjId()+"_",""));
+        //  alarmCondition.setEndRules(null);
+        alarmCondition.setEffectTimeType(alarmConfigItem.getCondition().getEffectTime().getType());
+        alarmCondition.setEffectTimeDetail((JSONObject) JSONObject.toJSON(alarmConfigItem.getCondition().getEffectTime()));
+        return alarmCondition;
     }
 }