添加自动清理过期报警记录功能,允许用户设置报警记录保留天数

This commit is contained in:
lin 2026-04-07 10:54:56 +08:00
parent d1de343167
commit 4e54db42da
3 changed files with 20 additions and 4 deletions

View File

@ -239,12 +239,16 @@ public class UserSetting {
/**
* 是否使用拉流的方式获取快照默认false避免流量大规模消耗开启后则使用拉流的方式获取快照
*/
private boolean snapByPullStream = false;
private boolean alarmSnapByStream = false;
/**
* 报警订阅白名单设置后只有在此列表中的上级平台才会接收报警订阅消息默认不设置则不限制
*/
private List<AlarmType> allowedAlarmType = new ArrayList<>();
/**
* 报警记录保留天数超过此天数的报警记录将在每天凌晨自动清理默认30天设置为0则不自动清理
*/
private int alarmKeepDays = 7;
}

View File

@ -27,10 +27,11 @@ import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
@ -93,6 +94,18 @@ public class AlarmServiceImpl implements IAlarmService {
}
}
@Scheduled(cron = "0 0 0 * * ?")
public void cleanExpiredAlarms() {
int keepDays = userSetting.getAlarmKeepDays();
if (keepDays <= 0) {
return;
}
String endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(new Date(System.currentTimeMillis() - (long) keepDays * 24 * 3600 * 1000));
int count = clearAlarmsByCondition(null, null, endTime);
log.info("自动清理过期报警记录完成,保留天数:{},清理数量:{}", keepDays, count);
}
@Scheduled(fixedDelay = 500)
public void executeAlarmQueue() {
if (alarmQueue.isEmpty()) {
@ -171,7 +184,6 @@ public class AlarmServiceImpl implements IAlarmService {
}
@Override
@Transactional
public int clearAlarmsByCondition(List<AlarmType> alarmType, String beginTime, String endTime) {
Long beginTimeLong = null;
Long endTimeLong = null;

View File

@ -269,7 +269,7 @@ user-settings:
# 建议根据实际情况调整,过大可能会占用较多内存,过小可能会增加数据库查询压力,
alarm-catch-size: 10000
# 是否使用拉流的方式获取快照默认false避免流量大规模消耗开启后则使用拉流的方式获取快照
snap-by-pull-stream: true
alarm-snap-by-stream: true
# 是否使用拉流的方式获取快照默认false避免流量大规模消耗开启后则使用拉流的方式获取快照
allowed-alarm-type:
- IntrusionDetection