Просмотр исходного кода

告警:补充日志输出,确认传到重复次数服务的参数都必填,增加消息id,用处调试日志

yf_zn 9 месяцев назад
Родитель
Сommit
d400e8ed47

+ 4 - 0
src/main/java/com/yunfeiyun/agmp/iots/warn/model/WarnResult.java

@@ -17,6 +17,10 @@ public class WarnResult {
      */
     private String message;
     /**
+     * 消息标识,用来打日志用的,可用UUID生成
+     */
+    private String messageId;
+    /**
      * 设备id
      */
     private String devId;

+ 30 - 51
src/main/java/com/yunfeiyun/agmp/iots/warn/service/IotWarnBussinessService.java

@@ -9,6 +9,7 @@ import com.yunfeiyun.agmp.iot.common.domain.IotWarnlog;
 import com.yunfeiyun.agmp.iots.warn.mapper.IotWarnBussinessMapper;
 import com.yunfeiyun.agmp.iots.warn.model.WarnConfigInfo;
 import com.yunfeiyun.agmp.iots.warn.model.WarnResult;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -19,6 +20,7 @@ import java.util.List;
 import java.util.Map;
 
 @Service
+@Slf4j
 public class IotWarnBussinessService {
 
     @Autowired
@@ -27,42 +29,28 @@ public class IotWarnBussinessService {
     @Resource
     private RedisCacheManager redisCacheManager;
 
-    /**
-     * 查询设备+配置的重复次数
-     * 注意:如果没数据会返回Null
-     *
-     * @param devId
-     * @param configId
-     * @return
-     */
     public Long selectIotWarnCountByDevAndConfig(String devId, String configId) {
+        log.info("查询设备ID: {}, 配置ID: {} 的重复次数", devId, configId);
         Long count = redisCacheManager.getCacheObject(RedisCacheKey.IOT_WARN_RE_COUNTS, configId + ":" + devId);
         if (count != null) {
+            log.info("从缓存中获取到重复次数: {}", count);
             return count;
         }
-        // count==null说明什么?1. 缓存手动清空了,但是数据库还有,2. 不存在他的数据(数据库+缓存都没有)
         IotWarncount iotWarncount = iotWarncountMapper.selectIotWarnCountByDevAndConfig(devId, configId);
-        // 缓存没有,数据库也没有
         if (iotWarncount == null) {
+            log.warn("数据库中未找到设备ID: {}, 配置ID: {} 的重复次数数据", devId, configId);
             return null;
         } else {
-            // 缓存有,数据库有放缓存
+            log.info("从数据库中获取到重复次数: {}, 并将其加入缓存", iotWarncount.getWctCount());
             redisCacheManager.setCacheObject(RedisCacheKey.IOT_WARN_RE_COUNTS, configId + ":" + devId, iotWarncount.getWctCount());
             return iotWarncount.getWctCount();
         }
     }
 
-    /**
-     * 增加重复次数
-     *
-     * @param reCount  null 代表需要添加,有值代表需要更新
-     * @param devId
-     * @param configId
-     * @return
-     */
     public int incrementReCount(Long reCount, String devId, String configId, String tid) {
+        log.info("增加设备ID: {}, 配置ID: {} 的重复次数", devId, configId);
         if (reCount == null) {
-            //从新添加,就是从0变为1
+            log.info("初始化重复次数为1");
             IotWarncount iotWarncount = new IotWarncount();
             iotWarncount.setWctBid(IdUtils.fastUUID());
             iotWarncount.setDevBid(devId);
@@ -70,13 +58,15 @@ public class IotWarnBussinessService {
             iotWarncount.setLastUpdateTime(DateUtils.dateTimeNow());
             iotWarncount.setWctCount(1L);
             iotWarncount.setTid(tid);
-            int re = iotWarncountMapper.insertIncrementReCount(iotWarncount);
+            int result = iotWarncountMapper.insertIncrementReCount(iotWarncount);
             redisCacheManager.setCacheObject(RedisCacheKey.IOT_WARN_RE_COUNTS, configId + ":" + devId, iotWarncount.getWctCount());
-            return re;
+            log.info("初始化重复次数成功, 结果: {}", result);
+            return result;
         } else {
-            int re = iotWarncountMapper.updateIncrementReCount(devId, configId);
+            int result = iotWarncountMapper.updateIncrementReCount(devId, configId);
             redisCacheManager.setCacheObject(RedisCacheKey.IOT_WARN_RE_COUNTS, configId + ":" + devId, reCount + 1);
-            return re;
+            log.info("更新重复次数成功, 新的重复次数: {}", reCount + 1);
+            return result;
         }
     }
 
@@ -84,49 +74,38 @@ public class IotWarnBussinessService {
         return incrementReCount(reCount, warnResult.getDevId(), warnResult.getConfigId(), warnResult.getTid());
     }
 
-    /**
-     * 重置重复次数
-     *
-     * @return
-     */
     public int resetReCount() {
-        // 清空缓存
+        log.info("重置所有重复次数");
         redisCacheManager.deleteObject(RedisCacheKey.IOT_WARN_RE_COUNTS, "*");
-        // 清除表
         int result = iotWarncountMapper.resetReCount();
+        log.info("重置所有重复次数结果: {}", result);
         return result;
     }
 
-    /**
-     * 重置指定设备和配置的重复次数
-     *
-     * @return
-     */
     public int resetReCountByDevIdAndConfigId(String devId, String configId) {
-        // 清空缓存
+        log.info("重置设备ID: {}, 配置ID: {} 的重复次数", devId, configId);
         redisCacheManager.deleteObject(RedisCacheKey.IOT_WARN_RE_COUNTS, configId + ":" + devId);
-        return iotWarncountMapper.resetReCountByDevIdAndConfigId(devId, configId);
+        int result = iotWarncountMapper.resetReCountByDevIdAndConfigId(devId, configId);
+        log.info("重置结果: {}", result);
+        return result;
     }
 
-    /**
-     * 插入预警记录
-     *
-     * @return
-     */
     public int insertWarnRecord(IotWarnlog iotWarnlog) {
-        return iotWarncountMapper.insertWarnRecord(iotWarnlog);
+        log.info("插入预警记录: {}", iotWarnlog);
+        int result = iotWarncountMapper.insertWarnRecord(iotWarnlog);
+        log.info("插入预警记录结果: {}", result);
+        return result;
     }
 
-    /**
-     * 查询预警配置信息
-     *
-     * @return
-     */
     List<WarnConfigInfo> selectIotWarnConfigInfoList(WarnConfigInfo warnConfigInfo) {
-        return iotWarncountMapper.selectIotWarnConfigInfoList(warnConfigInfo);
+        log.info("查询预警配置信息列表");
+        List<WarnConfigInfo> list = iotWarncountMapper.selectIotWarnConfigInfoList(warnConfigInfo);
+        log.info("查询到 {} 条预警配置信息", list.size());
+        return list;
     }
 
     Map<String, List<WarnConfigInfo>> selectIotWarnConfigInfoMap(WarnConfigInfo warnConfigInfo) {
+        log.info("构建预警配置信息映射");
         List<WarnConfigInfo> warnConfigInfoList = selectIotWarnConfigInfoList(warnConfigInfo);
         Map<String, List<WarnConfigInfo>> warnConfigInfoMap = new HashMap<>();
         for (WarnConfigInfo info : warnConfigInfoList) {
@@ -136,7 +115,7 @@ public class IotWarnBussinessService {
             }
             warnConfigInfoMap.get(wcBid).add(info);
         }
+        log.info("构建完成,共包含 {} 个不同的配置ID", warnConfigInfoMap.size());
         return warnConfigInfoMap;
     }
-
 }

+ 62 - 31
src/main/java/com/yunfeiyun/agmp/iots/warn/service/ReCountService.java

@@ -1,10 +1,14 @@
 package com.yunfeiyun.agmp.iots.warn.service;
 
+import com.yunfeiyun.agmp.common.constant.ErrorCode;
+import com.yunfeiyun.agmp.common.exception.BizException;
 import com.yunfeiyun.agmp.common.utils.DateUtils;
+import com.yunfeiyun.agmp.common.utils.StringUtils;
 import com.yunfeiyun.agmp.common.utils.uuid.IdUtils;
 import com.yunfeiyun.agmp.iot.common.domain.IotWarnconfig;
 import com.yunfeiyun.agmp.iot.common.domain.IotWarnlog;
 import com.yunfeiyun.agmp.iots.warn.model.WarnResult;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -12,54 +16,89 @@ import org.springframework.stereotype.Service;
  * 重复次数服务
  */
 @Service
+@Slf4j
 public class ReCountService {
     @Autowired
     private IotWarnBussinessService iotWarnBussinessService;
 
     /**
      * 针对预警解析出来的结果进行处理
-     * 注:WarnResult是WarnService和ReCountService对接的重要对象,里面的对象参数保证都传过来
      *
      * @param warnResult
      */
     public void handlerMessage(WarnResult warnResult) {
         // 注意必要字段必须传过来
+        validateMessageParam(warnResult);
+
+        String messageId = warnResult.getMessageId();
+        log.info("【设备预警】消息标识{}:处理数据:{}", messageId, warnResult);
         // 触发了预警,进行校验处理
         if (warnResult.isTriggered()) {
-            // 当前的获取重复次数
             Long reCount = iotWarnBussinessService.selectIotWarnCountByDevAndConfig(warnResult.getDevId(), warnResult.getConfigId());
-            // 设定的重复次数阀值
             int targetReCount = warnResult.getTargetReCount();
-            // 当前重复的次数
             int thisReCount = reCount == null ? 0 : reCount.intValue();
-            // 达到阀值,进入产生预警记录
+            log.info("【设备预警】消息标识{}:当前设备ID:{}, 预警配置ID:{} 的重复次数为: {}", messageId, warnResult.getDevId(), warnResult.getConfigId(), thisReCount);
             if (thisReCount + 1 > targetReCount) {
-                // 已经达到阀值,直接存储,次数也不用增加了
-                iotWarnBussinessService.insertWarnRecord(buildWarnMessage(warnResult));
+                log.info("【设备预警】消息标识{}:达到或超过阈值, 不再增加重复次数", messageId);
+                iotWarnBussinessService.insertWarnRecord(buildWarnMessage(messageId, warnResult));
             } else if (thisReCount + 1 == targetReCount) {
-                // 初次达到阀值,并增加数值
+                log.info("【设备预警】消息标识{}:初次达到阈值, 增加重复次数并生成预警记录", messageId);
                 iotWarnBussinessService.incrementReCount(reCount, warnResult);
-                // 存储预警
-                iotWarnBussinessService.insertWarnRecord(buildWarnMessage(warnResult));
+                iotWarnBussinessService.insertWarnRecord(buildWarnMessage(messageId, warnResult));
             } else {
-                //没达到阀值,只增加数值
+                log.info("【设备预警】消息标识{}:未达阈值, 增加重复次数", messageId);
                 iotWarnBussinessService.incrementReCount(reCount, warnResult);
             }
 
         } else {
-            //没有触发,则清除之前的
-            cleanReCount(warnResult.getDevId(), warnResult.getConfigId());
+            log.info("【设备预警】消息标识{}:未触发预警, 清除之前的重复计数", messageId);
+            cleanReCount(messageId, warnResult.getDevId(), warnResult.getConfigId());
         }
-
     }
 
     /**
-     * 构建告警信息
+     * 验证 WarnResult 对象的所有必要参数是否有效
      *
-     * @param warnResult
-     * @return
+     * @param warnResult 要验证的预警结果对象
      */
-    IotWarnlog buildWarnMessage(WarnResult warnResult) {
+    public void validateMessageParam(WarnResult warnResult) {
+        if (StringUtils.isEmpty(warnResult.getMessageId())) {
+            log.error("【设备预警】消息标识 不可为空");
+            throw new BizException(ErrorCode.FAILURE.getCode(), "消息标识不可为空");
+        }
+        String messageId = warnResult.getMessageId();
+        if (StringUtils.isEmpty(warnResult.getReportData())) {
+            log.error("【设备预警】消息标识{}:上报数据不可为空", messageId);
+            throw new BizException(ErrorCode.FAILURE.getCode(), "上报数据不可为空");
+        }
+        if (StringUtils.isEmpty(warnResult.getDevId())) {
+            log.error("【设备预警】消息标识{}:设备id不可为空", messageId);
+            throw new BizException(ErrorCode.FAILURE.getCode(), "设备id不可为空");
+        }
+        if (StringUtils.isEmpty(warnResult.getConfigId())) {
+            log.error("【设备预警】消息标识{}:配置id不可为空", messageId);
+            throw new BizException(ErrorCode.FAILURE.getCode(), "配置id不可为空");
+        }
+        if (warnResult.getTargetReCount() < 0) {
+            log.error("【设备预警】消息标识{}:重复次数阈值必须大于等于0", messageId);
+            throw new BizException(ErrorCode.FAILURE.getCode(), "重复次数阈值必须大于等于0");
+        }
+        if (warnResult.getConfig() == null) {
+            log.error("【设备预警】消息标识{}:配置对象不可为空", messageId);
+            throw new BizException(ErrorCode.FAILURE.getCode(), "配置对象不可为空");
+        }
+        if (StringUtils.isEmpty(warnResult.getTid())) {
+            log.error("【设备预警】消息标识{}:tid不可为空", messageId);
+            throw new BizException(ErrorCode.FAILURE.getCode(), "tid不可为空");
+        }
+        if (StringUtils.isEmpty(warnResult.getDevtypeBid())) {
+            log.error("【设备预警】消息标识{}:设备型号id不可为空", messageId);
+            throw new BizException(ErrorCode.FAILURE.getCode(), "设备型号id不可为空");
+        }
+        log.info("【设备预警】{}:所有参数验证通过");
+    }
+
+    IotWarnlog buildWarnMessage(String messageId, WarnResult warnResult) {
         IotWarnconfig config = warnResult.getConfig();
         IotWarnlog iotWarnlog = new IotWarnlog();
         iotWarnlog.setWlBid(IdUtils.fastUUID());
@@ -72,32 +111,24 @@ public class ReCountService {
         iotWarnlog.setWlCreateddate(DateUtils.dateTimeNow());
         iotWarnlog.setWlData(warnResult.getReportData());
         iotWarnlog.setTid(config.getTid());
+        log.info("【设备预警】消息标识{}:构建预警信息: {}", messageId, iotWarnlog);
         return iotWarnlog;
     }
 
-    /**
-     * 如果这次没触发,但之前有值(重复次数),需要将重复次数为0
-     *
-     * @param devId
-     * @param configId
-     */
-    public void cleanReCount(String devId, String configId) {
-
+    public void cleanReCount(String messageId, String devId, String configId) {
         Long count = iotWarnBussinessService.selectIotWarnCountByDevAndConfig(devId, configId);
-        //优化缓存情况
         if (count == null) {
+            log.info("【设备预警】消息标识{}:没有找到对应的重复计数, 设备ID:{}, 配置ID:{}", messageId, devId, configId);
             return;
         }
         if (count != 0) {
+            log.info("【设备预警】消息标识{}:重置设备ID:{}, 配置ID:{} 的重复计数", messageId, devId, configId);
             iotWarnBussinessService.resetReCountByDevIdAndConfigId(devId, configId);
-            return;
         }
     }
 
-    /**
-     * 清空重复次数
-     */
     public void resetAllReCount() {
+        log.info("清空所有重复次数");
         iotWarnBussinessService.resetReCount();
     }
 }