refactor(测试跟踪): 测试跟踪的自动清理报告数据采用批量处理,同时增加服务启动项,针对之前没有删除成功的资源报告进行删除处理

--bug=1034149 --user=宋天阳 [测试跟踪]github#27831 &28186S占用空间太多 https://www.tapd.cn/55049933/s/1451873
This commit is contained in:
song-tianyang 2024-01-16 13:58:35 +08:00 committed by 刘瑞斌
parent d454efb85f
commit d3e16991d8
4 changed files with 50 additions and 90 deletions

View File

@ -60,4 +60,10 @@ public interface ExtTestPlanMapper {
long countExecutingReportCount(String testPlanId); long countExecutingReportCount(String testPlanId);
void batchUpdateNode(@Param("request") TestPlanBatchMoveRequest request); void batchUpdateNode(@Param("request") TestPlanBatchMoveRequest request);
List<String> selectDeletedTestPlanReportIdsFromApiCaseReport();
List<String> selectDeletedTestPlanReportIdsFromApiScenarioReport();
List<String> selectDeletedTestPlanReportIdsFromUiReport();
} }

View File

@ -490,6 +490,21 @@
WHERE test_plan_id = #{0} WHERE test_plan_id = #{0}
AND status = 'RUNNING' AND status = 'RUNNING'
</select> </select>
<select id="selectDeletedTestPlanReportIdsFromApiCaseReport" resultType="java.lang.String">
SELECT DISTINCT relevance_test_plan_report_id
FROM api_definition_exec_result
WHERE relevance_test_plan_report_id not in (select id FROM test_plan_report);
</select>
<select id="selectDeletedTestPlanReportIdsFromApiScenarioReport" resultType="java.lang.String">
SELECT DISTINCT relevance_test_plan_report_id
FROM api_scenario_report
WHERE relevance_test_plan_report_id not in (select id FROM test_plan_report);
</select>
<select id="selectDeletedTestPlanReportIdsFromUiReport" resultType="java.lang.String">
SELECT DISTINCT relevance_test_plan_report_id
FROM ui_scenario_report
WHERE relevance_test_plan_report_id not in (select id FROM test_plan_report);
</select>
<update id="batchUpdateNode" parameterType="io.metersphere.plan.request.TestPlanBatchMoveRequest"> <update id="batchUpdateNode" parameterType="io.metersphere.plan.request.TestPlanBatchMoveRequest">
update test_plan set node_id = #{request.nodeId}, node_path = #{request.nodePath} update test_plan set node_id = #{request.nodeId}, node_path = #{request.nodePath}

View File

@ -3,14 +3,14 @@ package io.metersphere.listener;
import io.metersphere.commons.constants.ScheduleGroup; import io.metersphere.commons.constants.ScheduleGroup;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.RunInterface; import io.metersphere.commons.utils.RunInterface;
import io.metersphere.plan.service.TestPlanReportService;
import io.metersphere.service.*; import io.metersphere.service.*;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner; import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import jakarta.annotation.Resource;
@Component @Component
public class InitListener implements ApplicationRunner { public class InitListener implements ApplicationRunner {
@ -28,12 +28,14 @@ public class InitListener implements ApplicationRunner {
private PlatformPluginService platformPluginService; private PlatformPluginService platformPluginService;
@Resource @Resource
private BaseScheduleService baseScheduleService; private BaseScheduleService baseScheduleService;
@Resource
private TestPlanReportService testPlanReportService;
@Override @Override
public void run(ApplicationArguments applicationArguments) throws Exception { public void run(ApplicationArguments applicationArguments) throws Exception {
this.initOnceOperate(); this.initOnceOperate();
platformPluginService.loadPlatFormPlugins(); platformPluginService.loadPlatFormPlugins();
testPlanReportService.deleteIllegalityResourceReport();
baseScheduleService.startEnableSchedules(ScheduleGroup.ISSUE_SYNC); baseScheduleService.startEnableSchedules(ScheduleGroup.ISSUE_SYNC);
baseScheduleService.startEnableSchedules(ScheduleGroup.TEST_PLAN_TEST); baseScheduleService.startEnableSchedules(ScheduleGroup.TEST_PLAN_TEST);
} }

View File

@ -44,10 +44,7 @@ import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -957,93 +954,23 @@ public class TestPlanReportService {
} }
private void deleteReportBatch(List<String> reportIds) { private void deleteReportBatch(List<String> reportIds) {
int handleCount = 5000;
List<String> handleIdList;
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
TestPlanReportMapper planReportMapper = sqlSession.getMapper(TestPlanReportMapper.class);
TestPlanReportDataMapper planReportDataMapper = sqlSession.getMapper(TestPlanReportDataMapper.class);
TestPlanReportContentMapper planReportContentMapper = sqlSession.getMapper(TestPlanReportContentMapper.class);
try {
while (reportIds.size() > handleCount) {
handleIdList = new ArrayList<>(handleCount);
List<String> otherIdList = new ArrayList<>();
for (int index = 0; index < reportIds.size(); index++) {
if (index < handleCount) {
handleIdList.add(reportIds.get(index));
} else {
otherIdList.add(reportIds.get(index));
}
}
TestPlanReportExample deleteReportExample = new TestPlanReportExample();
deleteReportExample.createCriteria().andIdIn(handleIdList);
planReportMapper.deleteByExample(deleteReportExample);
TestPlanReportDataExample example = new TestPlanReportDataExample();
example.createCriteria().andTestPlanReportIdIn(handleIdList);
planReportDataMapper.deleteByExample(example);
TestPlanReportContentExample contentExample = new TestPlanReportContentExample();
contentExample.createCriteria().andTestPlanReportIdIn(handleIdList);
planReportContentMapper.deleteByExample(contentExample);
// //删除关联的接口用例报告
// ApiDefinitionExecResultExample apiDefinitionExecResultExample = new ApiDefinitionExecResultExample();
// apiDefinitionExecResultExample.createCriteria().andRelevanceTestPlanReportIdIn(handleIdList);
// batchDefinitionExecResultMapper.deleteByExample(apiDefinitionExecResultExample);
//
// //删除关联的场景和ui用例报告
// ApiScenarioReportExample apiScenarioReportExample = new ApiScenarioReportExample();
// apiScenarioReportExample.createCriteria().andRelevanceTestPlanReportIdIn(handleIdList);
// batchScenarioReportMapper.deleteByExample(apiScenarioReportExample);
//
// //删除关联的性能测试用例报告
// LoadTestReportExample loadTestReportExample = new LoadTestReportExample();
// loadTestReportExample.createCriteria().andRelevanceTestPlanReportIdIn(handleIdList);
// batchLoadTestReportMapper.deleteByExample(loadTestReportExample);
sqlSession.flushStatements();
reportIds = otherIdList;
}
if (!reportIds.isEmpty()) { if (!reportIds.isEmpty()) {
TestPlanReportExample deleteReportExample = new TestPlanReportExample(); TestPlanReportExample deleteReportExample = new TestPlanReportExample();
deleteReportExample.createCriteria().andIdIn(reportIds); deleteReportExample.createCriteria().andIdIn(reportIds);
planReportMapper.deleteByExample(deleteReportExample); testPlanReportMapper.deleteByExample(deleteReportExample);
TestPlanReportDataExample example = new TestPlanReportDataExample(); TestPlanReportDataExample example = new TestPlanReportDataExample();
example.createCriteria().andTestPlanReportIdIn(reportIds); example.createCriteria().andTestPlanReportIdIn(reportIds);
planReportDataMapper.deleteByExample(example); testPlanReportDataMapper.deleteByExample(example);
TestPlanReportContentExample contentExample = new TestPlanReportContentExample(); TestPlanReportContentExample contentExample = new TestPlanReportContentExample();
contentExample.createCriteria().andTestPlanReportIdIn(reportIds); contentExample.createCriteria().andTestPlanReportIdIn(reportIds);
planReportContentMapper.deleteByExample(contentExample); testPlanReportContentMapper.deleteByExample(contentExample);
// //删除关联的接口用例报告 this.deleteApiCaseReportByTestPlanExecute(reportIds);
// ApiDefinitionExecResultExample apiDefinitionExecResultExample = new ApiDefinitionExecResultExample(); this.deleteScenarioReportByTestPlanExecute(reportIds);
// apiDefinitionExecResultExample.createCriteria().andRelevanceTestPlanReportIdIn(reportIds); this.deleteUiReportByTestPlanExecute(reportIds);
// batchDefinitionExecResultMapper.deleteByExample(apiDefinitionExecResultExample);
//
// //删除关联的场景和ui用例报告
// ApiScenarioReportExample apiScenarioReportExample = new ApiScenarioReportExample();
// apiScenarioReportExample.createCriteria().andRelevanceTestPlanReportIdIn(reportIds);
// batchScenarioReportMapper.deleteByExample(apiScenarioReportExample);
//
// //删除关联的性能测试用例报告
// LoadTestReportExample loadTestReportExample = new LoadTestReportExample();
// loadTestReportExample.createCriteria().andRelevanceTestPlanReportIdIn(reportIds);
// batchLoadTestReportMapper.deleteByExample(loadTestReportExample);
sqlSession.flushStatements();
}
} finally {
if (sqlSession != null && sqlSessionFactory != null) {
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
}
} }
} }
@ -1615,7 +1542,7 @@ public class TestPlanReportService {
List<TestPlanReport> testPlanReports = testPlanReportMapper.selectByExample(example); List<TestPlanReport> testPlanReports = testPlanReportMapper.selectByExample(example);
List<String> ids = testPlanReports.stream().map(TestPlanReport::getId).collect(Collectors.toList()); List<String> ids = testPlanReports.stream().map(TestPlanReport::getId).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(ids)) { if (CollectionUtils.isNotEmpty(ids)) {
deleteReportBatch(ids); BatchProcessingUtil.consumerByStringList(ids, this::deleteReportBatch);
} }
} }
} }
@ -1817,4 +1744,14 @@ public class TestPlanReportService {
public String selectLastReportByTestPlanId(String testPlanId) { public String selectLastReportByTestPlanId(String testPlanId) {
return extTestPlanReportMapper.selectLastReportByTestPlanId(testPlanId); return extTestPlanReportMapper.selectLastReportByTestPlanId(testPlanId);
} }
@Async
public void deleteIllegalityResourceReport() {
List<String> deletedTestPlanReportIds = new ArrayList<>();
deletedTestPlanReportIds.addAll(extTestPlanMapper.selectDeletedTestPlanReportIdsFromApiCaseReport());
deletedTestPlanReportIds.addAll(extTestPlanMapper.selectDeletedTestPlanReportIdsFromApiScenarioReport());
deletedTestPlanReportIds.addAll(extTestPlanMapper.selectDeletedTestPlanReportIdsFromUiReport());
deletedTestPlanReportIds = deletedTestPlanReportIds.stream().distinct().toList();
BatchProcessingUtil.consumerByStringList(deletedTestPlanReportIds, this::deleteReportBatch);
}
} }