feat(biz): 新增动环告警消息相关功能
- 新增动环告警消息实体类及对应数据库表结构 - 提供告警消息服务接口,支持查询最新告警信息 - 扩展监控指标服务,增加最近三分钟数据查询能力 - 完成低代码平台表单配置同步,包括字段、按钮、导出等设置
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package com.jeelowcode.module.biz.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.jeelowcode.framework.utils.model.global.BaseTenantEntity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 动环告警消息
|
||||
*
|
||||
* @author yangchenjj
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName("LC_POWER_ENV_ALERT_MESSAGE")
|
||||
public class LcPowerEnvAlertMessageEntity extends BaseTenantEntity {
|
||||
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@TableId(value = "ID", type = IdType.NONE)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 设备编码
|
||||
*/
|
||||
@TableField("DEVICE_UID")
|
||||
private String deviceUid;
|
||||
|
||||
/**
|
||||
* 告警编码
|
||||
*/
|
||||
@TableField("ALARM_CODE")
|
||||
private String alarmCode;
|
||||
|
||||
/**
|
||||
* 告警类型
|
||||
*/
|
||||
@TableField("ALARM_TYPE")
|
||||
private String alarmType;
|
||||
|
||||
/**
|
||||
* 告警消息
|
||||
*/
|
||||
@TableField("ALARM_MESSAGE")
|
||||
private String alarmMessage;
|
||||
|
||||
/**
|
||||
* 开始时间
|
||||
*/
|
||||
@TableField("START_TIME")
|
||||
private LocalDateTime startTime;
|
||||
|
||||
/**
|
||||
* 结束时间
|
||||
*/
|
||||
@TableField("END_TIME")
|
||||
private LocalDateTime endTime;
|
||||
|
||||
/**
|
||||
* 告警状态
|
||||
*/
|
||||
@TableField("ALERT_STATUS")
|
||||
private Long alertStatus;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.jeelowcode.module.biz.event;
|
||||
|
||||
import com.jeelowcode.module.biz.entity.LcPowerEnvMonitorMetricEntity;
|
||||
import lombok.Getter;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
/**
|
||||
* 动环设备监控指标插入事件
|
||||
*
|
||||
* @author yangchenjj
|
||||
*/
|
||||
@Getter
|
||||
public class LcPowerEnvMetricInsertEvent extends ApplicationEvent {
|
||||
|
||||
private final LcPowerEnvMonitorMetricEntity metric;
|
||||
|
||||
public LcPowerEnvMetricInsertEvent(Object source, LcPowerEnvMonitorMetricEntity metric) {
|
||||
super(source);
|
||||
this.metric = metric;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.jeelowcode.module.biz.event;
|
||||
|
||||
import com.jeelowcode.module.biz.entity.LcPowerEnvMonitorMetricEntity;
|
||||
import lombok.Getter;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
/**
|
||||
* 动环设备监控指标更新事件
|
||||
*
|
||||
* @author yangchenjj
|
||||
*/
|
||||
@Getter
|
||||
public class LcPowerEnvMetricUpdateEvent extends ApplicationEvent {
|
||||
|
||||
private final LcPowerEnvMonitorMetricEntity metric;
|
||||
|
||||
public LcPowerEnvMetricUpdateEvent(Object source, LcPowerEnvMonitorMetricEntity metric) {
|
||||
super(source);
|
||||
this.metric = metric;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package com.jeelowcode.module.biz.listener;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.NumberUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.jeelowcode.module.biz.entity.LcPowerEnvAlertMessageEntity;
|
||||
import com.jeelowcode.module.biz.entity.LcPowerEnvMonitorMetricEntity;
|
||||
import com.jeelowcode.module.biz.event.LcPowerEnvMetricInsertEvent;
|
||||
import com.jeelowcode.module.biz.event.LcPowerEnvMetricUpdateEvent;
|
||||
import com.jeelowcode.module.biz.service.ILcPowerEnvAlertMessageService;
|
||||
import com.jeelowcode.module.biz.service.ILcPowerEnvMonitorMetricService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 动环设备监控指标事件监听器
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class LcPowerEnvMetricEventListener {
|
||||
|
||||
@Resource
|
||||
private ILcPowerEnvAlertMessageService alertService;
|
||||
|
||||
@Resource
|
||||
private ILcPowerEnvMonitorMetricService metricService;
|
||||
|
||||
/**
|
||||
* 处理指标插入事件
|
||||
*
|
||||
* @param event 指标插入事件
|
||||
*/
|
||||
@EventListener
|
||||
public void handleMetricInsert(LcPowerEnvMetricInsertEvent event) {
|
||||
log.info("处理动环设备监控指标插入事件,设备UID: {}, 属性编码: {}",
|
||||
event.getMetric().getDeviceUid(), event.getMetric().getPropertyCode());
|
||||
// 首先判断传来的值是否是布尔类型,如果不是,则不用告警,则直接返回
|
||||
LcPowerEnvMonitorMetricEntity metric = event.getMetric();
|
||||
if (!NumberUtil.equals(metric.getValueType(), Integer.valueOf(20300))) return;
|
||||
|
||||
// 查询最近三分钟数据
|
||||
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")) {
|
||||
// 判断三分钟内是否有告警,如果有,则延续告警消息,延长告警消息时间,如果没有则创建新的告警消息
|
||||
if (CollUtil.isNotEmpty(recentData)
|
||||
&& StrUtil.equals(CollUtil.getFirst(recentData).getMetricValue(), "true")) {
|
||||
// 更新告警消息
|
||||
Optional.ofNullable(alertMessage).ifPresent(alert -> {
|
||||
alert.setEndTime(metric.getUpdateTime());
|
||||
alertService.updateById(alert);
|
||||
});
|
||||
} else {
|
||||
// 新建告警消息,并且关闭之前的告警消息
|
||||
LcPowerEnvAlertMessageEntity newMessage = new LcPowerEnvAlertMessageEntity()
|
||||
.setDeviceUid(metric.getDeviceUid())
|
||||
.setAlarmCode(metric.getPropertyCode())
|
||||
.setAlarmType(metric.getPropertyName())
|
||||
.setAlarmMessage(metric.getPropertyName() + "异常")
|
||||
.setStartTime(metric.getUpdateTime())
|
||||
.setEndTime(metric.getUpdateTime())
|
||||
.setAlertStatus(1L);
|
||||
alertService.save(newMessage);
|
||||
// 关闭之前的告警消息
|
||||
Optional.ofNullable(alertMessage).ifPresent(alert -> {
|
||||
alert.setAlertStatus(0L);
|
||||
alertService.updateById(alert);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// 判断三分钟内是否有告警,如果有,则关闭告警消息,将告警消息的状态置为0,如果没有,则不用处理
|
||||
if (CollUtil.isNotEmpty(recentData)
|
||||
&& StrUtil.equals(CollUtil.getFirst(recentData).getMetricValue(), "true")) {
|
||||
// 关闭最新的告警消息
|
||||
Optional.ofNullable(alertMessage).ifPresent(alert -> {
|
||||
alert.setAlertStatus(0L);
|
||||
alertService.updateById(alert);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理指标更新事件
|
||||
*
|
||||
* @param event 指标更新事件
|
||||
*/
|
||||
@EventListener
|
||||
public void handleMetricUpdate(LcPowerEnvMetricUpdateEvent event) {
|
||||
log.info("处理动环设备监控指标更新事件,设备UID: {}, 属性编码: {}",
|
||||
event.getMetric().getDeviceUid(), event.getMetric().getPropertyCode());
|
||||
// update 属于对指标信息的重复消费,这里置空不予处理
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.jeelowcode.module.biz.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.jeelowcode.module.biz.entity.LcPowerEnvAlertMessageEntity;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* 动环告警消息 Mapper
|
||||
*
|
||||
* @author yangchenjj
|
||||
*/
|
||||
@Mapper
|
||||
public interface LcPowerEnvAlertMessageMapper extends BaseMapper<LcPowerEnvAlertMessageEntity> {
|
||||
|
||||
}
|
||||
@@ -31,4 +31,19 @@ public interface LcPowerEnvMonitorMetricMapper extends BaseMapper<LcPowerEnvMoni
|
||||
.eq(LcPowerEnvMonitorMetricEntity::getUpdateTime, updateTime));
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* 查询指定设备和属性编码最近三分钟的数据
|
||||
*
|
||||
* @param deviceUid 设备唯一标识
|
||||
* @param propertyCode 属性编码
|
||||
* @return 最近三分钟内的数据列表
|
||||
*/
|
||||
default List<LcPowerEnvMonitorMetricEntity> selectRecentData(String deviceUid, String propertyCode, LocalDateTime startTime) {
|
||||
return selectList(new LambdaQueryWrapperX<LcPowerEnvMonitorMetricEntity>()
|
||||
.eq(LcPowerEnvMonitorMetricEntity::getDeviceUid, deviceUid)
|
||||
.eq(LcPowerEnvMonitorMetricEntity::getPropertyCode, propertyCode)
|
||||
.ge(LcPowerEnvMonitorMetricEntity::getUpdateTime, startTime)
|
||||
.orderByDesc(LcPowerEnvMonitorMetricEntity::getUpdateTime));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.jeelowcode.module.biz.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.jeelowcode.module.biz.entity.LcPowerEnvAlertMessageEntity;
|
||||
|
||||
/**
|
||||
* 动环告警消息 Service接口
|
||||
*
|
||||
* @author yangchenjj
|
||||
*/
|
||||
public interface ILcPowerEnvAlertMessageService extends IService<LcPowerEnvAlertMessageEntity> {
|
||||
|
||||
/**
|
||||
* 查询最新的告警消息
|
||||
*
|
||||
* @param deviceUid 设备编码
|
||||
* @param propertyCode 属性编码
|
||||
* @return 最新的告警消息
|
||||
*/
|
||||
LcPowerEnvAlertMessageEntity getLatestAlertMessage(String deviceUid, String propertyCode);
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.jeelowcode.module.biz.service;
|
||||
|
||||
import com.jeelowcode.module.biz.entity.LcPowerEnvMonitorMetricEntity;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -19,4 +20,13 @@ public interface ILcPowerEnvMonitorMetricService {
|
||||
*/
|
||||
int saveBatch(List<LcPowerEnvMonitorMetricEntity> list);
|
||||
|
||||
/**
|
||||
* 查询指定设备和属性编码最近三分钟数据
|
||||
*
|
||||
* @param deviceUid 设备唯一标识
|
||||
* @param propertyCode 属性编码
|
||||
* @return 最近三分钟数据列表
|
||||
*/
|
||||
List<LcPowerEnvMonitorMetricEntity> getRecentData(String deviceUid, String propertyCode, LocalDateTime startTime);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.jeelowcode.module.biz.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.jeelowcode.module.biz.entity.LcPowerEnvAlertMessageEntity;
|
||||
import com.jeelowcode.module.biz.mapper.LcPowerEnvAlertMessageMapper;
|
||||
import com.jeelowcode.module.biz.service.ILcPowerEnvAlertMessageService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 动环告警消息 Service实现类
|
||||
*
|
||||
* @author yangchenjj
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class LcPowerEnvAlertMessageServiceImpl extends ServiceImpl<LcPowerEnvAlertMessageMapper, LcPowerEnvAlertMessageEntity> implements ILcPowerEnvAlertMessageService {
|
||||
|
||||
@Override
|
||||
public LcPowerEnvAlertMessageEntity getLatestAlertMessage(String deviceUid, String propertyCode) {
|
||||
LambdaQueryWrapper<LcPowerEnvAlertMessageEntity> queryWrapper = new LambdaQueryWrapper<>();
|
||||
|
||||
// 添加设备编码过滤条件
|
||||
if (deviceUid != null && !deviceUid.isEmpty()) {
|
||||
queryWrapper.eq(LcPowerEnvAlertMessageEntity::getDeviceUid, deviceUid);
|
||||
}
|
||||
|
||||
// 添加属性编码过滤条件
|
||||
if (propertyCode != null && !propertyCode.isEmpty()) {
|
||||
queryWrapper.eq(LcPowerEnvAlertMessageEntity::getAlarmCode, propertyCode);
|
||||
}
|
||||
|
||||
// 按开始时间倒序排列,获取最新的记录
|
||||
queryWrapper.orderByDesc(LcPowerEnvAlertMessageEntity::getStartTime);
|
||||
|
||||
// 获取第一条记录即为最新的告警消息
|
||||
List<LcPowerEnvAlertMessageEntity> list = this.list(queryWrapper.last("LIMIT 1"));
|
||||
|
||||
return list.isEmpty() ? null : list.get(0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +1,17 @@
|
||||
package com.jeelowcode.module.biz.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.jeelowcode.module.biz.entity.LcPowerEnvMonitorMetricEntity;
|
||||
import com.jeelowcode.module.biz.event.LcPowerEnvMetricInsertEvent;
|
||||
import com.jeelowcode.module.biz.event.LcPowerEnvMetricUpdateEvent;
|
||||
import com.jeelowcode.module.biz.mapper.LcPowerEnvMonitorMetricMapper;
|
||||
import com.jeelowcode.module.biz.service.ILcPowerEnvMonitorMetricService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -35,13 +39,29 @@ public class LcPowerEnvMonitorMetricServiceImpl implements ILcPowerEnvMonitorMet
|
||||
return list.parallelStream().mapToInt(metric -> {
|
||||
List<LcPowerEnvMonitorMetricEntity> metricList = baseMapper.selectListByCondition(metric.getDeviceUid(), metric.getPropertyCode(), metric.getUpdateTime());
|
||||
if (CollUtil.isEmpty(metricList)) {
|
||||
// 插入指标前分析告警信息
|
||||
SpringUtil.publishEvent(new LcPowerEnvMetricInsertEvent(this, metric));
|
||||
return baseMapper.insert(metric);
|
||||
} else {
|
||||
metric.setId(CollUtil.getFirst(metricList).getId());
|
||||
// 更新指标前分析告警信息
|
||||
SpringUtil.publishEvent(new LcPowerEnvMetricUpdateEvent(this, metric));
|
||||
return baseMapper.updateById(metric);
|
||||
}
|
||||
}
|
||||
).sum();
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* 查询指定设备和属性编码最近三分钟的数据
|
||||
*
|
||||
* @param deviceUid 设备唯一标识
|
||||
* @param propertyCode 属性编码
|
||||
* @return 最近三分钟内的数据列表
|
||||
*/
|
||||
@Override
|
||||
public List<LcPowerEnvMonitorMetricEntity> getRecentData(String deviceUid, String propertyCode, LocalDateTime startTime) {
|
||||
return baseMapper.selectRecentData(deviceUid, propertyCode, startTime);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user