Jelajahi Sumber

将部分info级别的日志修改为debug级别

lixing 3 tahun lalu
induk
melakukan
6cb684e9ac

+ 34 - 0
src/main/java/com/persagy/cache/AlarmInfoCache.java

@@ -234,4 +234,38 @@ public class AlarmInfoCache {
         }
     }
 
+    /**
+     * 获取缓存的报警条件数量
+     *
+     * @author lixing
+     * @version V1.0 2021/10/28 9:53 上午
+     */
+    public Integer getCachedConditionCount() {
+        return alarmConditionMap.size();
+    }
+
+    /**
+     * 获取缓存的设备数量
+     *
+     * @author lixing
+     * @version V1.0 2021/10/28 9:53 上午
+     */
+    public Integer getCachedObjCount() {
+        return objConditionMap.size();
+    }
+
+    /**
+     * 获取缓存的设备与报警条件关联关系数量
+     *
+     * @author lixing
+     * @version V1.0 2021/10/28 9:53 上午
+     */
+    public Integer getCachedObjConditionRelCount() {
+        int result = 0;
+        for (String objId : objConditionMap.keySet()) {
+            ObjConditionInfo objConditionInfo = objConditionMap.get(objId);
+            result += objConditionInfo.getConditions().size();
+        }
+        return result;
+    }
 }

+ 6 - 6
src/main/java/com/persagy/cache/AlarmLastTimeCache.java

@@ -52,8 +52,8 @@ public class AlarmLastTimeCache {
         Iterable<AlarmLastTime> alarmLastTimes = alarmLastTimeRepository.findAll();
         // alarmLastTimes 不会为空
         alarmLastTimes.forEach(alarmLastTime -> alarmLastTimeMap.put(alarmLastTime.getId(), alarmLastTime));
-        log.info("alarmLastTimeMap初始化完成");
-        log.info("当前alarmLastTimeMap为:[{}]", alarmLastTimeMapToString(alarmLastTimeMap));
+        log.debug("alarmLastTimeMap初始化完成");
+        log.debug("当前alarmLastTimeMap为:[{}]", alarmLastTimeMapToString(alarmLastTimeMap));
 
         // 初始化defineAlarmMap
         Iterable<ZktAlarmRecordDO> alarmRecords = alarmRecordRepository.findAll();
@@ -61,8 +61,8 @@ public class AlarmLastTimeCache {
         alarmRecords.forEach(
                 alarmRecord -> updateAlarmId(alarmRecord.getDefinitionId(), alarmRecord.getAlarmId())
         );
-        log.info("defineAlarmMap初始化完成");
-        log.info("当前defineAlarmMap为:[{}]", defineAlarmMap.toString());
+        log.debug("defineAlarmMap初始化完成");
+        log.debug("当前defineAlarmMap为:[{}]", defineAlarmMap.toString());
     }
 
     /**
@@ -209,8 +209,8 @@ public class AlarmLastTimeCache {
         alarmLastTimeMap.remove(alarmId);
         // 从数据库中移除
         alarmLastTimeRepository.deleteById(alarmId);
-        log.info("报警id[{}]从alarmLastTimeMap移除", alarmId);
-        log.info("当前alarmLastTimeMap缓存为:[{}]", alarmLastTimeMapToString(alarmLastTimeMap));
+        log.debug("报警id[{}]从alarmLastTimeMap移除", alarmId);
+        log.debug("当前alarmLastTimeMap缓存为:[{}]", alarmLastTimeMapToString(alarmLastTimeMap));
     }
 
     public AlarmLastTime get(String alarmId) {

+ 2 - 2
src/main/java/com/persagy/cache/CreatedAlarmIdsCache.java

@@ -42,8 +42,8 @@ public class CreatedAlarmIdsCache {
         alarmIdSet = Collections.synchronizedSet(new HashSet<>());
         Iterable<AlarmRecordIdsCache> alarmRecordIds = alarmRecordIdsCacheRepository.findAll();
         alarmRecordIds.forEach(alarmRecordIdsCache -> alarmIdSet.add(alarmRecordIdsCache.getId()));
-        log.info("CreatedAlarmIdsCache初始化完成");
-        log.info("当前CreatedAlarmIdsCache缓存为:[{}]", alarmIdSet.toString());
+        log.debug("CreatedAlarmIdsCache初始化完成");
+        log.debug("当前CreatedAlarmIdsCache缓存为:[{}]", alarmIdSet.toString());
     }
 
     /**

+ 1 - 0
src/main/java/com/persagy/client/GroupNettyClientHandler.java

@@ -177,6 +177,7 @@ public class GroupNettyClientHandler extends ChannelInboundHandlerAdapter {
                 default:
                     break;
             }
+            nettyMsgHandler.logCacheInfo();
             // 发送已接收回执
             nettyMsgHandler.acceptedReply(channelHandlerContext);
         }

+ 33 - 34
src/main/java/com/persagy/entity/AlarmDefine.java

@@ -5,38 +5,36 @@ import com.persagy.entity.v2.AlarmCondition;
 import lombok.Data;
 
 /**
-*@description:
-*@author:LuoGuangyi
-*@company:PersagyTechnologyCo.,Ltd
-*@since:2020/10/18 001814:37
-*@version:V1.0
- *{
- *     "streamId": "",
- *     "opCode": "",
- *     "content": [
- *        {
- *             "id": "000f9609-685e-47b7-bb7d-e91d31f21f8d",
- *             "objId": "Eq1101050029e8fa1f6e4c81483fbfd7c554e1cdcd2e",
- *             "projectId": "Pj1101050029",
- *             "itemCode": "1051",
- *             "category": "Eq",
- *             "level": "S",
- *             "remark": "更改报警等级333",
- *             "condition": {
- *             },
- *             "open": 0,
- *             "concern": 0,
- *             "userDefined": 0,
- *             "createUser": "System",
- *             "createTime": 1603268498000,
- *             "updateUser": "systemId",
- *             "updateTime": 1603372305000,
- *             "valid": 1
- *         }
- *     ]
+ * @description:
+ * @author:LuoGuangyi
+ * @company:PersagyTechnologyCo.,Ltd
+ * @since:2020/10/18 001814:37
+ * @version:V1.0 {
+ * "streamId": "",
+ * "opCode": "",
+ * "content": [
+ * {
+ * "id": "000f9609-685e-47b7-bb7d-e91d31f21f8d",
+ * "objId": "Eq1101050029e8fa1f6e4c81483fbfd7c554e1cdcd2e",
+ * "projectId": "Pj1101050029",
+ * "itemCode": "1051",
+ * "category": "Eq",
+ * "level": "S",
+ * "remark": "更改报警等级333",
+ * "condition": {
+ * },
+ * "open": 0,
+ * "concern": 0,
+ * "userDefined": 0,
+ * "createUser": "System",
+ * "createTime": 1603268498000,
+ * "updateUser": "systemId",
+ * "updateTime": 1603372305000,
+ * "valid": 1
  * }
- *
-**/
+ * ]
+ * }
+ **/
 @Data
 public class AlarmDefine {
     /**
@@ -97,10 +95,10 @@ public class AlarmDefine {
     /**
      * 将报警条件转换为报警定义
      *
-     * @param itemCode 报警类型编码
+     * @param itemCode       报警类型编码
      * @param alarmCondition 报警条件
-     * @param meterId 报警对象id
-     * @param projectId 项目id
+     * @param meterId        报警对象id
+     * @param projectId      项目id
      * @return 报警定义
      * @author lixing
      * @version V1.0 2021/10/22 5:59 下午
@@ -120,6 +118,7 @@ public class AlarmDefine {
         condition.setInfoCode(alarmCondition.getInfoCodes());
         condition.setConfigs(JSONObject.toJSONString(alarmCondition.getTriggerFrontend()));
         JSONObject effectTime = alarmCondition.getEffectTimeDetail();
+        effectTime = effectTime == null ? new JSONObject() : effectTime;
         effectTime.put("type", alarmCondition.getEffectTimeType());
         condition.setEffectTime(effectTime);
         condition.setRules(alarmCondition.getTriggerRules());

+ 24 - 18
src/main/java/com/persagy/service/impl/AlarmHandleServiceImpl.java

@@ -82,7 +82,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
      */
     @Override
     public void handleIotMsg(String msg) throws Exception {
-        log.info("接收到采集值:[{}]", msg);
+        log.debug("接收到采集值:[{}]", msg);
         // 校验采集值
         boolean validateIotMsgResult = validateIotMsg(msg);
         if (!validateIotMsgResult) {
@@ -158,7 +158,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
         // 获取设备的报警条件
         ObjConditionInfo objConditionInfo = alarmInfoCache.getAlarmConditionsByObjId(meterId);
         if (objConditionInfo == null || CollectionUtils.isEmpty(objConditionInfo.getConditions())) {
-            log.info("未获取到设备{}的报警条件", meterId);
+            log.warn("未获取到设备{}的报警条件", meterId);
             return;
         }
         LinkedList<ItemCodeCondition> itemCodeConditions = objConditionInfo.getConditions();
@@ -188,21 +188,27 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
                 log.error("参数:");
                 log.error(paramMap.toString());
             }
-            log.info("triggerResult:[{}],endResult:[{}]", triggerResult, endResult);
+            log.debug("triggerResult:[{}],endResult:[{}]", triggerResult, endResult);
             if (triggerResult && endResult) {
                 log.error("报警触发条件和报警恢复条件同时满足,请检查,报警定义详情【{}】", alarmCondition);
             }
 
             synchronized (conditionId.intern()) {
-                AlarmDefine alarmDefine = AlarmDefine.convert2AlarmDefine(itemCodeCondition.getItemCode(),
-                        alarmCondition, meterId, objConditionInfo.getProjectId());
+                AlarmDefine alarmDefine = null;
+                try {
+                    alarmDefine = AlarmDefine.convert2AlarmDefine(itemCodeCondition.getItemCode(),
+                            alarmCondition, meterId, objConditionInfo.getProjectId());
+                } catch (Exception e) {
+                    log.error("报警条件转换为报警定义出现异常", e);
+                    return;
+                }
                 //报警产生值满足(这里的满足不考虑报警持续时间)
                 if (triggerResult) {
-                    log.info("满足报警条件");
+                    log.debug("满足报警条件");
                     //报警的时候不考虑报警恢复,因为同时报警和报警恢复是不应该出现的
                     handleAlarmTriggerData(alarmDefine, dataTime, paramMap);
                 } else {
-                    log.info("不满足报警条件");
+                    log.debug("不满足报警条件");
                     //当前数据正常
                     handlerNormalData(alarmDefine, dataTime, endResult, paramMap);
                 }
@@ -326,7 +332,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
                 }
             }
         }
-        log.info("alarmDefineState: [{}], ( 0-正常 1-报警)", alarmConditionState.getState());
+        log.debug("alarmDefineState: [{}], ( 0-正常 1-报警)", alarmConditionState.getState());
         return alarmConditionState;
     }
 
@@ -374,7 +380,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
             }
 
             /* 超过报警恢复设置的持续时间 */
-            log.info("报警恢复持续时间大于设定时间:[{}]>[{}]", alarmSuspendLastTime, uphold);
+            log.debug("报警恢复持续时间大于设定时间:[{}]>[{}]", alarmSuspendLastTime, uphold);
 
             ZktAlarmRecordDO alarmRecordDO = getZktAlarmRecordDOWhenAlarmSuspend(defineId, dataTime, paramMap);
             String alarmId = alarmRecordDO.getAlarmId();
@@ -383,13 +389,13 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
             // 如果云端已经完成报警记录的创建,直接发送更新报警状态消息,并删除数据库中的报警信息
             if (createdAlarmIdsCache.contains(alarmId)) {
                 deleteZktAlarmRecordWhenAlarmSuspend(defineId);
-                log.info("发送报警恢复消息,报警id: [{}]", alarmId);
+                log.debug("发送报警恢复消息,报警id: [{}]", alarmId);
                 // 发送报警恢复消息
                 sendAlarmRecoverMessage(alarmDefine.getProjectId(), dataTime, paramMap, alarmId);
                 // 报警恢复后,从缓存中移除报警id
                 createdAlarmIdsCache.remove(alarmId);
             } else {
-                log.info("已创建的报警id中不包含[{}], 3分钟后重试发送报警恢复消息", alarmId);
+                log.debug("已创建的报警id中不包含[{}], 3分钟后重试发送报警恢复消息", alarmId);
                 // 创建恢复定时任务
                 createAlarmRecoverTimingJob(dataTime, paramMap, alarmRecordDO);
             }
@@ -581,7 +587,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
                 String alarmId = alarmRecordRepository.findById(defineId).orElse(new ZktAlarmRecordDO()).getAlarmId();
                 if (StringUtils.isNotBlank(alarmId)) {
                     alarmLastTimeCache.put(alarmId, Integer.valueOf(String.valueOf(lastTime)));
-                    log.info("开始拼装报警持续消息");
+                    log.debug("开始拼装报警持续消息");
                     initAlarmContinueMsg(alarmId, lastTime, paramMap, alarmDefine.getProjectId());
                 }
             }
@@ -598,11 +604,11 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
 
                 // 获取报警过期时间
                 LocalDateTime expireDateTime = getExpireLocalDateTime(dataTime, condition);
-                log.info("报警持续时间:[{}]", alarmLastTime);
-                log.info("设定的持续时间:[{}]", condition.getTriggerUphold());
+                log.debug("报警持续时间:[{}]", alarmLastTime);
+                log.debug("设定的持续时间:[{}]", condition.getTriggerUphold());
                 if (alarmLastTime >= condition.getTriggerUphold()) {
                     String alarmId = StringUtil.getUUID();
-                    log.info("报警持续时间大于设定的持续时间,产生一条报警: [{}]", alarmId);
+                    log.debug("报警持续时间大于设定的持续时间,产生一条报警: [{}]", alarmId);
                     // 有新的报警产生,alarmLastTimeMap中报警标记为可删除
                     alarmLastTimeCache.setDeleteAvailable(defineId);
                     alarmLastTimeCache.updateAlarmId(defineId, alarmId);
@@ -617,7 +623,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
                     //有过期时间,生成报警过期消息
                     if (expireDateTime != null) {
                         //过期时间
-                        log.info("产生一条定时过期报警消息, 过期时间为:{}", expireDateTime);
+                        log.debug("产生一条定时过期报警消息, 过期时间为:{}", expireDateTime);
                         // 创建一条过期任务
                         createExpireJob(alarmId, alarmDefine, alarmConditionState, expireDateTime);
                     }
@@ -625,8 +631,8 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
             }
         } else {
             // 其他条件全部改成默认值(不报警,不过期,报警开始时间和结束时间为空)
-            log.info("[{}]不在生效时间,重置报警状态", dataTime);
-            log.info("生效时间为:[{}]", condition.getEffectTime());
+            log.debug("[{}]不在生效时间,重置报警状态", dataTime);
+            log.debug("生效时间为:[{}]", condition.getEffectTime());
             alarmConditionState.reset();
         }
         alarmInfoCache.setAlarmState(defineId, alarmConditionState);

+ 23 - 0
src/main/java/com/persagy/service/impl/NettyMsgHandler.java

@@ -3,6 +3,7 @@ package com.persagy.service.impl;
 import cn.hutool.core.collection.CollectionUtil;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.TypeReference;
+import com.persagy.cache.AlarmInfoCache;
 import com.persagy.cache.AlarmLastTimeCache;
 import com.persagy.cache.CreatedAlarmIdsCache;
 import com.persagy.client.GroupNettyClient;
@@ -14,6 +15,7 @@ import com.persagy.utils.LockUtil;
 import io.netty.channel.ChannelHandlerContext;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
@@ -36,6 +38,11 @@ public class NettyMsgHandler {
     public CreatedAlarmIdsCache createdAlarmIdsCache;
     @Autowired
     public AlarmLastTimeCache alarmLastTimeCache;
+    @Autowired
+    public AlarmInfoCache alarmInfoCache;
+    @Value("${logging.level.com.persagy}")
+    private String logLevel;
+
     /**
      * 已接收回执
      *
@@ -119,6 +126,22 @@ public class NettyMsgHandler {
     }
 
     /**
+     * 日志记录当前缓存中的数据数量
+     *
+     * @author lixing
+     * @version V1.0 2021/10/28 9:58 上午
+     */
+    public void logCacheInfo() {
+        // 只在debug日志等级下才获取数据数量,避免预期之外的统计计算
+        if (!"debug".equals(logLevel)) {
+            return;
+        }
+        log.debug("当前缓存中报警条件数量:[{}]", alarmInfoCache.getCachedConditionCount());
+        log.debug("当前缓存中设备数量:[{}]", alarmInfoCache.getCachedObjCount());
+        log.debug("当前缓存中设备与报警条件关联关系数量:[{}]", alarmInfoCache.getCachedObjConditionRelCount());
+    }
+
+    /**
      * 缓存报警条件
      *
      * @param msg 全量报警条件消息

+ 11 - 7
src/main/resources/application.yml

@@ -39,14 +39,14 @@ spring:
 group:
   code: WD   #标识哪个集团 比如万达使用WD, 华润使用HR
   alarm:
-#    host: 192.168.17.55    #netty IP
-    host: localhost    #netty IP
+    host: 192.168.17.55    #netty IP
+    #    host: localhost    #netty IP
     port: 9986          #netty 端口9986
-terminal:  #边缘端IOT采集程序地址
-  alarm:  # 拼接后的地址为ws://host:port/suffix
+terminal: #边缘端IOT采集程序地址
+  alarm: # 拼接后的地址为ws://host:port/suffix
     compress: false    #采用的是压缩方式还是不压缩方式  true-压缩 false-不压缩
-#    host: 192.168.17.55
-    host: localhost
+    host: 192.168.17.55
+    #    host: localhost
     port: 8080
     suffix: websocket/iot   #websocker后缀
 project:
@@ -103,4 +103,8 @@ autolog:
     metadata-max-age-ms: 300000
     request-timeout-ms: 30000
     key-serializer: org.apache.kafka.common.serialization.StringSerializer
-    value-serializer: org.apache.kafka.common.serialization.StringSerializer
+    value-serializer: org.apache.kafka.common.serialization.StringSerializer
+logging:
+  level:
+    com:
+      persagy: debug

+ 0 - 1
src/main/resources/logback-spring.xml

@@ -89,5 +89,4 @@ configuration 子节点为 appender、logger、root       -->
         <appender-ref ref="stdout"/>
         <appender-ref ref="infoFile"/>
     </root>
-    <logger name="com.persagy" level="DEBUG"/>
 </configuration>