|
|
|
|
@ -229,59 +229,6 @@ public class AlertLaunchServiceImpl implements IAlertLaunchService { |
|
|
|
|
/** |
|
|
|
|
* 单条规则执行逻辑(独立事务执行,避免互相影响) |
|
|
|
|
*/ |
|
|
|
|
// @Transactional(propagation = Propagation.REQUIRES_NEW)
|
|
|
|
|
// public Map<String, Integer> processRule(AlertRule rule, Integer currentBatch) {
|
|
|
|
|
// Map<String, Integer> failureCountMap = new HashMap<>();
|
|
|
|
|
// List<AlertMessage> messages = new ArrayList<>();
|
|
|
|
|
// List<String> msgs = new ArrayList<>();
|
|
|
|
|
//
|
|
|
|
|
// try {
|
|
|
|
|
// boolean isChangedRule = "CHANGED".equalsIgnoreCase(rule.getAlertSql());
|
|
|
|
|
// List<Map<String, Object>> queryResults = isChangedRule
|
|
|
|
|
// ? handleChangedRule(rule)
|
|
|
|
|
// : jdbcTemplate.queryForList(rule.getAlertSql());
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// for (Map<String, Object> row : queryResults) {
|
|
|
|
|
// AlertMessage msg = new AlertMessage();
|
|
|
|
|
// msg.setLevel(rule.getSeverity());
|
|
|
|
|
// msg.setType(rule.getType());
|
|
|
|
|
// msg.setOccurTime(new Date());
|
|
|
|
|
// msg.setStatus("UNHANDLED");
|
|
|
|
|
//
|
|
|
|
|
// String equId = row.get("equ_id") != null ? row.get("equ_id").toString() : null;
|
|
|
|
|
// msg.setEquId(equId);
|
|
|
|
|
// msg.setEquType(row.get("equ_type") != null ? row.get("equ_type").toString() : null);
|
|
|
|
|
// String msgdes = fillTemplate(rule.getAlertTemplate(), row, rule);
|
|
|
|
|
//
|
|
|
|
|
// msg.setBatch(currentBatch);
|
|
|
|
|
// msg.setMessage(msgdes);
|
|
|
|
|
// msg.setAlertRuleId(rule.getId());
|
|
|
|
|
// messages.add(msg);
|
|
|
|
|
// msgs.add(msgdes);
|
|
|
|
|
//
|
|
|
|
|
// if (equId != null) {
|
|
|
|
|
// failureCountMap.put(equId, failureCountMap.getOrDefault(equId, 0) + 1);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// // ⚙️ 发送预警 & 入库
|
|
|
|
|
// if (!messages.isEmpty()) {
|
|
|
|
|
// alertSender.sendAlert(msgs, rule.getAlertWay(), rule.getAlertStation());
|
|
|
|
|
// insertAlertMessages(messages);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// log.info("规则执行成功 → 规则ID={} 名称='{}',生成预警 {} 条",
|
|
|
|
|
// rule.getId(), rule.getRuleName(), messages.size());
|
|
|
|
|
//
|
|
|
|
|
// } catch (Exception e) {
|
|
|
|
|
// // ⚠️ 独立事务自动回滚,不影响其他规则
|
|
|
|
|
// log.error("【规则执行异常】规则ID={} 名称='{}',SQL={},原因:{}",
|
|
|
|
|
// rule.getId(), rule.getRuleName(), rule.getAlertSql(), e.getMessage(), e);
|
|
|
|
|
// }
|
|
|
|
|
// return failureCountMap;
|
|
|
|
|
// }
|
|
|
|
|
@Transactional(propagation = Propagation.REQUIRES_NEW) |
|
|
|
|
public Map<String, Integer> processRule(AlertRule rule, Integer currentBatch) { |
|
|
|
|
Map<String, Integer> failureCountMap = new HashMap<>(); |
|
|
|
|
@ -294,71 +241,43 @@ public class AlertLaunchServiceImpl implements IAlertLaunchService { |
|
|
|
|
? handleChangedRule(rule) |
|
|
|
|
: jdbcTemplate.queryForList(rule.getAlertSql()); |
|
|
|
|
|
|
|
|
|
// 设备ID -> AlertMessage
|
|
|
|
|
Map<String, AlertMessage> messageMap = new HashMap<>(); |
|
|
|
|
// 设备ID -> 拼接内容
|
|
|
|
|
Map<String, StringBuilder> msgBuilderMap = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
for (Map<String, Object> row : queryResults) { |
|
|
|
|
String equId = row.get("equ_id") != null ? row.get("equ_id").toString() : null; |
|
|
|
|
String equType = row.get("equ_type") != null ? row.get("equ_type").toString() : null; |
|
|
|
|
|
|
|
|
|
// 当前行的描述
|
|
|
|
|
String msgdes = fillTemplate(rule.getAlertTemplate(), row, rule); |
|
|
|
|
|
|
|
|
|
// 若设备ID为空,生成临时ID避免丢失记录
|
|
|
|
|
if (equId == null) { |
|
|
|
|
equId = UUID.randomUUID().toString(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 若该设备第一次出现,创建 AlertMessage 实体
|
|
|
|
|
AlertMessage msg = messageMap.get(equId); |
|
|
|
|
if (msg == null) { |
|
|
|
|
msg = new AlertMessage(); |
|
|
|
|
AlertMessage msg = new AlertMessage(); |
|
|
|
|
msg.setLevel(rule.getSeverity()); |
|
|
|
|
msg.setType(rule.getType()); |
|
|
|
|
msg.setOccurTime(new Date()); |
|
|
|
|
msg.setStatus("UNHANDLED"); |
|
|
|
|
|
|
|
|
|
String equId = row.get("equ_id") != null ? row.get("equ_id").toString() : null; |
|
|
|
|
msg.setEquId(equId); |
|
|
|
|
msg.setEquType(equType); |
|
|
|
|
msg.setEquType(row.get("equ_type") != null ? row.get("equ_type").toString() : null); |
|
|
|
|
String msgdes = fillTemplate(rule.getAlertTemplate(), row, rule); |
|
|
|
|
|
|
|
|
|
msg.setBatch(currentBatch); |
|
|
|
|
msg.setMessage(msgdes); |
|
|
|
|
msg.setAlertRuleId(rule.getId()); |
|
|
|
|
messageMap.put(equId, msg); |
|
|
|
|
|
|
|
|
|
msgBuilderMap.put(equId, new StringBuilder()); |
|
|
|
|
messages.add(msg); |
|
|
|
|
msgs.add(msgdes); |
|
|
|
|
|
|
|
|
|
// ✅ 第一次遇到该设备时计数一次
|
|
|
|
|
failureCountMap.put(equId, 1); |
|
|
|
|
if (equId != null) { |
|
|
|
|
failureCountMap.put(equId, failureCountMap.getOrDefault(equId, 0) + 1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 拼接消息(换行分隔)
|
|
|
|
|
msgBuilderMap.get(equId) |
|
|
|
|
.append("- ").append(msgdes).append("\n"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 拼接完成,填入 message 字段
|
|
|
|
|
for (Map.Entry<String, AlertMessage> entry : messageMap.entrySet()) { |
|
|
|
|
String equId = entry.getKey(); |
|
|
|
|
AlertMessage msg = entry.getValue(); |
|
|
|
|
msg.setMessage(msgBuilderMap.get(equId).toString().trim()); |
|
|
|
|
messages.add(msg); |
|
|
|
|
msgs.add(msg.getMessage()); |
|
|
|
|
} |
|
|
|
|
System.out.println("生成预警信息条数"+messageMap.size()); |
|
|
|
|
// ⚙️ 发送预警 & 入库
|
|
|
|
|
if (!messages.isEmpty()) { |
|
|
|
|
alertSender.sendAlert(msgs, rule.getAlertWay(), rule.getAlertStation()); |
|
|
|
|
insertAlertMessages(messages); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
log.info("规则执行成功 → 规则ID={} 名称='{}',生成预警 {} 条(涉及设备数 {})", |
|
|
|
|
rule.getId(), rule.getRuleName(), messages.size(), failureCountMap.size()); |
|
|
|
|
log.info("规则执行成功 → 规则ID={} 名称='{}',生成预警 {} 条", |
|
|
|
|
rule.getId(), rule.getRuleName(), messages.size()); |
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
// ⚠️ 独立事务自动回滚,不影响其他规则
|
|
|
|
|
log.error("【规则执行异常】规则ID={} 名称='{}',SQL={},原因:{}", |
|
|
|
|
rule.getId(), rule.getRuleName(), rule.getAlertSql(), e.getMessage(), e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return failureCountMap; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|