From d3e16991d84c7c22b99a1d1bfbf10ba5dbcac541 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Tue, 16 Jan 2024 13:58:35 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA):?= =?UTF-8?q?=20=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA=E7=9A=84=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=B8=85=E7=90=86=E6=8A=A5=E5=91=8A=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E9=87=87=E7=94=A8=E6=89=B9=E9=87=8F=E5=A4=84=E7=90=86=EF=BC=8C?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E5=A2=9E=E5=8A=A0=E6=9C=8D=E5=8A=A1=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E9=A1=B9=EF=BC=8C=E9=92=88=E5=AF=B9=E4=B9=8B=E5=89=8D?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E5=88=A0=E9=99=A4=E6=88=90=E5=8A=9F=E7=9A=84?= =?UTF-8?q?=E8=B5=84=E6=BA=90=E6=8A=A5=E5=91=8A=E8=BF=9B=E8=A1=8C=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1034149 --user=宋天阳 [测试跟踪]github#27831 &28186S占用空间太多 https://www.tapd.cn/55049933/s/1451873 --- .../base/mapper/ext/ExtTestPlanMapper.java | 6 + .../base/mapper/ext/ExtTestPlanMapper.xml | 15 +++ .../io/metersphere/listener/InitListener.java | 8 +- .../plan/service/TestPlanReportService.java | 111 ++++-------------- 4 files changed, 50 insertions(+), 90 deletions(-) diff --git a/test-track/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanMapper.java b/test-track/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanMapper.java index 75baddb728..b5d49c02c7 100644 --- a/test-track/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanMapper.java +++ b/test-track/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanMapper.java @@ -60,4 +60,10 @@ public interface ExtTestPlanMapper { long countExecutingReportCount(String testPlanId); void batchUpdateNode(@Param("request") TestPlanBatchMoveRequest request); + + List selectDeletedTestPlanReportIdsFromApiCaseReport(); + + List selectDeletedTestPlanReportIdsFromApiScenarioReport(); + + List selectDeletedTestPlanReportIdsFromUiReport(); } diff --git a/test-track/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanMapper.xml b/test-track/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanMapper.xml index a76f928124..af769ad640 100644 --- a/test-track/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanMapper.xml +++ b/test-track/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanMapper.xml @@ -490,6 +490,21 @@ WHERE test_plan_id = #{0} AND status = 'RUNNING' + + + update test_plan set node_id = #{request.nodeId}, node_path = #{request.nodePath} diff --git a/test-track/backend/src/main/java/io/metersphere/listener/InitListener.java b/test-track/backend/src/main/java/io/metersphere/listener/InitListener.java index 60eb393640..50f77f2715 100644 --- a/test-track/backend/src/main/java/io/metersphere/listener/InitListener.java +++ b/test-track/backend/src/main/java/io/metersphere/listener/InitListener.java @@ -3,14 +3,14 @@ package io.metersphere.listener; import io.metersphere.commons.constants.ScheduleGroup; import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.RunInterface; +import io.metersphere.plan.service.TestPlanReportService; import io.metersphere.service.*; +import jakarta.annotation.Resource; import org.apache.commons.lang3.StringUtils; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; -import jakarta.annotation.Resource; - @Component public class InitListener implements ApplicationRunner { @@ -28,12 +28,14 @@ public class InitListener implements ApplicationRunner { private PlatformPluginService platformPluginService; @Resource private BaseScheduleService baseScheduleService; + @Resource + private TestPlanReportService testPlanReportService; @Override public void run(ApplicationArguments applicationArguments) throws Exception { this.initOnceOperate(); platformPluginService.loadPlatFormPlugins(); - + testPlanReportService.deleteIllegalityResourceReport(); baseScheduleService.startEnableSchedules(ScheduleGroup.ISSUE_SYNC); baseScheduleService.startEnableSchedules(ScheduleGroup.TEST_PLAN_TEST); } diff --git a/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanReportService.java b/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanReportService.java index 3b2fc1a3ca..197a5ab808 100644 --- a/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanReportService.java +++ b/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanReportService.java @@ -44,10 +44,7 @@ import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.ObjectUtils; 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.mybatis.spring.SqlSessionUtils; import org.springframework.context.annotation.Lazy; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @@ -957,93 +954,23 @@ public class TestPlanReportService { } private void deleteReportBatch(List reportIds) { - int handleCount = 5000; - List 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 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()) { - TestPlanReportExample deleteReportExample = new TestPlanReportExample(); - deleteReportExample.createCriteria().andIdIn(reportIds); - planReportMapper.deleteByExample(deleteReportExample); + if (!reportIds.isEmpty()) { + TestPlanReportExample deleteReportExample = new TestPlanReportExample(); + deleteReportExample.createCriteria().andIdIn(reportIds); + testPlanReportMapper.deleteByExample(deleteReportExample); - TestPlanReportDataExample example = new TestPlanReportDataExample(); - example.createCriteria().andTestPlanReportIdIn(reportIds); - planReportDataMapper.deleteByExample(example); + TestPlanReportDataExample example = new TestPlanReportDataExample(); + example.createCriteria().andTestPlanReportIdIn(reportIds); + testPlanReportDataMapper.deleteByExample(example); - TestPlanReportContentExample contentExample = new TestPlanReportContentExample(); - contentExample.createCriteria().andTestPlanReportIdIn(reportIds); - planReportContentMapper.deleteByExample(contentExample); + TestPlanReportContentExample contentExample = new TestPlanReportContentExample(); + contentExample.createCriteria().andTestPlanReportIdIn(reportIds); + testPlanReportContentMapper.deleteByExample(contentExample); - // //删除关联的接口用例报告 - // ApiDefinitionExecResultExample apiDefinitionExecResultExample = new ApiDefinitionExecResultExample(); - // apiDefinitionExecResultExample.createCriteria().andRelevanceTestPlanReportIdIn(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); - } + this.deleteApiCaseReportByTestPlanExecute(reportIds); + this.deleteScenarioReportByTestPlanExecute(reportIds); + this.deleteUiReportByTestPlanExecute(reportIds); } } @@ -1615,7 +1542,7 @@ public class TestPlanReportService { List testPlanReports = testPlanReportMapper.selectByExample(example); List ids = testPlanReports.stream().map(TestPlanReport::getId).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(ids)) { - deleteReportBatch(ids); + BatchProcessingUtil.consumerByStringList(ids, this::deleteReportBatch); } } } @@ -1817,4 +1744,14 @@ public class TestPlanReportService { public String selectLastReportByTestPlanId(String testPlanId) { return extTestPlanReportMapper.selectLastReportByTestPlanId(testPlanId); } + + @Async + public void deleteIllegalityResourceReport() { + List 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); + } }