From 3ddb7903ea91fd505c3fb176824ce2d37c0ab86c Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Mon, 27 Feb 2023 17:38:25 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E6=A8=A1=E5=9D=97=E6=A0=91=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --story=1011189 --user=赵勇 系统左侧模块树加载效率优化 https://www.tapd.cn/55049933/s/1343693 --- .../commons/constants/ApiTestConstants.java | 9 ++ .../service/definition/ApiModuleService.java | 83 +++---------------- .../scenario/ApiScenarioModuleService.java | 65 +++------------ 3 files changed, 33 insertions(+), 124 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/commons/constants/ApiTestConstants.java b/api-test/backend/src/main/java/io/metersphere/commons/constants/ApiTestConstants.java index 4d8d9b72a5..1c44a53636 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/constants/ApiTestConstants.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/constants/ApiTestConstants.java @@ -1,8 +1,17 @@ package io.metersphere.commons.constants; +import io.metersphere.commons.enums.ApiTestDataStatus; + +import java.util.List; + public class ApiTestConstants { public static final String JAR_PATH = "JAR_PATH"; public static final String ROOT = "root"; public static final String LAST_RESULT = "last_result"; public static final String FAKE_ERROR = "FakeError"; + public static final String STATUS = "status"; + + public static final List STATUS_ALL = List.of( + ApiTestDataStatus.PREPARE.getValue(), ApiTestDataStatus.UNDERWAY.getValue(), ApiTestDataStatus.COMPLETED.getValue() + ); } diff --git a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiModuleService.java b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiModuleService.java index 1f5da828af..24f0d14611 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiModuleService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiModuleService.java @@ -10,10 +10,7 @@ import io.metersphere.base.mapper.ProjectMapper; import io.metersphere.base.mapper.ext.ExtApiDefinitionMapper; import io.metersphere.base.mapper.ext.ExtApiModuleMapper; import io.metersphere.base.mapper.ext.ExtApiTestCaseMapper; -import io.metersphere.commons.constants.ProjectModuleDefaultNodeEnum; -import io.metersphere.commons.constants.PropertyConstant; -import io.metersphere.commons.constants.RequestTypeConstants; -import io.metersphere.commons.constants.TestCaseConstants; +import io.metersphere.commons.constants.*; import io.metersphere.commons.enums.ApiTestDataStatus; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.JSON; @@ -37,6 +34,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import jakarta.annotation.Resource; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -113,7 +111,7 @@ public class ApiModuleService extends NodeTreeService { if (CollectionUtils.isNotEmpty(apiModules) && MapUtils.isNotEmpty(trashApiMap)) { apiModules.forEach(node -> { List moduleIds = new ArrayList<>(); - moduleIds = this.nodeList(apiModules, node.getId(), moduleIds); + this.nodeList(apiModules, node.getId(), moduleIds); moduleIds.add(node.getId()); int countNum = 0; for (String moduleId : moduleIds) { @@ -153,83 +151,29 @@ public class ApiModuleService extends NodeTreeService { public List getNodeTreeByProjectId(String projectId, String protocol, String versionId) { ApiDefinitionRequest request = new ApiDefinitionRequest(); - List apiModules = getApiModulesByProjectAndPro(projectId, protocol); - request.setProjectId(projectId); - request.setProtocol(protocol); - List list = new ArrayList<>(); - list.add(ApiTestDataStatus.PREPARE.getValue()); - list.add(ApiTestDataStatus.UNDERWAY.getValue()); - list.add(ApiTestDataStatus.COMPLETED.getValue()); - Map> filters = new LinkedHashMap<>(); - filters.put("status", list); - request.setFilters(filters); - - //优化: 所有统计SQL一次查询出来 - List allModuleIdList = new ArrayList<>(); - for (ApiModuleDTO node : apiModules) { - List moduleIds = new ArrayList<>(); - moduleIds = this.nodeList(apiModules, node.getId(), moduleIds); - moduleIds.add(node.getId()); - for (String moduleId : moduleIds) { - if (!allModuleIdList.contains(moduleId)) { - allModuleIdList.add(moduleId); - } - } - } - request.setModuleIds(allModuleIdList); - if (StringUtils.isNotBlank(versionId)) { - request.setVersionId(versionId); - } - List> moduleCountList = extApiDefinitionMapper.moduleCountByCollection(request); - Map moduleCountMap = this.parseModuleCountList(moduleCountList); - apiModules.forEach(node -> { - List moduleIds = new ArrayList<>(); - moduleIds = this.nodeList(apiModules, node.getId(), moduleIds); - moduleIds.add(node.getId()); - int countNum = 0; - for (String moduleId : moduleIds) { - if (moduleCountMap.containsKey(moduleId)) { - countNum += moduleCountMap.get(moduleId).intValue(); - } - } - node.setCaseNum(countNum); - }); - return getNodeTrees(apiModules); + return getNodeTreeByCondition(projectId, protocol, versionId, request); } public List getNodeTreeByCondition(String projectId, String protocol, String versionId, ApiDefinitionRequest request) { List apiModules = getApiModulesByProjectAndPro(projectId, protocol); + LogUtil.info("当前API模块节点:", apiModules.size()); + request.setProjectId(projectId); request.setProtocol(protocol); - List list = new ArrayList<>(); - list.add(ApiTestDataStatus.PREPARE.getValue()); - list.add(ApiTestDataStatus.UNDERWAY.getValue()); - list.add(ApiTestDataStatus.COMPLETED.getValue()); Map> filters = new LinkedHashMap<>(); - filters.put("status", list); + filters.put(ApiTestConstants.STATUS, ApiTestConstants.STATUS_ALL); request.setFilters(filters); - //优化: 所有统计SQL一次查询出来 - List allModuleIdList = new ArrayList<>(); - for (ApiModuleDTO node : apiModules) { - List moduleIds = new ArrayList<>(); - moduleIds = this.nodeList(apiModules, node.getId(), moduleIds); - moduleIds.add(node.getId()); - for (String moduleId : moduleIds) { - if (!allModuleIdList.contains(moduleId)) { - allModuleIdList.add(moduleId); - } - } - } - request.setModuleIds(allModuleIdList); if (StringUtils.isNotBlank(versionId)) { request.setVersionId(versionId); } + List> moduleCountList = extApiDefinitionMapper.moduleCountByCollection(request); Map moduleCountMap = this.parseModuleCountList(moduleCountList); - apiModules.forEach(node -> { + // 获取所有模块数ID + for (ApiModuleDTO node : apiModules) { List moduleIds = new ArrayList<>(); - moduleIds = this.nodeList(apiModules, node.getId(), moduleIds); + this.nodeList(apiModules, node.getId(), moduleIds); moduleIds.add(node.getId()); int countNum = 0; for (String moduleId : moduleIds) { @@ -238,7 +182,7 @@ public class ApiModuleService extends NodeTreeService { } } node.setCaseNum(countNum); - }); + } return getNodeTrees(apiModules); } @@ -260,7 +204,7 @@ public class ApiModuleService extends NodeTreeService { return returnMap; } - public static List nodeList(List apiNodes, String pid, List list) { + public static void nodeList(List apiNodes, String pid, List list) { for (ApiModuleDTO node : apiNodes) { //遍历出父id等于参数的id,add进子节点集合 if (StringUtils.equals(node.getParentId(), pid)) { @@ -269,7 +213,6 @@ public class ApiModuleService extends NodeTreeService { nodeList(apiNodes, node.getId(), list); } } - return list; } public String addNode(ApiModule node) { diff --git a/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioModuleService.java b/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioModuleService.java index c65f10036d..1c1166d4e9 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioModuleService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioModuleService.java @@ -9,6 +9,7 @@ import io.metersphere.base.mapper.ApiScenarioModuleMapper; import io.metersphere.base.mapper.ProjectMapper; import io.metersphere.base.mapper.ext.ExtApiScenarioMapper; import io.metersphere.base.mapper.ext.ExtApiScenarioModuleMapper; +import io.metersphere.commons.constants.ApiTestConstants; import io.metersphere.commons.constants.ProjectModuleDefaultNodeEnum; import io.metersphere.commons.constants.PropertyConstant; import io.metersphere.commons.constants.TestCaseConstants; @@ -33,6 +34,7 @@ import org.springframework.transaction.annotation.Transactional; import org.apache.commons.collections.CollectionUtils; import jakarta.annotation.Resource; + import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Function; @@ -62,29 +64,18 @@ public class ApiScenarioModuleService extends NodeTreeService getNodeTreeByProjectId(String projectId) { List nodes = extApiScenarioModuleMapper.getNodeTreeByProjectId(projectId); ApiScenarioRequest request = new ApiScenarioRequest(); + return getApiScenarioModuleDTOS(projectId, nodes, request); + } + + private List getApiScenarioModuleDTOS(String projectId, List nodes, ApiScenarioRequest request) { request.setProjectId(projectId); - List list = new ArrayList<>(); - list.add(ApiTestDataStatus.PREPARE.getValue()); - list.add(ApiTestDataStatus.UNDERWAY.getValue()); - list.add(ApiTestDataStatus.COMPLETED.getValue()); Map> filters = new LinkedHashMap<>(); - filters.put("status", list); + filters.put(ApiTestConstants.STATUS, ApiTestConstants.STATUS_ALL); request.setFilters(filters); - List allModuleIdList = new ArrayList<>(); - for (ApiScenarioModuleDTO node : nodes) { - List moduleIds = new ArrayList<>(); - moduleIds = this.nodeList(nodes, node.getId(), moduleIds); - moduleIds.add(node.getId()); - for (String moduleId : moduleIds) { - if (!allModuleIdList.contains(moduleId)) { - allModuleIdList.add(moduleId); - } - } - } - request.setModuleIds(allModuleIdList); + List> moduleCountList = extApiScenarioMapper.listModuleByCollection(request); Map moduleCountMap = this.parseModuleCountList(moduleCountList); - nodes.forEach(node -> { + for (ApiScenarioModuleDTO node : nodes) { List moduleIds = new ArrayList<>(); moduleIds = this.nodeList(nodes, node.getId(), moduleIds); moduleIds.add(node.getId()); @@ -95,47 +86,13 @@ public class ApiScenarioModuleService extends NodeTreeService getNodeTreeByProjectId(String projectId, ApiScenarioRequest request) { List nodes = extApiScenarioModuleMapper.getNodeTreeByProjectId(projectId); - request.setProjectId(projectId); - List list = new ArrayList<>(); - list.add(ApiTestDataStatus.PREPARE.getValue()); - list.add(ApiTestDataStatus.UNDERWAY.getValue()); - list.add(ApiTestDataStatus.COMPLETED.getValue()); - Map> filters = new LinkedHashMap<>(); - filters.put("status", list); - request.setFilters(filters); - List allModuleIdList = new ArrayList<>(); - for (ApiScenarioModuleDTO node : nodes) { - List moduleIds = new ArrayList<>(); - moduleIds = this.nodeList(nodes, node.getId(), moduleIds); - moduleIds.add(node.getId()); - for (String moduleId : moduleIds) { - if (!allModuleIdList.contains(moduleId)) { - allModuleIdList.add(moduleId); - } - } - } - request.setModuleIds(allModuleIdList); - List> moduleCountList = extApiScenarioMapper.listModuleByCollection(request); - Map moduleCountMap = this.parseModuleCountList(moduleCountList); - nodes.forEach(node -> { - List moduleIds = new ArrayList<>(); - moduleIds = this.nodeList(nodes, node.getId(), moduleIds); - moduleIds.add(node.getId()); - int countNum = 0; - for (String moduleId : moduleIds) { - if (moduleCountMap.containsKey(moduleId)) { - countNum += moduleCountMap.get(moduleId).intValue(); - } - } - node.setCaseNum(countNum); - }); - return getNodeTrees(nodes); + return getApiScenarioModuleDTOS(projectId, nodes, request); } public List getTrashNodeTreeByProjectId(String projectId) {