refactor(测试计划): 优化变更历史删除逻辑
This commit is contained in:
parent
6b19f3a297
commit
f5a0939f42
|
@ -6,6 +6,7 @@ import io.metersphere.sdk.constants.ParamConstants;
|
||||||
import io.metersphere.sdk.domain.OperationLogBlobExample;
|
import io.metersphere.sdk.domain.OperationLogBlobExample;
|
||||||
import io.metersphere.sdk.mapper.OperationLogBlobMapper;
|
import io.metersphere.sdk.mapper.OperationLogBlobMapper;
|
||||||
import io.metersphere.sdk.util.LogUtils;
|
import io.metersphere.sdk.util.LogUtils;
|
||||||
|
import io.metersphere.sdk.util.SubListUtils;
|
||||||
import io.metersphere.system.domain.SystemParameter;
|
import io.metersphere.system.domain.SystemParameter;
|
||||||
import io.metersphere.system.mapper.BaseOperationHistoryMapper;
|
import io.metersphere.system.mapper.BaseOperationHistoryMapper;
|
||||||
import io.metersphere.system.mapper.BaseOperationLogMapper;
|
import io.metersphere.system.mapper.BaseOperationLogMapper;
|
||||||
|
@ -14,7 +15,6 @@ import jakarta.annotation.Resource;
|
||||||
import org.apache.commons.collections.CollectionUtils;
|
import org.apache.commons.collections.CollectionUtils;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@ -58,29 +58,21 @@ public class CleanHistoryJob {
|
||||||
private void doCleanupHistory(int limit) {
|
private void doCleanupHistory(int limit) {
|
||||||
try {
|
try {
|
||||||
//变更历史处理
|
//变更历史处理
|
||||||
List<String> sourceIds = baseOperationHistoryMapper.selectSourceIds();
|
List<String> sourceIds = baseOperationHistoryMapper.selectSourceIds(limit);
|
||||||
int size = 100;
|
SubListUtils.dealForSubList(sourceIds, 100, subList -> {
|
||||||
List<List<String>> batchList = splitList(sourceIds, size);
|
cleanupHistory(subList, limit);
|
||||||
batchList.forEach(batch -> cleanupHistory(batch, limit));
|
});
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LogUtils.error(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) {
|
public void cleanupHistory(List<String> batch, int limit) {
|
||||||
batch.forEach(sourceId -> {
|
batch.forEach(sourceId -> {
|
||||||
List<Long> ids = baseOperationHistoryMapper.selectIdsBySourceId(sourceId, limit);
|
List<Long> ids = baseOperationHistoryMapper.selectIdsBySourceId(sourceId, limit);
|
||||||
if (CollectionUtils.isNotEmpty(ids)) {
|
if (CollectionUtils.isNotEmpty(ids)) {
|
||||||
baseOperationHistoryMapper.deleteByIds(sourceId, ids);
|
baseOperationHistoryMapper.deleteBySourceId(sourceId, ids);
|
||||||
List<Long> logIds = baseOperationLogMapper.selectIdByHistoryIds(ids);
|
List<Long> logIds = baseOperationLogMapper.selectIdByHistoryIds(ids);
|
||||||
ids.removeAll(logIds);
|
ids.removeAll(logIds);
|
||||||
if (CollectionUtils.isNotEmpty(ids)) {
|
if (CollectionUtils.isNotEmpty(ids)) {
|
||||||
|
|
|
@ -9,11 +9,11 @@ import java.util.List;
|
||||||
|
|
||||||
public interface BaseOperationHistoryMapper {
|
public interface BaseOperationHistoryMapper {
|
||||||
|
|
||||||
List<String> selectSourceIds();
|
List<String> selectSourceIds(@Param("limit") int limit);
|
||||||
|
|
||||||
List<Long> selectIdsBySourceId(@Param("sourceId") String sourceId, @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);
|
List<OperationHistoryDTO> list(@Param("request") OperationHistoryRequest request);
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
|
|
||||||
<select id="selectSourceIds" resultType="java.lang.String">
|
<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>
|
</select>
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@
|
||||||
select id from operation_history where source_id = #{sourceId} ORDER BY create_time DESC LIMIT #{limit}
|
select id from operation_history where source_id = #{sourceId} ORDER BY create_time DESC LIMIT #{limit}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<delete id="deleteByIds">
|
<delete id="deleteBySourceId">
|
||||||
DELETE FROM operation_history WHERE source_id = #{sourceId} and id not in
|
DELETE FROM operation_history WHERE source_id = #{sourceId} and id not in
|
||||||
<foreach collection="ids" item="id" separator="," open="(" close=")">
|
<foreach collection="ids" item="id" separator="," open="(" close=")">
|
||||||
#{id}
|
#{id}
|
||||||
|
|
Loading…
Reference in New Issue