From 85aa8ab18ca3ae15483c877589c508bef6b3b67c Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Tue, 22 Mar 2022 19:13:42 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E7=94=A8=E4=BE=8B=E5=88=97=E8=A1=A8=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1011348 --user=赵勇 【接口测试】-接口定义-case列表查询和执行单接口case变慢了 https://www.tapd.cn/55049933/s/1123873 --- .../api/dto/definition/ParamsDTO.java | 9 ++ .../api/service/ApiTestCaseService.java | 51 ++++++++-- .../base/mapper/ext/ExtApiTestCaseMapper.java | 18 ++-- .../base/mapper/ext/ExtApiTestCaseMapper.xml | 94 ++++++++++++------- .../service/TestCaseReviewApiCaseService.java | 2 +- 5 files changed, 128 insertions(+), 46 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/api/dto/definition/ParamsDTO.java diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/ParamsDTO.java b/backend/src/main/java/io/metersphere/api/dto/definition/ParamsDTO.java new file mode 100644 index 0000000000..5e60eab260 --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/definition/ParamsDTO.java @@ -0,0 +1,9 @@ +package io.metersphere.api.dto.definition; + +import lombok.Data; + +@Data +public class ParamsDTO { + private String id; + private String value; +} diff --git a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java index 561c84b8d0..79b35ec689 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java @@ -133,10 +133,7 @@ public class ApiTestCaseService { if (CollectionUtils.isEmpty(apiTestCases)) { return apiTestCases; } - if (BooleanUtils.isTrue(request.isSelectEnvironment())) { - setCaseEnvironment(apiTestCases); - } - buildUserInfo(apiTestCases); + buildUserInfo(apiTestCases, request.isSelectEnvironment()); return apiTestCases; } @@ -202,17 +199,31 @@ public class ApiTestCaseService { return request; } - public void buildUserInfo(List apiTestCases) { + public void buildUserInfo(List apiTestCases, boolean isSelectEnvironment) { List userIds = new ArrayList(); userIds.addAll(apiTestCases.stream().map(ApiTestCaseDTO::getCreateUserId).collect(Collectors.toList())); userIds.addAll(apiTestCases.stream().map(ApiTestCaseDTO::getUpdateUserId).collect(Collectors.toList())); + List ids = apiTestCases.stream().map(ApiTestCaseDTO::getId).collect(Collectors.toList()); + List passRateList = extApiTestCaseMapper.findPassRateByIds(ids); + + Map passRates = passRateList.stream() + .collect(Collectors.toMap(ParamsDTO::getId, ParamsDTO::getValue)); + Map envMap = null; + if (BooleanUtils.isTrue(isSelectEnvironment)) { + envMap = this.getApiCaseEnvironments(ids); + } if (!CollectionUtils.isEmpty(userIds)) { Map userMap = userService.queryNameByIds(userIds); + Map finalEnvMap = envMap; apiTestCases.forEach(caseResult -> { + caseResult.setPassRate(passRates.get(caseResult.getId())); User createUser = userMap.get(caseResult.getCreateUserId()); if (createUser != null) { caseResult.setCreateUser(createUser.getName()); } + if (finalEnvMap != null && finalEnvMap.containsKey(caseResult.getId())) { + caseResult.setEnvironment(finalEnvMap.get(caseResult.getId())); + } User updateUser = userMap.get(caseResult.getUpdateUserId()); if (updateUser != null) { caseResult.setUpdateUser(updateUser.getName()); @@ -222,7 +233,6 @@ public class ApiTestCaseService { } public ApiTestCaseInfo get(String id) { -// ApiTestCaseWithBLOBs returnBlobs = apiTestCaseMapper.selectByPrimaryKey(id); ApiTestCaseInfo model = extApiTestCaseMapper.selectApiCaseInfoByPrimaryKey(id); if (model != null) { if (StringUtils.equalsIgnoreCase(model.getApiMethod(), "esb")) { @@ -1051,6 +1061,35 @@ public class ApiTestCaseService { return null; } + public Map getApiCaseEnvironments(List caseIds) { + List environments = extApiTestCaseMapper.getApiCaseEnvironments(caseIds); + if (CollectionUtils.isEmpty(environments)) { + return null; + } + try { + List envIds = environments.stream().map(ParamsDTO::getValue).collect(Collectors.toList()); + ApiTestEnvironmentExample example = new ApiTestEnvironmentExample(); + example.createCriteria().andIdIn(envIds); + List environmentList = apiTestEnvironmentMapper.selectByExample(example); + if (CollectionUtils.isEmpty(environmentList)) { + return null; + } + Map envMap = environmentList.stream() + .collect(Collectors.toMap(ApiTestEnvironment::getId, ApiTestEnvironment::getName)); + + Map caseEnvMap = environments.stream().collect(HashMap::new, (m, v) -> m.put(v.getId(), v.getValue()), HashMap::putAll); + caseEnvMap.forEach((k, v) -> { + if (envMap.containsKey(v)) { + caseEnvMap.put(k, envMap.get(v)); + } + }); + return caseEnvMap; + } catch (Exception e) { + LogUtil.error("api case environmentId incorrect parsing", e); + } + return null; + } + public List getFollows(String testId) { List result = new ArrayList<>(); if (StringUtils.isBlank(testId)) { diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.java index 9c2d119b4f..44d545b6ab 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.java @@ -4,7 +4,6 @@ import io.metersphere.api.dto.datacount.ApiDataCountResult; import io.metersphere.api.dto.definition.*; import io.metersphere.base.domain.ApiDefinition; import io.metersphere.base.domain.ApiTestCase; -import io.metersphere.controller.request.BaseQueryRequest; import org.apache.ibatis.annotations.Param; import java.util.List; @@ -16,6 +15,7 @@ public interface ExtApiTestCaseMapper { List listSimple(@Param("request") ApiTestCaseRequest request); List selectIdsNotExistsInPlan(@Param("projectId") String projectId, @Param("planId") String planId); + List selectIdsNotExistsInReview(@Param("projectId") String projectId, @Param("reviewId") String reviewId); @@ -43,9 +43,10 @@ public interface ExtApiTestCaseMapper { List getCannotReductionApiCaseList(@Param("ids") List ids); - List selectCaseIdsByApiIds(@Param("ids")List apiIds); + List selectCaseIdsByApiIds(@Param("ids") List apiIds); + + List selectNameByIdIn(@Param("ids") List ids); - List selectNameByIdIn(@Param("ids")List ids); String selectNameById(String id); List selectIdsByQuery(@Param("request") ApiTestCaseRequest request); @@ -54,16 +55,21 @@ public interface ExtApiTestCaseMapper { List getIdsOrderByUpdateTime(@Param("projectId") String projectId); - Long getPreOrder(@Param("projectId")String projectId, @Param("baseOrder") Long baseOrder); + Long getPreOrder(@Param("projectId") String projectId, @Param("baseOrder") Long baseOrder); - Long getLastOrder(@Param("projectId")String projectId, @Param("baseOrder") Long baseOrder); + Long getLastOrder(@Param("projectId") String projectId, @Param("baseOrder") Long baseOrder); /** * 获取接口用例的环境 + * * @param caseId 用例ID * @return ApiEnvironment */ String getApiCaseEnvironment(@Param("caseId") String caseId); - int moduleCount(@Param("request")ApiTestCaseRequest request); + int moduleCount(@Param("request") ApiTestCaseRequest request); + + List findPassRateByIds(@Param("ids") List ids); + + List getApiCaseEnvironments(@Param("caseIds") List caseIds); } diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml index 688a3e4650..9d85238ee8 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml @@ -312,22 +312,62 @@ + + @@ -643,7 +665,7 @@ WHERE testCase.id = #{0} - + and t1.name like CONCAT('%', #{request.name},'%') @@ -751,6 +773,12 @@ order by `order` desc limit 1; + diff --git a/backend/src/main/java/io/metersphere/track/service/TestCaseReviewApiCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestCaseReviewApiCaseService.java index c1c65cb845..f13dc86e6e 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseReviewApiCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseReviewApiCaseService.java @@ -42,7 +42,7 @@ public class TestCaseReviewApiCaseService { if (CollectionUtils.isEmpty(apiTestCases)) { return apiTestCases; } - apiTestCaseService.buildUserInfo(apiTestCases); + apiTestCaseService.buildUserInfo(apiTestCases, request.isSelectEnvironment()); return apiTestCases; } public List getExecResultByReviewId(String reviewId) {