refactor: 测试计划报告方法优化 (#9175)

Co-authored-by: chenjianxing <jianxing.chen@fit2cloud.com>
Co-authored-by: jianxing <41557596+AgAngle@users.noreply.github.com>
This commit is contained in:
metersphere-bot 2022-01-10 18:55:56 +08:00 committed by GitHub
parent 44e4711644
commit 908dd5b734
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 109 additions and 18 deletions

View File

@ -18,6 +18,7 @@ import io.metersphere.dto.RequestResult;
import io.metersphere.dto.ResultDTO; import io.metersphere.dto.ResultDTO;
import io.metersphere.notice.sender.NoticeModel; import io.metersphere.notice.sender.NoticeModel;
import io.metersphere.notice.service.NoticeSendService; import io.metersphere.notice.service.NoticeSendService;
import io.metersphere.track.dto.PlanReportCaseDTO;
import io.metersphere.track.dto.TestPlanDTO; import io.metersphere.track.dto.TestPlanDTO;
import io.metersphere.track.request.testcase.QueryTestPlanRequest; import io.metersphere.track.request.testcase.QueryTestPlanRequest;
import io.metersphere.track.request.testcase.TrackCount; import io.metersphere.track.request.testcase.TrackCount;
@ -374,4 +375,10 @@ public class ApiDefinitionExecResultService {
public ApiDefinitionExecResult getInfo(String id) { public ApiDefinitionExecResult getInfo(String id) {
return apiDefinitionExecResultMapper.selectByPrimaryKey(id); return apiDefinitionExecResultMapper.selectByPrimaryKey(id);
} }
public List<PlanReportCaseDTO> selectForPlanReport(List<String> apiReportIds) {
if (CollectionUtils.isEmpty(apiReportIds))
return new ArrayList<>();
return extApiDefinitionExecResultMapper.selectForPlanReport(apiReportIds);
}
} }

View File

@ -32,6 +32,7 @@ import io.metersphere.log.vo.api.ModuleReference;
import io.metersphere.notice.sender.NoticeModel; import io.metersphere.notice.sender.NoticeModel;
import io.metersphere.notice.service.NoticeSendService; import io.metersphere.notice.service.NoticeSendService;
import io.metersphere.service.UserService; import io.metersphere.service.UserService;
import io.metersphere.track.dto.PlanReportCaseDTO;
import io.metersphere.utils.LoggerUtil; import io.metersphere.utils.LoggerUtil;
import org.apache.commons.beanutils.BeanMap; import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
@ -693,6 +694,10 @@ public class ApiScenarioReportService {
return status; return status;
} }
public List<PlanReportCaseDTO> selectForPlanReport(List<String> reportIds) {
return extApiScenarioReportMapper.selectForPlanReport(reportIds);
}
public void cleanUpReport(long time, String projectId) { public void cleanUpReport(long time, String projectId) {
ApiScenarioReportExample example = new ApiScenarioReportExample(); ApiScenarioReportExample example = new ApiScenarioReportExample();
example.createCriteria().andCreateTimeLessThan(time).andProjectIdEqualTo(projectId); example.createCriteria().andCreateTimeLessThan(time).andProjectIdEqualTo(projectId);

View File

@ -2,6 +2,7 @@ package io.metersphere.base.mapper.ext;
import io.metersphere.api.dto.datacount.ExecutedCaseInfoResult; import io.metersphere.api.dto.datacount.ExecutedCaseInfoResult;
import io.metersphere.base.domain.ApiDefinitionExecResult; import io.metersphere.base.domain.ApiDefinitionExecResult;
import io.metersphere.track.dto.PlanReportCaseDTO;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import java.util.Collection; import java.util.Collection;
@ -30,4 +31,5 @@ public interface ExtApiDefinitionExecResultMapper {
List<ApiDefinitionExecResult> selectApiResultByProjectId(String projectId); List<ApiDefinitionExecResult> selectApiResultByProjectId(String projectId);
} List<PlanReportCaseDTO> selectForPlanReport(@Param("ids") List<String> apiReportIds);
}

View File

@ -116,5 +116,12 @@
and (t3.project_id = #{projectId} OR t4.project_id = #{projectId} OR t6.project_id = #{projectId}) and (t3.project_id = #{projectId} OR t4.project_id = #{projectId} OR t6.project_id = #{projectId})
and t.`STATUS` in ('running', 'waiting') and t.`STATUS` in ('running', 'waiting')
</select> </select>
<select id="selectForPlanReport" resultType="io.metersphere.track.dto.PlanReportCaseDTO">
select status from api_definition_exec_result
where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</select>
</mapper> </mapper>

View File

@ -5,6 +5,7 @@ import io.metersphere.api.dto.automation.APIScenarioReportResult;
import io.metersphere.api.dto.datacount.ApiDataCountResult; import io.metersphere.api.dto.datacount.ApiDataCountResult;
import io.metersphere.base.domain.ApiScenarioReport; import io.metersphere.base.domain.ApiScenarioReport;
import io.metersphere.dto.ApiReportCountDTO; import io.metersphere.dto.ApiReportCountDTO;
import io.metersphere.track.dto.PlanReportCaseDTO;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import java.util.Collection; import java.util.Collection;
@ -34,4 +35,6 @@ public interface ExtApiScenarioReportMapper {
List<ApiScenarioReport> selectStatusByIds(@Param("ids") Collection<String> values); List<ApiScenarioReport> selectStatusByIds(@Param("ids") Collection<String> values);
List<ApiScenarioReport> selectReportByProjectId(String projectId); List<ApiScenarioReport> selectReportByProjectId(String projectId);
}
List<PlanReportCaseDTO> selectForPlanReport(@Param("ids") List<String> reportIds);
}

View File

@ -308,4 +308,11 @@
#{value} #{value}
</foreach> </foreach>
</select> </select>
</mapper> <select id="selectForPlanReport" resultType="io.metersphere.track.dto.PlanReportCaseDTO">
select status from api_scenario_report
WHERE id IN
<foreach collection="ids" item="value" separator="," open="(" close=")">
#{value}
</foreach>
</select>
</mapper>

View File

@ -5,6 +5,7 @@ import io.metersphere.base.domain.LoadTestReportWithBLOBs;
import io.metersphere.dto.DashboardTestDTO; import io.metersphere.dto.DashboardTestDTO;
import io.metersphere.dto.ReportDTO; import io.metersphere.dto.ReportDTO;
import io.metersphere.performance.controller.request.ReportRequest; import io.metersphere.performance.controller.request.ReportRequest;
import io.metersphere.track.dto.PlanReportCaseDTO;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import java.util.List; import java.util.List;
@ -22,4 +23,6 @@ public interface ExtLoadTestReportMapper {
void updateJmxContentIfAbsent(LoadTestReportWithBLOBs record); void updateJmxContentIfAbsent(LoadTestReportWithBLOBs record);
List<LoadTestReport> selectReportByProjectId(String projectId); List<LoadTestReport> selectReportByProjectId(String projectId);
List<PlanReportCaseDTO> selectForPlanReport(@Param("ids") List<String> reportIds);
} }

View File

@ -171,6 +171,13 @@
and t.status = 'running' and t.status = 'running'
</select> </select>
<select id="selectForPlanReport" resultType="io.metersphere.track.dto.PlanReportCaseDTO">
select status from load_test_report
WHERE id IN
<foreach collection="ids" item="value" separator="," open="(" close=")">
#{value}
</foreach>
</select>
<update id="updateJmxContentIfAbsent"> <update id="updateJmxContentIfAbsent">
update load_test_report update load_test_report
@ -179,4 +186,4 @@
and jmx_content is null and jmx_content is null
</update> </update>
</mapper> </mapper>

View File

@ -11,8 +11,7 @@ import java.util.Map;
public class TestPlanUtils { public class TestPlanUtils {
public static void buildStatusResultMap(Map<String, TestCaseReportStatusResultDTO> reportStatusResultMap, String result) {
public static void getStatusResultMap(Map<String, TestCaseReportStatusResultDTO> reportStatusResultMap, String result) {
if (StringUtils.isBlank(result)) { if (StringUtils.isBlank(result)) {
result = TestPlanTestCaseStatus.Prepare.name(); result = TestPlanTestCaseStatus.Prepare.name();
} }
@ -35,6 +34,11 @@ public class TestPlanUtils {
} }
} }
/**
* 将map转成前端需要的数组数据
* @param resultMap
* @param statusResult
*/
public static void addToReportCommonStatusResultList(Map<String, TestCaseReportStatusResultDTO> resultMap, public static void addToReportCommonStatusResultList(Map<String, TestCaseReportStatusResultDTO> resultMap,
List<TestCaseReportStatusResultDTO> statusResult) { List<TestCaseReportStatusResultDTO> statusResult) {
addToReportStatusResultList(resultMap, statusResult, TestPlanTestCaseStatus.Pass.name()); addToReportStatusResultList(resultMap, statusResult, TestPlanTestCaseStatus.Pass.name());
@ -48,7 +52,10 @@ public class TestPlanUtils {
addToReportStatusResultList(resultMap, statusResult, TestPlanTestCaseStatus.Prepare.name()); addToReportStatusResultList(resultMap, statusResult, TestPlanTestCaseStatus.Prepare.name());
} }
public static void calculatePlanReport(List<PlanReportCaseDTO> planReportCaseDTOS, /**
* 将当前用例状态对应的统计数据存储在map中
*/
public static void buildStatusResultMap(List<PlanReportCaseDTO> planReportCaseDTOS,
Map<String, TestCaseReportStatusResultDTO> statusResultMap, Map<String, TestCaseReportStatusResultDTO> statusResultMap,
TestPlanSimpleReportDTO report, String successStatus) { TestPlanSimpleReportDTO report, String successStatus) {
planReportCaseDTOS.forEach(item -> { planReportCaseDTOS.forEach(item -> {
@ -56,12 +63,14 @@ public class TestPlanUtils {
String status = item.getStatus(); String status = item.getStatus();
if (StringUtils.isNotBlank(status) if (StringUtils.isNotBlank(status)
&& !StringUtils.equalsAny(status, TestPlanTestCaseStatus.Underway.name(), TestPlanTestCaseStatus.Prepare.name())) { && !StringUtils.equalsAny(status, TestPlanTestCaseStatus.Underway.name(), TestPlanTestCaseStatus.Prepare.name())) {
// 计算执行过的数量
report.setExecuteCount(report.getExecuteCount() + 1); report.setExecuteCount(report.getExecuteCount() + 1);
if (StringUtils.equals(successStatus, status)) { if (StringUtils.equals(successStatus, status)) {
// 计算执行成功的数量
report.setPassCount(report.getPassCount() + 1); report.setPassCount(report.getPassCount() + 1);
} }
} }
TestPlanUtils.getStatusResultMap(statusResultMap, status); buildStatusResultMap(statusResultMap, status);
}); });
} }
} }

View File

@ -560,7 +560,7 @@ public class IssuesService {
Map<String, TestCaseReportStatusResultDTO> statusResultMap = new HashMap<>(); Map<String, TestCaseReportStatusResultDTO> statusResultMap = new HashMap<>();
planReportIssueDTOS.forEach(item -> { planReportIssueDTOS.forEach(item -> {
String status = null; String status;
// 本地缺陷 // 本地缺陷
if (StringUtils.equalsIgnoreCase(item.getPlatform(), IssuesManagePlatform.Local.name()) if (StringUtils.equalsIgnoreCase(item.getPlatform(), IssuesManagePlatform.Local.name())
|| StringUtils.isBlank(item.getPlatform())) { || StringUtils.isBlank(item.getPlatform())) {
@ -571,7 +571,7 @@ public class IssuesService {
if (StringUtils.isBlank(status)) { if (StringUtils.isBlank(status)) {
status = IssuesStatus.NEW.toString(); status = IssuesStatus.NEW.toString();
} }
TestPlanUtils.getStatusResultMap(statusResultMap, status); TestPlanUtils.buildStatusResultMap(statusResultMap, status);
}); });
Set<String> status = statusResultMap.keySet(); Set<String> status = statusResultMap.keySet();
status.forEach(item -> { status.forEach(item -> {

View File

@ -355,12 +355,20 @@ public class TestPlanApiCaseService {
public void calculatePlanReport(String planId, TestPlanSimpleReportDTO report) { public void calculatePlanReport(String planId, TestPlanSimpleReportDTO report) {
List<PlanReportCaseDTO> planReportCaseDTOS = extTestPlanApiCaseMapper.selectForPlanReport(planId); List<PlanReportCaseDTO> planReportCaseDTOS = extTestPlanApiCaseMapper.selectForPlanReport(planId);
calculatePlanReport(report, planReportCaseDTOS);
}
public void calculatePlanReport(List<String> apiReportIds, TestPlanSimpleReportDTO report) {
List<PlanReportCaseDTO> planReportCaseDTOS = apiDefinitionExecResultService.selectForPlanReport(apiReportIds);
calculatePlanReport(report, planReportCaseDTOS);
}
private void calculatePlanReport(TestPlanSimpleReportDTO report, List<PlanReportCaseDTO> planReportCaseDTOS) {
TestPlanApiResultReportDTO apiResult = report.getApiResult(); TestPlanApiResultReportDTO apiResult = report.getApiResult();
List<TestCaseReportStatusResultDTO> statusResult = new ArrayList<>(); List<TestCaseReportStatusResultDTO> statusResult = new ArrayList<>();
Map<String, TestCaseReportStatusResultDTO> statusResultMap = new HashMap<>(); Map<String, TestCaseReportStatusResultDTO> statusResultMap = new HashMap<>();
TestPlanUtils.calculatePlanReport(planReportCaseDTOS, statusResultMap, report, "success"); TestPlanUtils.buildStatusResultMap(planReportCaseDTOS, statusResultMap, report, "success");
TestPlanUtils.addToReportCommonStatusResultList(statusResultMap, statusResult); TestPlanUtils.addToReportCommonStatusResultList(statusResultMap, statusResult);

View File

@ -9,7 +9,10 @@ import io.metersphere.base.mapper.LoadTestMapper;
import io.metersphere.base.mapper.LoadTestReportMapper; import io.metersphere.base.mapper.LoadTestReportMapper;
import io.metersphere.base.mapper.TestPlanLoadCaseMapper; import io.metersphere.base.mapper.TestPlanLoadCaseMapper;
import io.metersphere.base.mapper.TestPlanMapper; import io.metersphere.base.mapper.TestPlanMapper;
import io.metersphere.base.mapper.ext.ExtLoadTestReportMapper;
import io.metersphere.base.mapper.ext.ExtTestPlanLoadCaseMapper; import io.metersphere.base.mapper.ext.ExtTestPlanLoadCaseMapper;
import io.metersphere.commons.constants.PerformanceTestStatus;
import io.metersphere.commons.constants.TestPlanLoadCaseStatus;
import io.metersphere.commons.constants.TestPlanStatus; import io.metersphere.commons.constants.TestPlanStatus;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.*; import io.metersphere.commons.utils.*;
@ -59,6 +62,8 @@ public class TestPlanLoadCaseService {
@Resource @Resource
private LoadTestReportMapper loadTestReportMapper; private LoadTestReportMapper loadTestReportMapper;
@Resource @Resource
private ExtLoadTestReportMapper extLoadTestReportMapper;
@Resource
private LoadTestMapper loadTestMapper; private LoadTestMapper loadTestMapper;
@Resource @Resource
@Lazy @Lazy
@ -184,7 +189,7 @@ public class TestPlanLoadCaseService {
TestPlanLoadCaseExample example = new TestPlanLoadCaseExample(); TestPlanLoadCaseExample example = new TestPlanLoadCaseExample();
example.createCriteria().andLoadReportIdEqualTo(report.getId()); example.createCriteria().andLoadReportIdEqualTo(report.getId());
List<TestPlanLoadCase> cases = testPlanLoadCaseMapper.selectByExample(example); List<TestPlanLoadCase> cases = testPlanLoadCaseMapper.selectByExample(example);
if (CollectionUtils.isEmpty(cases) || !cases.get(0).getStatus().equals("success")) { if (CollectionUtils.isEmpty(cases) || !cases.get(0).getStatus().equals(TestPlanLoadCaseStatus.success.name())) {
break; break;
} }
} }
@ -352,7 +357,7 @@ public class TestPlanLoadCaseService {
example.createCriteria() example.createCriteria()
.andTestPlanIdEqualTo(planId) .andTestPlanIdEqualTo(planId)
.andLoadCaseIdIn(performanceIds) .andLoadCaseIdIn(performanceIds)
.andStatusEqualTo("error"); .andStatusEqualTo(TestPlanLoadCaseStatus.error.name());
return testPlanLoadCaseMapper.countByExample(example) > 0 ? true : false; return testPlanLoadCaseMapper.countByExample(example) > 0 ? true : false;
} }
@ -367,12 +372,31 @@ public class TestPlanLoadCaseService {
public void calculatePlanReport(String planId, TestPlanSimpleReportDTO report) { public void calculatePlanReport(String planId, TestPlanSimpleReportDTO report) {
List<PlanReportCaseDTO> planReportCaseDTOS = extTestPlanLoadCaseMapper.selectForPlanReport(planId); List<PlanReportCaseDTO> planReportCaseDTOS = extTestPlanLoadCaseMapper.selectForPlanReport(planId);
calculatePlanReport(report, planReportCaseDTOS);
}
public void calculatePlanReport(List<String> reportIds, TestPlanSimpleReportDTO report) {
List<PlanReportCaseDTO> planReportCaseDTOS = extLoadTestReportMapper.selectForPlanReport(reportIds);
// 性能测试的报告状态跟用例的执行状态不一样
planReportCaseDTOS.forEach(item -> {
if (item.getStatus().equals(PerformanceTestStatus.Completed.name())) {
item.setStatus(TestPlanLoadCaseStatus.success.name());
} else if (item.getStatus().equals(PerformanceTestStatus.Error.name())) {
item.setStatus(TestPlanLoadCaseStatus.error.name());
} else {
item.setStatus(TestPlanLoadCaseStatus.run.name());
}
});
calculatePlanReport(report, planReportCaseDTOS);
}
private void calculatePlanReport(TestPlanSimpleReportDTO report, List<PlanReportCaseDTO> planReportCaseDTOS) {
TestPlanLoadResultReportDTO loadResult = new TestPlanLoadResultReportDTO(); TestPlanLoadResultReportDTO loadResult = new TestPlanLoadResultReportDTO();
report.setLoadResult(loadResult); report.setLoadResult(loadResult);
List<TestCaseReportStatusResultDTO> statusResult = new ArrayList<>(); List<TestCaseReportStatusResultDTO> statusResult = new ArrayList<>();
Map<String, TestCaseReportStatusResultDTO> statusResultMap = new HashMap<>(); Map<String, TestCaseReportStatusResultDTO> statusResultMap = new HashMap<>();
TestPlanUtils.calculatePlanReport(planReportCaseDTOS, statusResultMap, report, "success"); TestPlanUtils.buildStatusResultMap(planReportCaseDTOS, statusResultMap, report, TestPlanLoadCaseStatus.success.name());
TestPlanUtils.addToReportCommonStatusResultList(statusResultMap, statusResult); TestPlanUtils.addToReportCommonStatusResultList(statusResultMap, statusResult);
loadResult.setCaseData(statusResult); loadResult.setCaseData(statusResult);
@ -389,7 +413,7 @@ public class TestPlanLoadCaseService {
} }
public List<TestPlanLoadCaseDTO> getFailureCases(String planId) { public List<TestPlanLoadCaseDTO> getFailureCases(String planId) {
List<TestPlanLoadCaseDTO> failureCases = extTestPlanLoadCaseMapper.getCases(planId, "error"); List<TestPlanLoadCaseDTO> failureCases = extTestPlanLoadCaseMapper.getCases(planId, TestPlanLoadCaseStatus.error.name());
return buildCases(failureCases); return buildCases(failureCases);
} }

View File

@ -430,11 +430,20 @@ public class TestPlanScenarioCaseService {
public void calculatePlanReport(String planId, TestPlanSimpleReportDTO report) { public void calculatePlanReport(String planId, TestPlanSimpleReportDTO report) {
List<PlanReportCaseDTO> planReportCaseDTOS = extTestPlanScenarioCaseMapper.selectForPlanReport(planId); List<PlanReportCaseDTO> planReportCaseDTOS = extTestPlanScenarioCaseMapper.selectForPlanReport(planId);
calculatePlanReport(report, planReportCaseDTOS);
}
public void calculatePlanReport(List<String> reportIds, TestPlanSimpleReportDTO report) {
List<PlanReportCaseDTO> planReportCaseDTOS = apiScenarioReportService.selectForPlanReport(reportIds);
calculatePlanReport(report, planReportCaseDTOS);
}
private void calculatePlanReport(TestPlanSimpleReportDTO report, List<PlanReportCaseDTO> planReportCaseDTOS) {
TestPlanApiResultReportDTO apiResult = report.getApiResult(); TestPlanApiResultReportDTO apiResult = report.getApiResult();
List<TestCaseReportStatusResultDTO> statusResult = new ArrayList<>(); List<TestCaseReportStatusResultDTO> statusResult = new ArrayList<>();
Map<String, TestCaseReportStatusResultDTO> statusResultMap = new HashMap<>(); Map<String, TestCaseReportStatusResultDTO> statusResultMap = new HashMap<>();
TestPlanUtils.calculatePlanReport(planReportCaseDTOS, statusResultMap, report, "Success"); TestPlanUtils.buildStatusResultMap(planReportCaseDTOS, statusResultMap, report, "Success");
TestPlanUtils.addToReportCommonStatusResultList(statusResultMap, statusResult); TestPlanUtils.addToReportCommonStatusResultList(statusResultMap, statusResult);
TestPlanScenarioStepCountDTO stepCount = new TestPlanScenarioStepCountDTO(); TestPlanScenarioStepCountDTO stepCount = new TestPlanScenarioStepCountDTO();
for (PlanReportCaseDTO item : planReportCaseDTOS) { for (PlanReportCaseDTO item : planReportCaseDTOS) {

View File

@ -384,7 +384,7 @@ public class TestPlanTestCaseService {
List<TestCaseReportStatusResultDTO> statusResult = new ArrayList<>(); List<TestCaseReportStatusResultDTO> statusResult = new ArrayList<>();
Map<String, TestCaseReportStatusResultDTO> statusResultMap = new HashMap<>(); Map<String, TestCaseReportStatusResultDTO> statusResultMap = new HashMap<>();
TestPlanUtils.calculatePlanReport(planReportCaseDTOS, statusResultMap, report, TestPlanTestCaseStatus.Pass.name()); TestPlanUtils.buildStatusResultMap(planReportCaseDTOS, statusResultMap, report, TestPlanTestCaseStatus.Pass.name());
TestPlanUtils.addToReportCommonStatusResultList(statusResultMap, statusResult); TestPlanUtils.addToReportCommonStatusResultList(statusResultMap, statusResult);
TestPlanUtils.addToReportStatusResultList(statusResultMap, statusResult, TestPlanTestCaseStatus.Blocking.name()); TestPlanUtils.addToReportStatusResultList(statusResultMap, statusResult, TestPlanTestCaseStatus.Blocking.name());