feat(iot): 添加设备日志定时清理功能
- 在DeviceMapper中新增批量删除设备、事件、功能日志的方法 - 在DeviceMapper.xml中添加对应的SQL删除语句 - 在DeviceServiceImpl中实现批量删除日志逻辑和定时清理方法 - 创建DevLogDeleteJob定时任务类执行日志清理 - 在IDeviceService接口中添加devLogDelete方法定义 - 实现按天数阈值分批删除历史日志数据 - 添加日志清理进度监控和统计功能 Signed-off-by: Gjm <你的邮箱>
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package iot.lidee.iot.data.job;
|
||||
|
||||
import iot.lidee.iot.service.IDeviceService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 定时删除设备日志
|
||||
* @author gsb
|
||||
* @date 2024/4/11 10:33
|
||||
*/
|
||||
@Component("DevLogDeleteJob")
|
||||
@Slf4j
|
||||
public class DevLogDeleteJob {
|
||||
@Resource
|
||||
private IDeviceService deviceService;
|
||||
|
||||
/**
|
||||
* 定期删除设备日志
|
||||
*/
|
||||
public void devLogDelete() {
|
||||
log.info("------------------[定时删除设备日志]---------------------");
|
||||
deviceService.devLogDelete();
|
||||
log.info("------------------[删除设备日志完成]---------------------");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import iot.lidee.iot.model.ThingsModels.ThingsModelValuesOutput;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -381,4 +382,10 @@ public interface DeviceMapper extends BaseMapperX<Device>
|
||||
*/
|
||||
public List<String> selectSerialNumbersByProductId(@Param("productId") Long productId);
|
||||
|
||||
int deleteDeviceLogBatch(@Param("cutoffDate") Date cutoffDate, @Param("batchSize") int batchSize);
|
||||
|
||||
int deleteEventLogBatch(@Param("cutoffDate") Date cutoffDate, @Param("batchSize") int batchSize);
|
||||
|
||||
int deleteFunctionLogBatch(@Param("cutoffDate") Date cutoffDate, @Param("batchSize") int batchSize);
|
||||
|
||||
}
|
||||
|
||||
@@ -362,4 +362,5 @@ public interface IDeviceService
|
||||
|
||||
public int updateDeviceBySerialNumber(Device device) ;
|
||||
|
||||
void devLogDelete();
|
||||
}
|
||||
|
||||
@@ -1847,5 +1847,99 @@ public class DeviceServiceImpl implements IDeviceService {
|
||||
public List<String> selectSerialNumbersByProductId(Long productId) {
|
||||
return deviceMapper.selectSerialNumbersByProductId(productId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void devLogDelete() {
|
||||
batchDeleteLogsBeforeDays(15);
|
||||
}
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void batchDeleteLogsBeforeDays(int days) {
|
||||
Date cutoffDate = DateUtils.addDays(new Date(), -days);
|
||||
|
||||
log.info("开始删除{}天前的日志数据,截止时间:{}", days, cutoffDate);
|
||||
|
||||
long totalDeleted = 0;
|
||||
|
||||
long deviceLogDeleted = deleteDeviceLogBatch(cutoffDate);
|
||||
totalDeleted += deviceLogDeleted;
|
||||
log.info("删除设备日志(iot_device_log)完成,共删除:{}条", deviceLogDeleted);
|
||||
|
||||
long eventLogDeleted = deleteEventLogBatch(cutoffDate);
|
||||
totalDeleted += eventLogDeleted;
|
||||
log.info("删除事件日志(iot_event_log)完成,共删除:{}条", eventLogDeleted);
|
||||
|
||||
long functionLogDeleted = deleteFunctionLogBatch(cutoffDate);
|
||||
totalDeleted += functionLogDeleted;
|
||||
log.info("删除功能日志(iot_function_log)完成,共删除:{}条", functionLogDeleted);
|
||||
|
||||
log.info("日志清理完成,总计删除:{}条记录", totalDeleted);
|
||||
}
|
||||
|
||||
private long deleteDeviceLogBatch(Date cutoffDate) {
|
||||
long totalDeleted = 0;
|
||||
int batchSize = 5000;
|
||||
|
||||
while (true) {
|
||||
int deleted = deviceMapper.deleteDeviceLogBatch(cutoffDate, batchSize);
|
||||
totalDeleted += deleted;
|
||||
|
||||
if (deleted == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return totalDeleted;
|
||||
}
|
||||
|
||||
private long deleteEventLogBatch(Date cutoffDate) {
|
||||
long totalDeleted = 0;
|
||||
int batchSize = 5000;
|
||||
|
||||
while (true) {
|
||||
int deleted = deviceMapper.deleteEventLogBatch(cutoffDate, batchSize);
|
||||
totalDeleted += deleted;
|
||||
|
||||
if (deleted == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return totalDeleted;
|
||||
}
|
||||
|
||||
private long deleteFunctionLogBatch(Date cutoffDate) {
|
||||
long totalDeleted = 0;
|
||||
int batchSize = 5000;
|
||||
|
||||
while (true) {
|
||||
int deleted = deviceMapper.deleteFunctionLogBatch(cutoffDate, batchSize);
|
||||
totalDeleted += deleted;
|
||||
|
||||
if (deleted == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return totalDeleted;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -968,4 +968,22 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
where d.product_id = #{productId,jdbcType=BIGINT}
|
||||
</select>
|
||||
|
||||
|
||||
<delete id="deleteDeviceLogBatch">
|
||||
DELETE FROM iot_device_log
|
||||
WHERE create_time < #{cutoffDate}
|
||||
LIMIT #{batchSize}
|
||||
</delete>
|
||||
|
||||
<delete id="deleteEventLogBatch">
|
||||
DELETE FROM iot_event log
|
||||
WHERE create_time < #{cutoffDate}
|
||||
LIMIT #{batchSize}
|
||||
</delete>
|
||||
|
||||
<delete id="deleteFunctionLogBatch">
|
||||
DELETE FROM iot_alert_log
|
||||
WHERE create_time < #{cutoffDate}
|
||||
LIMIT #{batchSize}
|
||||
</delete>
|
||||
</mapper>
|
||||
|
||||
Reference in New Issue
Block a user