From 47ad7a1c2ae47a86a3c33a2c685a93d73a25a837 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Thu, 11 May 2023 15:38:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=B5=8B=E8=AF=95=E8=AE=A1=E5=88=92):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E6=B5=8B=E8=AF=95=E8=AE=A1=E5=88=92?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E5=AE=8C=E6=88=90=E4=B9=8B=E5=90=8E=E7=9A=84?= =?UTF-8?q?=E5=9B=9E=E8=B0=83=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化测试计划执行完成之后的回调函数 --- .../plan/service/TestPlanMessageService.java | 57 +++---------------- .../plan/service/TestPlanReportService.java | 20 +++---- .../plan/service/TestPlanService.java | 55 +++++++++++++++--- 3 files changed, 64 insertions(+), 68 deletions(-) diff --git a/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanMessageService.java b/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanMessageService.java index 310c545ce6..0c97b0a974 100644 --- a/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanMessageService.java +++ b/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanMessageService.java @@ -1,6 +1,9 @@ package io.metersphere.plan.service; -import io.metersphere.base.domain.*; +import io.metersphere.base.domain.ShareInfo; +import io.metersphere.base.domain.TestPlan; +import io.metersphere.base.domain.TestPlanReport; +import io.metersphere.base.domain.TestPlanReportContentWithBLOBs; import io.metersphere.base.mapper.TestPlanMapper; import io.metersphere.commons.constants.*; import io.metersphere.commons.utils.BeanUtils; @@ -24,7 +27,10 @@ import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.*; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.stream.Collectors; @Service @@ -45,25 +51,14 @@ public class TestPlanMessageService { @Resource private TestPlanReportService testPlanReportService; - public static final Integer FULL_MARKS = 100; - - @Async - public void checkTestPlanStatusAndSendMessage(TestPlanReport report, TestPlanReportContentWithBLOBs testPlanReportContent, boolean sendMessage) { + public void checkTestPlanStatusAndSendMessage(TestPlanReport report, TestPlanReportContentWithBLOBs testPlanReportContent, TestPlan testPlan, boolean sendMessage) { if (report != null && testPlanReportContent != null) { // 异步发送通知需要指定调用其他服务的user HttpHeaderUtils.runAsUser(report.getCreator()); try { report = testPlanReportService.checkTestPlanReportHasErrorCase(report, testPlanReportContent); if (!report.getIsApiCaseExecuting() && !report.getIsPerformanceExecuting() && !report.getIsScenarioExecuting() && !report.getIsUiScenarioExecuting()) { - //更新TestPlan状态为完成 - TestPlanWithBLOBs testPlan = testPlanMapper.selectByPrimaryKey(report.getTestPlanId()); - if (testPlan != null - && !StringUtils.equalsAny(testPlan.getStatus(), TestPlanStatus.Completed.name(), TestPlanStatus.Finished.name())) { - - testPlan.setStatus(calcTestPlanStatusWithPassRate(testPlan)); - testPlanService.editTestPlan(testPlan); - } if (sendMessage && testPlan != null && StringUtils.equalsAny(report.getTriggerMode(), ReportTriggerMode.MANUAL.name(), ReportTriggerMode.API.name(), @@ -82,40 +77,6 @@ public class TestPlanMessageService { } } - private String calcTestPlanStatusWithPassRate(TestPlanWithBLOBs testPlan) { - try { - // 计算通过率 - TestPlanDTOWithMetric testPlanDTOWithMetric = BeanUtils.copyBean(new TestPlanDTOWithMetric(), testPlan); - testPlanService.calcTestPlanRate(Collections.singletonList(testPlanDTOWithMetric)); - //测试进度 - Double testRate = Optional.ofNullable(testPlanDTOWithMetric.getTestRate()).orElse(0.0); - //通过率 - Double passRate = Optional.ofNullable(testPlanDTOWithMetric.getPassRate()).orElse(0.0); - - // 已完成:测试进度=100% 且 通过率=100% - if (testRate >= FULL_MARKS && passRate >= FULL_MARKS) { - return TestPlanStatus.Completed.name(); - } - - // 已结束:超过了计划结束时间(如有) 或 测试进度=100% 且 通过率非100% - Long plannedEndTime = testPlan.getPlannedEndTime(); - long currentTime = System.currentTimeMillis(); - if (Objects.nonNull(plannedEndTime) && currentTime >= plannedEndTime) { - return TestPlanStatus.Finished.name(); - } - - if (testRate >= FULL_MARKS && passRate < FULL_MARKS) { - return TestPlanStatus.Finished.name(); - } - - } catch (Exception e) { - LogUtil.error("计算通过率失败!", e); - } - - // 进行中:0 < 测试进度 < 100% - return TestPlanStatus.Underway.name(); - } - private void sendMessage(TestPlan testPlan, TestPlanReport testPlanReport, String projectId) { assert testPlan != null; SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class); 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 aed2e40331..0975b03236 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 @@ -586,20 +586,25 @@ public class TestPlanReportService { isSendMessage = true; } TestPlanReportContentWithBLOBs content = null; + TestPlanWithBLOBs testPlanWithBLOBs = null; try { HttpHeaderUtils.runAsUser(testPlanReport.getCreator()); boolean isRerunningTestPlan = BooleanUtils.isTrue(StringUtils.equalsIgnoreCase(testPlanReport.getStatus(), APITestStatus.Rerunning.name())); //测试计划报告结果数据初始化 testPlanReport.setStatus(finishStatus); + //统计并保存报告 content = this.countAndSaveTestPlanReport(testPlanReport, isRerunningTestPlan); - this.setReportExecuteResult(testPlanReport, finishStatus); + //更新测试计划的执行相关信息(状态、执行率等) + testPlanWithBLOBs = testPlanService.selectAndChangeTestPlanExecuteInfo(testPlanReport.getTestPlanId()); } catch (Exception e) { testPlanReport.setStatus(finishStatus); LogUtil.error("统计测试计划状态失败!", e); } finally { HttpHeaderUtils.clearUser(); testPlanReportMapper.updateByPrimaryKey(testPlanReport); - testPlanMessageService.checkTestPlanStatusAndSendMessage(testPlanReport, content, isSendMessage); + if (testPlanWithBLOBs != null) { + testPlanMessageService.checkTestPlanStatusAndSendMessage(testPlanReport, content, testPlanWithBLOBs, isSendMessage); + } this.executeTestPlanByQueue(testPlanReportId); } } @@ -617,17 +622,6 @@ public class TestPlanReportService { } } - /** - * 测试计划报告设置最终执行状态 - */ - private void setReportExecuteResult(TestPlanReport testPlanReport, String finishStatus) { - //计算测试计划状态 - testPlanReport.setStatus(StringUtils.equalsIgnoreCase(finishStatus, TestPlanReportStatus.COMPLETED.name()) ? - TestPlanReportStatus.SUCCESS.name() : finishStatus); - //检查更新测试计划状态 - testPlanService.checkTestPlanStatusWhenExecuteOver(testPlanReport.getTestPlanId()); - } - /** * 统计测试计划报告信息 */ diff --git a/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanService.java b/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanService.java index f3aad7d7d6..8a30994e7f 100644 --- a/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanService.java +++ b/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanService.java @@ -293,13 +293,7 @@ public class TestPlanService { && res.getActualStartTime() == null) { testPlan.setActualStartTime(System.currentTimeMillis()); } - - if (testPlan.getName() == null) {// 若是点击该测试计划,则仅更新了updateTime,其它字段全为null,使用updateByPrimaryKeySelective - testPlanMapper.updateByPrimaryKeySelective(testPlan); - } else { // 有修改字段的调用,为保证将某些时间置null的情况,使用updateByPrimaryKey - baseScheduleService.updateNameByResourceID(testPlan.getId(), testPlan.getName());// 同步更新该测试的定时任务的name - testPlanMapper.updateByPrimaryKeyWithBLOBs(testPlan); // 更新 - } + testPlanMapper.updateByPrimaryKeyWithBLOBs(testPlan); return testPlanMapper.selectByPrimaryKey(testPlan.getId()); } @@ -2300,4 +2294,51 @@ public class TestPlanService { } return testPlanExecuteService.runTestPlan(testPlanId, projectId, userId, triggerMode, planReportId, executionWay, apiRunConfig); } + + public TestPlanWithBLOBs selectAndChangeTestPlanExecuteInfo(String testPlanId) { + //更新TestPlan状态为完成 + TestPlanWithBLOBs testPlan = testPlanMapper.selectByPrimaryKey(testPlanId); + if (testPlan != null + && !StringUtils.equalsAny(testPlan.getStatus(), TestPlanStatus.Completed.name(), TestPlanStatus.Finished.name())) { + testPlan.setStatus(calcTestPlanStatusWithPassRate(testPlan)); + this.editTestPlan(testPlan); + } + return testPlan; + } + + private String calcTestPlanStatusWithPassRate(TestPlanWithBLOBs testPlan) { + try { + int fullMarks = 100; + + // 计算通过率 + TestPlanDTOWithMetric testPlanDTOWithMetric = BeanUtils.copyBean(new TestPlanDTOWithMetric(), testPlan); + testPlanService.calcTestPlanRate(Collections.singletonList(testPlanDTOWithMetric)); + //测试进度 + Double testRate = Optional.ofNullable(testPlanDTOWithMetric.getTestRate()).orElse(0.0); + //通过率 + Double passRate = Optional.ofNullable(testPlanDTOWithMetric.getPassRate()).orElse(0.0); + + // 已完成:测试进度=100% 且 通过率=100% + if (testRate >= fullMarks && passRate >= fullMarks) { + return TestPlanStatus.Completed.name(); + } + + // 已结束:超过了计划结束时间(如有) 或 测试进度=100% 且 通过率非100% + Long plannedEndTime = testPlan.getPlannedEndTime(); + long currentTime = System.currentTimeMillis(); + if (Objects.nonNull(plannedEndTime) && currentTime >= plannedEndTime) { + return TestPlanStatus.Finished.name(); + } + + if (testRate >= fullMarks && passRate < fullMarks) { + return TestPlanStatus.Finished.name(); + } + + } catch (Exception e) { + LogUtil.error("计算通过率失败!", e); + } + + // 进行中:0 < 测试进度 < 100% + return TestPlanStatus.Underway.name(); + } }