perf: 优化日志保留与分页查询

- 降低普通只读请求的日志写入并保留敏感 GET 审计

- 增加数据库分批清理、时间索引和文件滚动容量上限

- 增加近 30 天筛选、稳定倒序和分页大小限制
This commit is contained in:
2026-07-30 14:21:36 +08:00
parent c78074a969
commit 03f45212ef
21 changed files with 732 additions and 19 deletions

View File

@@ -0,0 +1,67 @@
package tech.easyflow.system.schedule;
import org.junit.Test;
import tech.easyflow.log.LogRecordProperties;
import tech.easyflow.system.mapper.SysLogMapper;
import java.util.Date;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* {@link SysLogCleanupJob} 分批清理行为测试。
*/
public class SysLogCleanupJobTest {
/**
* 验证清理任务在最后一个不满批次后停止。
*/
@Test
public void cleanupShouldStopAfterPartialBatch() {
SysLogMapper mapper = mock(SysLogMapper.class);
LogRecordProperties properties = properties(5_000, 10);
Date cutoff = new Date();
when(mapper.deleteExpiredBatch(cutoff, 5_000))
.thenReturn(5_000)
.thenReturn(1_200);
SysLogCleanupJob job = new SysLogCleanupJob(mapper, properties);
assertEquals(6_200, job.cleanupExpired(cutoff));
verify(mapper, times(2)).deleteExpiredBatch(cutoff, 5_000);
}
/**
* 验证清理任务遵守单次调度最大批次数。
*/
@Test
public void cleanupShouldRespectMaxBatches() {
SysLogMapper mapper = mock(SysLogMapper.class);
LogRecordProperties properties = properties(2_000, 3);
Date cutoff = new Date();
when(mapper.deleteExpiredBatch(cutoff, 2_000)).thenReturn(2_000);
SysLogCleanupJob job = new SysLogCleanupJob(mapper, properties);
assertEquals(6_000, job.cleanupExpired(cutoff));
verify(mapper, times(3)).deleteExpiredBatch(cutoff, 2_000);
}
/**
* 创建测试日志配置。
*
* @param batchSize 单批行数
* @param maxBatches 最大批次数
* @return 日志配置
*/
private LogRecordProperties properties(int batchSize, int maxBatches) {
LogRecordProperties properties = new LogRecordProperties();
properties.getRetention().setBatchSize(batchSize);
properties.getRetention().setMaxBatches(maxBatches);
return properties;
}
}