refactor(接口测试): 优化模块树加载方法

--story=1011189 --user=赵勇 系统左侧模块树加载效率优化 https://www.tapd.cn/55049933/s/1343693
This commit is contained in:
fit2-zhao 2023-02-27 17:38:25 +08:00 committed by fit2-zhao
parent 1a13c8f989
commit 3ddb7903ea
3 changed files with 33 additions and 124 deletions

View File

@ -1,8 +1,17 @@
package io.metersphere.commons.constants; package io.metersphere.commons.constants;
import io.metersphere.commons.enums.ApiTestDataStatus;
import java.util.List;
public class ApiTestConstants { public class ApiTestConstants {
public static final String JAR_PATH = "JAR_PATH"; public static final String JAR_PATH = "JAR_PATH";
public static final String ROOT = "root"; public static final String ROOT = "root";
public static final String LAST_RESULT = "last_result"; public static final String LAST_RESULT = "last_result";
public static final String FAKE_ERROR = "FakeError"; public static final String FAKE_ERROR = "FakeError";
public static final String STATUS = "status";
public static final List<String> STATUS_ALL = List.of(
ApiTestDataStatus.PREPARE.getValue(), ApiTestDataStatus.UNDERWAY.getValue(), ApiTestDataStatus.COMPLETED.getValue()
);
} }

View File

@ -10,10 +10,7 @@ import io.metersphere.base.mapper.ProjectMapper;
import io.metersphere.base.mapper.ext.ExtApiDefinitionMapper; import io.metersphere.base.mapper.ext.ExtApiDefinitionMapper;
import io.metersphere.base.mapper.ext.ExtApiModuleMapper; import io.metersphere.base.mapper.ext.ExtApiModuleMapper;
import io.metersphere.base.mapper.ext.ExtApiTestCaseMapper; import io.metersphere.base.mapper.ext.ExtApiTestCaseMapper;
import io.metersphere.commons.constants.ProjectModuleDefaultNodeEnum; import io.metersphere.commons.constants.*;
import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.constants.RequestTypeConstants;
import io.metersphere.commons.constants.TestCaseConstants;
import io.metersphere.commons.enums.ApiTestDataStatus; import io.metersphere.commons.enums.ApiTestDataStatus;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.JSON; import io.metersphere.commons.utils.JSON;
@ -37,6 +34,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -113,7 +111,7 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
if (CollectionUtils.isNotEmpty(apiModules) && MapUtils.isNotEmpty(trashApiMap)) { if (CollectionUtils.isNotEmpty(apiModules) && MapUtils.isNotEmpty(trashApiMap)) {
apiModules.forEach(node -> { apiModules.forEach(node -> {
List<String> moduleIds = new ArrayList<>(); List<String> moduleIds = new ArrayList<>();
moduleIds = this.nodeList(apiModules, node.getId(), moduleIds); this.nodeList(apiModules, node.getId(), moduleIds);
moduleIds.add(node.getId()); moduleIds.add(node.getId());
int countNum = 0; int countNum = 0;
for (String moduleId : moduleIds) { for (String moduleId : moduleIds) {
@ -153,83 +151,29 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
public List<ApiModuleDTO> getNodeTreeByProjectId(String projectId, String protocol, String versionId) { public List<ApiModuleDTO> getNodeTreeByProjectId(String projectId, String protocol, String versionId) {
ApiDefinitionRequest request = new ApiDefinitionRequest(); ApiDefinitionRequest request = new ApiDefinitionRequest();
List<ApiModuleDTO> apiModules = getApiModulesByProjectAndPro(projectId, protocol); return getNodeTreeByCondition(projectId, protocol, versionId, request);
request.setProjectId(projectId);
request.setProtocol(protocol);
List<String> list = new ArrayList<>();
list.add(ApiTestDataStatus.PREPARE.getValue());
list.add(ApiTestDataStatus.UNDERWAY.getValue());
list.add(ApiTestDataStatus.COMPLETED.getValue());
Map<String, List<String>> filters = new LinkedHashMap<>();
filters.put("status", list);
request.setFilters(filters);
//优化 所有统计SQL一次查询出来
List<String> allModuleIdList = new ArrayList<>();
for (ApiModuleDTO node : apiModules) {
List<String> 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<Map<String, Object>> moduleCountList = extApiDefinitionMapper.moduleCountByCollection(request);
Map<String, Integer> moduleCountMap = this.parseModuleCountList(moduleCountList);
apiModules.forEach(node -> {
List<String> 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);
} }
public List<ApiModuleDTO> getNodeTreeByCondition(String projectId, String protocol, String versionId, ApiDefinitionRequest request) { public List<ApiModuleDTO> getNodeTreeByCondition(String projectId, String protocol, String versionId, ApiDefinitionRequest request) {
List<ApiModuleDTO> apiModules = getApiModulesByProjectAndPro(projectId, protocol); List<ApiModuleDTO> apiModules = getApiModulesByProjectAndPro(projectId, protocol);
LogUtil.info("当前API模块节点", apiModules.size());
request.setProjectId(projectId); request.setProjectId(projectId);
request.setProtocol(protocol); request.setProtocol(protocol);
List<String> list = new ArrayList<>();
list.add(ApiTestDataStatus.PREPARE.getValue());
list.add(ApiTestDataStatus.UNDERWAY.getValue());
list.add(ApiTestDataStatus.COMPLETED.getValue());
Map<String, List<String>> filters = new LinkedHashMap<>(); Map<String, List<String>> filters = new LinkedHashMap<>();
filters.put("status", list); filters.put(ApiTestConstants.STATUS, ApiTestConstants.STATUS_ALL);
request.setFilters(filters); request.setFilters(filters);
//优化 所有统计SQL一次查询出来
List<String> allModuleIdList = new ArrayList<>();
for (ApiModuleDTO node : apiModules) {
List<String> 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)) { if (StringUtils.isNotBlank(versionId)) {
request.setVersionId(versionId); request.setVersionId(versionId);
} }
List<Map<String, Object>> moduleCountList = extApiDefinitionMapper.moduleCountByCollection(request); List<Map<String, Object>> moduleCountList = extApiDefinitionMapper.moduleCountByCollection(request);
Map<String, Integer> moduleCountMap = this.parseModuleCountList(moduleCountList); Map<String, Integer> moduleCountMap = this.parseModuleCountList(moduleCountList);
apiModules.forEach(node -> { // 获取所有模块数ID
for (ApiModuleDTO node : apiModules) {
List<String> moduleIds = new ArrayList<>(); List<String> moduleIds = new ArrayList<>();
moduleIds = this.nodeList(apiModules, node.getId(), moduleIds); this.nodeList(apiModules, node.getId(), moduleIds);
moduleIds.add(node.getId()); moduleIds.add(node.getId());
int countNum = 0; int countNum = 0;
for (String moduleId : moduleIds) { for (String moduleId : moduleIds) {
@ -238,7 +182,7 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
} }
} }
node.setCaseNum(countNum); node.setCaseNum(countNum);
}); }
return getNodeTrees(apiModules); return getNodeTrees(apiModules);
} }
@ -260,7 +204,7 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
return returnMap; return returnMap;
} }
public static List<String> nodeList(List<ApiModuleDTO> apiNodes, String pid, List<String> list) { public static void nodeList(List<ApiModuleDTO> apiNodes, String pid, List<String> list) {
for (ApiModuleDTO node : apiNodes) { for (ApiModuleDTO node : apiNodes) {
//遍历出父id等于参数的idadd进子节点集合 //遍历出父id等于参数的idadd进子节点集合
if (StringUtils.equals(node.getParentId(), pid)) { if (StringUtils.equals(node.getParentId(), pid)) {
@ -269,7 +213,6 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
nodeList(apiNodes, node.getId(), list); nodeList(apiNodes, node.getId(), list);
} }
} }
return list;
} }
public String addNode(ApiModule node) { public String addNode(ApiModule node) {

View File

@ -9,6 +9,7 @@ import io.metersphere.base.mapper.ApiScenarioModuleMapper;
import io.metersphere.base.mapper.ProjectMapper; import io.metersphere.base.mapper.ProjectMapper;
import io.metersphere.base.mapper.ext.ExtApiScenarioMapper; import io.metersphere.base.mapper.ext.ExtApiScenarioMapper;
import io.metersphere.base.mapper.ext.ExtApiScenarioModuleMapper; import io.metersphere.base.mapper.ext.ExtApiScenarioModuleMapper;
import io.metersphere.commons.constants.ApiTestConstants;
import io.metersphere.commons.constants.ProjectModuleDefaultNodeEnum; import io.metersphere.commons.constants.ProjectModuleDefaultNodeEnum;
import io.metersphere.commons.constants.PropertyConstant; import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.constants.TestCaseConstants; import io.metersphere.commons.constants.TestCaseConstants;
@ -33,6 +34,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function; import java.util.function.Function;
@ -62,29 +64,18 @@ public class ApiScenarioModuleService extends NodeTreeService<ApiScenarioModuleD
public List<ApiScenarioModuleDTO> getNodeTreeByProjectId(String projectId) { public List<ApiScenarioModuleDTO> getNodeTreeByProjectId(String projectId) {
List<ApiScenarioModuleDTO> nodes = extApiScenarioModuleMapper.getNodeTreeByProjectId(projectId); List<ApiScenarioModuleDTO> nodes = extApiScenarioModuleMapper.getNodeTreeByProjectId(projectId);
ApiScenarioRequest request = new ApiScenarioRequest(); ApiScenarioRequest request = new ApiScenarioRequest();
return getApiScenarioModuleDTOS(projectId, nodes, request);
}
private List<ApiScenarioModuleDTO> getApiScenarioModuleDTOS(String projectId, List<ApiScenarioModuleDTO> nodes, ApiScenarioRequest request) {
request.setProjectId(projectId); request.setProjectId(projectId);
List<String> list = new ArrayList<>();
list.add(ApiTestDataStatus.PREPARE.getValue());
list.add(ApiTestDataStatus.UNDERWAY.getValue());
list.add(ApiTestDataStatus.COMPLETED.getValue());
Map<String, List<String>> filters = new LinkedHashMap<>(); Map<String, List<String>> filters = new LinkedHashMap<>();
filters.put("status", list); filters.put(ApiTestConstants.STATUS, ApiTestConstants.STATUS_ALL);
request.setFilters(filters); request.setFilters(filters);
List<String> allModuleIdList = new ArrayList<>();
for (ApiScenarioModuleDTO node : nodes) {
List<String> 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<Map<String, Object>> moduleCountList = extApiScenarioMapper.listModuleByCollection(request); List<Map<String, Object>> moduleCountList = extApiScenarioMapper.listModuleByCollection(request);
Map<String, Integer> moduleCountMap = this.parseModuleCountList(moduleCountList); Map<String, Integer> moduleCountMap = this.parseModuleCountList(moduleCountList);
nodes.forEach(node -> { for (ApiScenarioModuleDTO node : nodes) {
List<String> moduleIds = new ArrayList<>(); List<String> moduleIds = new ArrayList<>();
moduleIds = this.nodeList(nodes, node.getId(), moduleIds); moduleIds = this.nodeList(nodes, node.getId(), moduleIds);
moduleIds.add(node.getId()); moduleIds.add(node.getId());
@ -95,47 +86,13 @@ public class ApiScenarioModuleService extends NodeTreeService<ApiScenarioModuleD
} }
} }
node.setCaseNum(countNum); node.setCaseNum(countNum);
}); }
return getNodeTrees(nodes); return getNodeTrees(nodes);
} }
public List<ApiScenarioModuleDTO> getNodeTreeByProjectId(String projectId, ApiScenarioRequest request) { public List<ApiScenarioModuleDTO> getNodeTreeByProjectId(String projectId, ApiScenarioRequest request) {
List<ApiScenarioModuleDTO> nodes = extApiScenarioModuleMapper.getNodeTreeByProjectId(projectId); List<ApiScenarioModuleDTO> nodes = extApiScenarioModuleMapper.getNodeTreeByProjectId(projectId);
request.setProjectId(projectId); return getApiScenarioModuleDTOS(projectId, nodes, request);
List<String> list = new ArrayList<>();
list.add(ApiTestDataStatus.PREPARE.getValue());
list.add(ApiTestDataStatus.UNDERWAY.getValue());
list.add(ApiTestDataStatus.COMPLETED.getValue());
Map<String, List<String>> filters = new LinkedHashMap<>();
filters.put("status", list);
request.setFilters(filters);
List<String> allModuleIdList = new ArrayList<>();
for (ApiScenarioModuleDTO node : nodes) {
List<String> 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<Map<String, Object>> moduleCountList = extApiScenarioMapper.listModuleByCollection(request);
Map<String, Integer> moduleCountMap = this.parseModuleCountList(moduleCountList);
nodes.forEach(node -> {
List<String> 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);
} }
public List<ApiScenarioModuleDTO> getTrashNodeTreeByProjectId(String projectId) { public List<ApiScenarioModuleDTO> getTrashNodeTreeByProjectId(String projectId) {