diff --git a/src/main/java/com/genersoft/iot/vmp/conf/UserSetting.java b/src/main/java/com/genersoft/iot/vmp/conf/UserSetting.java index f39cd8e60..adb733384 100644 --- a/src/main/java/com/genersoft/iot/vmp/conf/UserSetting.java +++ b/src/main/java/com/genersoft/iot/vmp/conf/UserSetting.java @@ -239,12 +239,16 @@ public class UserSetting { /** * 是否使用拉流的方式获取快照,默认false,避免流量大规模消耗,开启后则使用拉流的方式获取快照 */ - private boolean snapByPullStream = false; + private boolean alarmSnapByStream = false; /** * 报警订阅白名单,设置后只有在此列表中的上级平台才会接收报警订阅消息,默认不设置则不限制 */ private List allowedAlarmType = new ArrayList<>(); + /** + * 报警记录保留天数,超过此天数的报警记录将在每天凌晨自动清理,默认30天,设置为0则不自动清理 + */ + private int alarmKeepDays = 7; } diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/AlarmServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/AlarmServiceImpl.java index 8b41a9c6a..1397438ba 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/impl/AlarmServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/service/impl/AlarmServiceImpl.java @@ -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, String beginTime, String endTime) { Long beginTimeLong = null; Long endTimeLong = null; diff --git a/src/main/resources/配置详情.yml b/src/main/resources/配置详情.yml index 6cb938536..29461d186 100644 --- a/src/main/resources/配置详情.yml +++ b/src/main/resources/配置详情.yml @@ -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