From aca31b166c0b485e186192824f6e001e67d86115 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Mon, 31 May 2021 18:01:37 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E3=80=81=E6=8E=A5=E5=8F=A3=E5=9C=BA=E6=99=AF?= =?UTF-8?q?=E3=80=81=E5=8A=9F=E8=83=BD=E6=A1=88=E4=BE=8B=E7=9A=84=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E6=A0=91=E6=9F=A5=E6=89=BE=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化接口定义、接口场景、功能案例的模块树查找速度 --- .../api/service/ApiModuleService.java | 49 ++++++++++++++++- .../api/service/ApiScenarioModuleService.java | 54 ++++++++++++++++--- .../mapper/ext/ExtApiDefinitionMapper.java | 3 ++ .../mapper/ext/ExtApiDefinitionMapper.xml | 6 +++ .../base/mapper/ext/ExtApiScenarioMapper.java | 3 ++ .../base/mapper/ext/ExtApiScenarioMapper.xml | 6 +++ .../base/mapper/ext/ExtTestCaseMapper.java | 3 ++ .../base/mapper/ext/ExtTestCaseMapper.xml | 6 +++ .../track/service/TestCaseNodeService.java | 54 +++++++++++++++++-- 9 files changed, 171 insertions(+), 13 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java b/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java index a227616dc8..50ca40240c 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java @@ -88,16 +88,61 @@ public class ApiModuleService extends NodeTreeService { Map> filters = new LinkedHashMap<>(); filters.put("status", list); request.setFilters(filters); +// apiModules.forEach(node -> { +// List moduleIds = new ArrayList<>(); +// moduleIds = this.nodeList(apiModules, node.getId(), moduleIds); +// moduleIds.add(node.getId()); +// request.setModuleIds(moduleIds); +// node.setCaseNum(extApiDefinitionMapper.moduleCount(request)); +// }); + + //优化: 所有统计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); + 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()); - request.setModuleIds(moduleIds); - node.setCaseNum(extApiDefinitionMapper.moduleCount(request)); + int countNum = 0; + for (String moduleId : moduleIds) { + if(moduleCountMap.containsKey(moduleId)){ + countNum += moduleCountMap.get(moduleId).intValue(); + } + } + node.setCaseNum(countNum); }); return getNodeTrees(apiModules); } + private Map parseModuleCountList(List> moduleCountList) { + Map returnMap = new HashMap<>(); + for (Map map: moduleCountList){ + Object moduleIdObj = map.get("moduleId"); + Object countNumObj = map.get("countNum"); + if(moduleIdObj!= null && countNumObj != null){ + String moduleId = String.valueOf(moduleIdObj); + try { + Integer countNumInteger = new Integer(String.valueOf(countNumObj)); + returnMap.put(moduleId,countNumInteger); + }catch (Exception e){ + } + } + } + return returnMap; + } + public static List nodeList(List apiNodes, String pid, List list) { for (ApiModuleDTO node : apiNodes) { //遍历出父id等于参数的id,add进子节点集合 diff --git a/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java b/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java index f3c431f123..772827551c 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java @@ -87,16 +87,58 @@ public class ApiScenarioModuleService extends NodeTreeService> filters = new LinkedHashMap<>(); filters.put("status", list); request.setFilters(filters); + //优化:所有SQL统一查出来 +// nodes.forEach(node -> { +// List scenarioNodes = new ArrayList<>(); +// scenarioNodes = this.nodeList(nodes, node.getId(), scenarioNodes); +// scenarioNodes.add(node.getId()); +// request.setModuleIds(scenarioNodes); +// node.setCaseNum(extApiScenarioMapper.listModule(request)); +// }); + 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 scenarioNodes = new ArrayList<>(); - scenarioNodes = this.nodeList(nodes, node.getId(), scenarioNodes); - scenarioNodes.add(node.getId()); - request.setModuleIds(scenarioNodes); - node.setCaseNum(extApiScenarioMapper.listModule(request)); + 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); } - + private Map parseModuleCountList(List> moduleCountList) { + Map returnMap = new HashMap<>(); + for (Map map: moduleCountList){ + Object moduleIdObj = map.get("moduleId"); + Object countNumObj = map.get("countNum"); + if(moduleIdObj!= null && countNumObj != null){ + String moduleId = String.valueOf(moduleIdObj); + try { + Integer countNumInteger = new Integer(String.valueOf(countNumObj)); + returnMap.put(moduleId,countNumInteger); + }catch (Exception e){ + } + } + } + return returnMap; + } public static List nodeList(List nodes, String pid, List list) { for (ApiScenarioModuleDTO node : nodes) { diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.java index bc48afd234..4419b56e9b 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.java @@ -11,6 +11,7 @@ import io.metersphere.controller.request.BaseQueryRequest; import org.apache.ibatis.annotations.Param; import java.util.List; +import java.util.Map; public interface ExtApiDefinitionMapper { List selectScheduleList(@Param("projectId") String projectId); @@ -48,4 +49,6 @@ public interface ExtApiDefinitionMapper { List selectEffectiveIdByProjectId(String projectId); List listByIds(@Param("ids") List ids); + + List> moduleCountByCollection(@Param("request") ApiDefinitionRequest request); } diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.xml index 9d8fdae9a8..ffc874ba74 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.xml @@ -492,6 +492,12 @@ + + diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.java index 1efb53b53d..3c8a553a5f 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.java @@ -9,6 +9,7 @@ import io.metersphere.base.domain.ApiScenarioWithBLOBs; import org.apache.ibatis.annotations.Param; import java.util.List; +import java.util.Map; public interface ExtApiScenarioMapper { List list(@Param("request") ApiScenarioRequest request); @@ -48,4 +49,6 @@ public interface ExtApiScenarioMapper { void updateCustomNumByProjectId(@Param("projectId") String projectId); List listWithIds(@Param("ids") List ids); + + List> listModuleByCollection(@Param("request") ApiScenarioRequest request); } diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.xml index ef86d0f7c8..0d4d9f1347 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.xml @@ -136,6 +136,12 @@ select count(id) from api_scenario + + + +