refactor(测试计划): 优化变更历史删除逻辑

This commit is contained in:
WangXu10 2024-05-24 14:39:31 +08:00 committed by 刘瑞斌
parent 6b19f3a297
commit f5a0939f42
3 changed files with 10 additions and 18 deletions

View File

@ -6,6 +6,7 @@ import io.metersphere.sdk.constants.ParamConstants;
import io.metersphere.sdk.domain.OperationLogBlobExample;
import io.metersphere.sdk.mapper.OperationLogBlobMapper;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.sdk.util.SubListUtils;
import io.metersphere.system.domain.SystemParameter;
import io.metersphere.system.mapper.BaseOperationHistoryMapper;
import io.metersphere.system.mapper.BaseOperationLogMapper;
@ -14,7 +15,6 @@ import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@ -58,29 +58,21 @@ public class CleanHistoryJob {
private void doCleanupHistory(int limit) {
try {
//变更历史处理
List<String> sourceIds = baseOperationHistoryMapper.selectSourceIds();
int size = 100;
List<List<String>> batchList = splitList(sourceIds, size);
batchList.forEach(batch -> cleanupHistory(batch, limit));
List<String> sourceIds = baseOperationHistoryMapper.selectSourceIds(limit);
SubListUtils.dealForSubList(sourceIds, 100, subList -> {
cleanupHistory(subList, limit);
});
} catch (Exception e) {
LogUtils.error(e);
}
}
private List<List<String>> splitList(List<String> list, int size) {
List<List<String>> batchList = new ArrayList<>();
for (int i = 0; i < list.size(); i += size) {
batchList.add(list.subList(i, Math.min(i + size, list.size())));
}
return batchList;
}
public void cleanupHistory(List<String> batch, int limit) {
batch.forEach(sourceId -> {
List<Long> ids = baseOperationHistoryMapper.selectIdsBySourceId(sourceId, limit);
if (CollectionUtils.isNotEmpty(ids)) {
baseOperationHistoryMapper.deleteByIds(sourceId, ids);
baseOperationHistoryMapper.deleteBySourceId(sourceId, ids);
List<Long> logIds = baseOperationLogMapper.selectIdByHistoryIds(ids);
ids.removeAll(logIds);
if (CollectionUtils.isNotEmpty(ids)) {

View File

@ -9,11 +9,11 @@ import java.util.List;
public interface BaseOperationHistoryMapper {
List<String> selectSourceIds();
List<String> selectSourceIds(@Param("limit") int limit);
List<Long> selectIdsBySourceId(@Param("sourceId") String sourceId, @Param("limit") int limit);
void deleteByIds(@Param("sourceId") String sourceId, @Param("ids") List<Long> ids);
void deleteBySourceId(@Param("sourceId") String sourceId, @Param("ids") List<Long> ids);
List<OperationHistoryDTO> list(@Param("request") OperationHistoryRequest request);

View File

@ -4,7 +4,7 @@
<select id="selectSourceIds" resultType="java.lang.String">
select source_id from operation_history GROUP BY source_id
select source_id from operation_history GROUP BY source_id having count(source_id) > #{limit}
</select>
@ -12,7 +12,7 @@
select id from operation_history where source_id = #{sourceId} ORDER BY create_time DESC LIMIT #{limit}
</select>
<delete id="deleteByIds">
<delete id="deleteBySourceId">
DELETE FROM operation_history WHERE source_id = #{sourceId} and id not in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}