From f20eed30bf7415d4bf761a297107c758da6ac18a Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Thu, 11 Apr 2024 21:02:21 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=94=A8=E4=BE=8B=E9=9B=86=E5=90=88=E6=8A=A5?= =?UTF-8?q?=E5=91=8A=E5=A4=B1=E8=B4=A5=E5=81=9C=E6=AD=A2=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=9A=84=E7=BC=BA=E9=99=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/listener/MessageListener.java | 4 +- .../scenario/ApiScenarioBatchRunService.java | 43 ++++++++++++++----- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/listener/MessageListener.java b/backend/services/api-test/src/main/java/io/metersphere/api/listener/MessageListener.java index 3ca388efae..5eb9fcdc0b 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/listener/MessageListener.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/listener/MessageListener.java @@ -112,7 +112,9 @@ public class MessageListener { if (BooleanUtils.isTrue(queue.getRunModeConfig().getStopOnFailure()) && StringUtils.equals(dto.getReportStatus(), ApiReportStatus.ERROR.name())) { String reportId = queue.getRunModeConfig().isIntegratedReport() ? queue.getRunModeConfig().getCollectionReport().getReportId() : dto.getReportId(); if (resourceType.equals(ApiExecuteResourceType.API_SCENARIO)) { - apiScenarioBatchRunService.UpdateStopOnFailureReport(queue); + apiScenarioBatchRunService.updateStopOnFailureReport(queue); + } else { + apiScenarioBatchRunService.updateStopOnFailureApiReport(queue); } switch (resourceType) { case API_CASE -> apiReportService.updateReportStatus(reportId, ApiReportStatus.ERROR.name()); diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioBatchRunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioBatchRunService.java index cf34e55b0e..9362574e63 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioBatchRunService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioBatchRunService.java @@ -1,9 +1,6 @@ package io.metersphere.api.service.scenario; -import io.metersphere.api.domain.ApiScenario; -import io.metersphere.api.domain.ApiScenarioRecord; -import io.metersphere.api.domain.ApiScenarioReport; -import io.metersphere.api.domain.ApiScenarioReportStep; +import io.metersphere.api.domain.*; import io.metersphere.api.dto.ApiScenarioParamConfig; import io.metersphere.api.dto.ApiScenarioParseTmpParam; import io.metersphere.api.dto.debug.ApiResourceRunRequest; @@ -12,8 +9,7 @@ import io.metersphere.api.dto.scenario.ApiScenarioBatchRunRequest; import io.metersphere.api.dto.scenario.ApiScenarioDetail; import io.metersphere.api.dto.scenario.ApiScenarioParseParam; import io.metersphere.api.dto.scenario.ApiScenarioStepDTO; -import io.metersphere.api.mapper.ApiScenarioReportMapper; -import io.metersphere.api.mapper.ExtApiScenarioMapper; +import io.metersphere.api.mapper.*; import io.metersphere.api.service.ApiBatchRunBaseService; import io.metersphere.api.service.ApiExecuteService; import io.metersphere.api.service.queue.ApiExecutionQueueService; @@ -64,6 +60,12 @@ public class ApiScenarioBatchRunService { private ApiBatchRunBaseService apiBatchRunBaseService; @Resource private ExtApiScenarioMapper extApiScenarioMapper; + @Resource + private ApiReportMapper apiReportMapper; + @Resource + private ApiReportDetailMapper apiReportDetailMapper; + @Resource + private ApiReportStepMapper apiReportStepMapper; /** * 异步批量执行 @@ -420,7 +422,7 @@ public class ApiScenarioBatchRunService { return StringUtils.isBlank(runModeConfig.getEnvironmentId()) ? apiScenario.getGrouped() : runModeConfig.getGrouped(); } - public void UpdateStopOnFailureReport(ExecutionQueue queue) { + public void updateStopOnFailureReport(ExecutionQueue queue) { ApiRunModeConfigDTO runModeConfig = queue.getRunModeConfig(); try { ExecutionQueueDetail queueDetail = apiExecutionQueueService.getNextDetail(queue.getQueueId()); @@ -452,16 +454,37 @@ public class ApiScenarioBatchRunService { Long pendingCount = requestCount + report.getPendingCount(); report.setPendingCount(pendingCount); // 计算各种通过率 - report = computeRequestRate(report); + long total = apiScenarioReportService.getRequestTotal(report); + report = computeRequestRate(report, total); apiScenarioReportMapper.updateByPrimaryKeySelective(report); } } catch (Exception e) { LogUtils.error("失败停止,补充报告步骤失败:", e); } } + public void updateStopOnFailureApiReport(ExecutionQueue queue) { + ApiRunModeConfigDTO runModeConfig = queue.getRunModeConfig(); + if (runModeConfig.isIntegratedReport()) { + // 获取未执行的请求数,更新统计指标 + String reportId = runModeConfig.getCollectionReport().getReportId(); + ApiReport report = apiReportMapper.selectByPrimaryKey(reportId); + ApiReportDetailExample example = new ApiReportDetailExample(); + example.createCriteria().andReportIdEqualTo(reportId); + ApiReportStepExample stepExample = new ApiReportStepExample(); + stepExample.createCriteria().andReportIdEqualTo(reportId); + long total = apiReportStepMapper.countByExample(stepExample); + long pendCount = total - apiReportDetailMapper.countByExample(example); + report.setPendingCount(pendCount); + ApiScenarioReport apiScenarioReport = new ApiScenarioReport(); + BeanUtils.copyBean(apiScenarioReport, report); + apiScenarioReport = computeRequestRate(apiScenarioReport, total); + BeanUtils.copyBean(report, apiScenarioReport); + apiReportMapper.updateByPrimaryKeySelective(report); + } - public ApiScenarioReport computeRequestRate(ApiScenarioReport report) { - long total = apiScenarioReportService.getRequestTotal(report); + } + + public ApiScenarioReport computeRequestRate(ApiScenarioReport report , long total) { // 计算各个概率 double successRate = calculateRate(report.getSuccessCount(), total); double errorRate = calculateRate(report.getErrorCount(), total);