Browse Source

修改告警配置定时执行接口

zyl 3 weeks ago
parent
commit
c455332f1c

+ 1 - 1
liutongyi-admin/src/main/java/com/citygis/web/domain/SysWarningLog.java

@@ -73,7 +73,7 @@ public class SysWarningLog extends BaseEntity implements Serializable {
     @Excel(name = "告警唯一标识")
     @ApiModelProperty(value = "告警唯一标识")
     @TableField("ALERT_ID")
-    private String alertId;
+    private Long alertId;
 
     @TableField(exist = false)
     private List<Long> logIds;

+ 1 - 1
liutongyi-admin/src/main/java/com/citygis/web/mapper/OpenSqlMapper.java

@@ -5,6 +5,6 @@ import java.util.List;
 
 public interface OpenSqlMapper {
 
-    List<LinkedHashMap<String, Object>> getDateBySql(String sql);
+    List<LinkedHashMap<String, Object>> getDataBySql(String sql);
 
 }

+ 109 - 46
liutongyi-admin/src/main/java/com/citygis/web/task/AlarmTask.java

@@ -45,74 +45,137 @@ public class AlarmTask {
         Snowflake snowflake = new Snowflake(1, 1);
 
         SysWarningCheckInstance sysWarningCheckInstance = warningCheckInstanceService.getById(checkId);
+        if (sysWarningCheckInstance == null) {
+            log.error("未找到检查实例,checkId={}", checkId);
+            return null;
+        }
 
         Integer threshold = sysWarningCheckInstance.getThreshold();
+        SysWarningExecute executeConfig = warningExecuteService.getById(sysWarningCheckInstance.getExecuteId());
+        if (executeConfig == null) {
+            log.error("未找到执行配置,executeId={}", sysWarningCheckInstance.getExecuteId());
+            return null;
+        }
 
-        SysWarningExecute byId = warningExecuteService.getById(sysWarningCheckInstance.getExecuteId());
-
-        String sql = byId.getExecuteSql();
-
-        // 参数校验
+        String sql = executeConfig.getExecuteSql();
         if (sql == null || threshold == null) {
             log.error("参数无效: sql={}, threshold={}", sql, threshold);
-            throw new IllegalArgumentException("sql 或 threshold 是空的");
+            throw new IllegalArgumentException("SQL或阈值未设置");
         }
 
         try {
-            // 执行SQL查询
-            List<LinkedHashMap<String, Object>> dataBySql = openSqlMapper.getDateBySql(sql);
-
+            List<LinkedHashMap<String, Object>> dataBySql = openSqlMapper.getDataBySql(sql);
             if (dataBySql.isEmpty()) {
-                log.info("无数据");
+                log.info("SQL查询无数据,sql={}", sql);
                 return null;
             }
 
-            // 获取所有列名(使用第一个结果行的key集合)
             Set<String> columns = dataBySql.get(0).keySet();
+            SysWarningLog lastWarningLog = null;
+
+            for (LinkedHashMap<String, Object> row : dataBySql) {
+                for (String column : columns) {
+                    Object value = row.get(column);
+                    if (value == null) {
+                        log.debug("列 {} 的值为空", column);
+                        continue;
+                    }
+
+                    try {
+                        int numericValue = Integer.parseInt(value.toString());
+                        if (numericValue >= threshold) {
+                            SysWarningLog logEntry = new SysWarningLog();
+                            logEntry.setLogId(snowflake.nextId());
+                            logEntry.setTargetName(sysWarningCheckInstance.getTargetName());
+                            logEntry.setCheckId(sysWarningCheckInstance.getCheckId());
+                            logEntry.setCheckName(sysWarningCheckInstance.getCheckName());
+                            logEntry.setDescription(String.format(
+                                    "警告: 列 '%s' 值 %d 超过阈值 %d",
+                                    column, numericValue, threshold
+                            ));
+                            logEntry.setLevel(sysWarningCheckInstance.getLevel());
+                            logEntry.setCreateTime(new Date());
+                            // 设置正确的alertId,示例中使用检查实例的ID
+                            logEntry.setAlertId(sysWarningCheckInstance.getAlertId());
+
+                            warningLogService.save(logEntry);
+                            log.warn("触发告警: 列 {} 值 {} 超过阈值 {}", column, numericValue, threshold);
+                            lastWarningLog = logEntry;
+                        }
+                    } catch (NumberFormatException e) {
+                        log.error("列 '{}' 的值 '{}' 无法转换为整数", column, value, e);
+                    }
+                }
+            }
 
-            SysWarningLog sysWarningLog = new SysWarningLog();
-            // 遍历每一行数据
-            dataBySql.forEach(row ->
-                    columns.forEach(column -> {
-                        Object value = row.get(column);
+            return lastWarningLog;
+        } catch (Exception e) {
+            log.error("执行告警任务失败,SQL: {}", sql, e);
+            return null;
+        }
+    }
 
-                        if (value == null) {
-                            log.debug("列 {} 无值", column);
-                            return;
-                        }
 
-                        try {
-                            // 尝试转换为整型数值
-                            int intValue = Integer.parseInt(value.toString());
+    public SysWarningLog alarm2(Integer checkId) {
+        Snowflake snowflake = new Snowflake(1, 1);
 
-                            if (intValue >= threshold) {
+        SysWarningCheckInstance sysWarningCheckInstance = warningCheckInstanceService.getById(checkId);
+        if (sysWarningCheckInstance == null) {
+            log.error("未找到检查实例,checkId={}", checkId);
+            return null;
+        }
 
-                                String warning = String.format("警告: 列 '%s' 值 %d 超过 设定 %d", column, intValue, threshold);
+        Integer threshold = sysWarningCheckInstance.getThreshold();
+        SysWarningExecute executeConfig = warningExecuteService.getById(sysWarningCheckInstance.getExecuteId());
+        if (executeConfig == null) {
+            log.error("未找到执行配置,executeId={}", sysWarningCheckInstance.getExecuteId());
+            return null;
+        }
 
-                                sysWarningLog.setLogId(snowflake.nextId());
-                                sysWarningLog.setTargetName(sysWarningCheckInstance.getTargetName());
-                                sysWarningLog.setCheckId(sysWarningCheckInstance.getCheckId());
-                                sysWarningLog.setCheckName(sysWarningCheckInstance.getCheckName());
-                                sysWarningLog.setDescription(warning);
-                                sysWarningLog.setLevel(sysWarningCheckInstance.getLevel());
-                                sysWarningLog.setCreateTime(new Date());
-                                sysWarningLog.setAlertId(sysWarningLog.getAlertId());
+        String sql = executeConfig.getExecuteSql();
+        if (sql == null || threshold == null) {
+            log.error("参数无效: sql={}, threshold={}", sql, threshold);
+            throw new IllegalArgumentException("SQL或阈值未设置");
+        }
 
-                                warningLogService.save(sysWarningLog);
+        try {
+            List<LinkedHashMap<String, Object>> dataBySql = openSqlMapper.getDataBySql(sql);
+            if (dataBySql.isEmpty()) {
+                log.info("SQL查询无数据,sql={}", sql);
+                return null;
+            }
 
-                                // 触发报警(此处可替换为实际报警逻辑)
-                                log.warn("警告: 列 '{}' 值 {} 超过告警值 {}",
-                                        column, intValue, threshold);
-                            }
-                        } catch (NumberFormatException e) {
-                            log.error("列中的非整数值'{}': {}", column, value);
-                        }
-                    })
-            );
-            return sysWarningLog;
+            Set<String> columns = dataBySql.get(0).keySet();
+            SysWarningLog lastWarningLog = null;
+
+            for (LinkedHashMap<String, Object> row : dataBySql) {
+                Integer num = Integer.parseInt(row.get("num").toString());
+                String tableName = row.get("tableName").toString();
+                if (num >= threshold){
+                    SysWarningLog logEntry = new SysWarningLog();
+                    logEntry.setLogId(snowflake.nextId());
+                    logEntry.setTargetName(sysWarningCheckInstance.getTargetName());
+                    logEntry.setCheckId(sysWarningCheckInstance.getCheckId());
+                    logEntry.setCheckName(sysWarningCheckInstance.getCheckName());
+                    logEntry.setDescription(String.format(
+                            "警告: 表 '%s' 值 %d 超过阈值 %d",
+                            tableName, num, threshold
+                    ));
+                    logEntry.setLevel(sysWarningCheckInstance.getLevel());
+                    logEntry.setCreateTime(new Date());
+                    // 设置正确的alertId,示例中使用检查实例的ID
+                    logEntry.setAlertId(sysWarningCheckInstance.getAlertId());
+
+                    warningLogService.save(logEntry);
+                    log.warn("触发告警: 表 {} 值 {} 超过阈值 {}", tableName, num, threshold);
+                    lastWarningLog = logEntry;
+                }
+            }
+
+            return lastWarningLog;
         } catch (Exception e) {
-            log.error("执行警报任务时出错: {}", sql, e);
+            log.error("执行告警任务失败,SQL: {}", sql, e);
+            return null;
         }
-        return null;
     }
 }

+ 1 - 1
liutongyi-admin/src/main/resources/mapper/OpenSqlMapper.xml

@@ -2,7 +2,7 @@
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.citygis.web.mapper.OpenSqlMapper">
 
-    <select id="getDateBySql" parameterType="String" resultType="java.util.LinkedHashMap">
+    <select id="getDataBySql" parameterType="String" resultType="java.util.LinkedHashMap">
         ${sql}
     </select>