fix(biz): 优化环境监控告警逻辑
- 重构告警触发条件判断,区分switch属性与其他属性 - 新增closePreviousAlert方法统一处理告警关闭逻辑 - 修改设备指标转换器中alarm状态计算方式 - 完善注释说明switch属性状态含义(true为在线,false为离线) - 调整告警创建与更新策略,避免重复告警消息产生
This commit is contained in:
@@ -121,7 +121,13 @@ public interface LcPowerEnvMonitorMetricEntityConvert {
|
||||
Map<String, String> controlEnumValue = JSONUtil.toBean(metric.getControlEnumValue(), new TypeReference<Map<String, String>>() {
|
||||
}, true);
|
||||
deviceMetric.put(metric.getPropertyCode() + "View", controlEnumValue.get(metric.getValue()));
|
||||
if (StrUtil.equals(metric.getValue(), "true")) deviceMetric.put("alarm", Boolean.TRUE);
|
||||
|
||||
// 当值为true且属性不是开关,或者值为false且属性是开关时,触发告警
|
||||
boolean isAlarm = ("true".equals(metric.getValue()) && !"switch".equals(metric.getPropertyCode())) ||
|
||||
("false".equals(metric.getValue()) && "switch".equals(metric.getPropertyCode()));
|
||||
if (isAlarm) {
|
||||
deviceMetric.put("alarm", Boolean.TRUE);
|
||||
}
|
||||
}
|
||||
});
|
||||
return deviceMetric;
|
||||
|
||||
@@ -47,17 +47,25 @@ public class LcPowerEnvMetricEventListener {
|
||||
List<LcPowerEnvMonitorMetricEntity> recentData = metricService.getRecentData(metric.getDeviceUid(), metric.getPropertyCode(), metric.getCreateTime().minusMinutes(3));
|
||||
LcPowerEnvAlertMessageEntity alertMessage = alertService.getLatestAlertMessage(metric.getDeviceUid(), metric.getPropertyCode());
|
||||
|
||||
if ((StrUtil.equals(metric.getMetricValue(), "true") && !StrUtil.equals(metric.getPropertyCode(), "switch")) ||
|
||||
(StrUtil.equals(metric.getMetricValue(), "false") && StrUtil.equals(metric.getPropertyCode(), "switch"))) {
|
||||
// 判断三分钟内是否有告警,如果有,则延续告警消息,延长告警消息时间,如果没有则创建新的告警消息
|
||||
if (CollUtil.isNotEmpty(recentData)
|
||||
&& StrUtil.equals(CollUtil.getFirst(recentData).getMetricValue(), "true")) {
|
||||
// 更新告警消息
|
||||
Optional.ofNullable(alertMessage).ifPresent(alert -> {
|
||||
alert.setEndTime(metric.getUpdateTime());
|
||||
alertService.updateById(alert);
|
||||
});
|
||||
} else {
|
||||
// 根据属性编码和当前值判断是否需要触发告警
|
||||
boolean isSwitchProperty = StrUtil.equals(metric.getPropertyCode(), "switch");
|
||||
boolean shouldTriggerAlert;
|
||||
|
||||
if (isSwitchProperty) {
|
||||
// switch 开关状态正好是反过来,true是正常,代表在线,false是异常,代表离线。
|
||||
shouldTriggerAlert = StrUtil.equals(metric.getMetricValue(), "false");
|
||||
} else {
|
||||
shouldTriggerAlert = StrUtil.equals(metric.getMetricValue(), "true");
|
||||
}
|
||||
|
||||
// 获取最近一条数据的值用于比较
|
||||
String previousValue = CollUtil.isNotEmpty(recentData) ? CollUtil.getFirst(recentData).getMetricValue() : null;
|
||||
|
||||
if (shouldTriggerAlert) {
|
||||
// 需要触发告警的情况
|
||||
boolean shouldCreateNewAlert = previousValue == null || !StrUtil.equals(previousValue, metric.getMetricValue());
|
||||
|
||||
if (shouldCreateNewAlert) {
|
||||
// 新建告警消息,并且关闭之前的告警消息
|
||||
LcPowerEnvAlertMessageEntity newMessage = new LcPowerEnvAlertMessageEntity()
|
||||
.setDeviceUid(metric.getDeviceUid())
|
||||
@@ -69,15 +77,21 @@ public class LcPowerEnvMetricEventListener {
|
||||
.setAlertStatus(1L);
|
||||
alertService.save(newMessage);
|
||||
// 关闭之前的告警消息
|
||||
closePreviousAlert(alertMessage);
|
||||
} else {
|
||||
// 更新告警消息,延长告警时间
|
||||
Optional.ofNullable(alertMessage).ifPresent(alert -> {
|
||||
alert.setAlertStatus(0L);
|
||||
alert.setEndTime(metric.getUpdateTime());
|
||||
alertService.updateById(alert);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// 判断三分钟内是否有告警,如果有,则关闭告警消息,将告警消息的状态置为0,如果没有,则不用处理
|
||||
if (CollUtil.isNotEmpty(recentData)
|
||||
&& StrUtil.equals(CollUtil.getFirst(recentData).getMetricValue(), "true")) {
|
||||
// 不需要触发告警的情况,如果之前有告警则关闭它
|
||||
boolean shouldCloseAlert = previousValue != null &&
|
||||
((isSwitchProperty && StrUtil.equals(previousValue, "false")) ||
|
||||
(!isSwitchProperty && StrUtil.equals(previousValue, "true")));
|
||||
|
||||
if (shouldCloseAlert) {
|
||||
// 关闭最新的告警消息
|
||||
Optional.ofNullable(alertMessage).ifPresent(alert -> {
|
||||
alert.setAlertStatus(0L);
|
||||
@@ -87,6 +101,18 @@ public class LcPowerEnvMetricEventListener {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭之前的告警消息
|
||||
*
|
||||
* @param alertMessage 告警消息
|
||||
*/
|
||||
private void closePreviousAlert(LcPowerEnvAlertMessageEntity alertMessage) {
|
||||
Optional.ofNullable(alertMessage).ifPresent(alert -> {
|
||||
alert.setAlertStatus(0L);
|
||||
alertService.updateById(alert);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理指标更新事件
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user