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;
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<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.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<ApiModuleDTO> {
if (CollectionUtils.isNotEmpty(apiModules) && MapUtils.isNotEmpty(trashApiMap)) {
apiModules.forEach(node -> {
List<String> 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<ApiModuleDTO> {
public List<ApiModuleDTO> getNodeTreeByProjectId(String projectId, String protocol, String versionId) {
ApiDefinitionRequest request = new ApiDefinitionRequest();
List<ApiModuleDTO> apiModules = getApiModulesByProjectAndPro(projectId, protocol);
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);
return getNodeTreeByCondition(projectId, protocol, versionId, request);
}
public List<ApiModuleDTO> getNodeTreeByCondition(String projectId, String protocol, String versionId, ApiDefinitionRequest request) {
List<ApiModuleDTO> apiModules = getApiModulesByProjectAndPro(projectId, protocol);
LogUtil.info("当前API模块节点", apiModules.size());
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);
filters.put(ApiTestConstants.STATUS, ApiTestConstants.STATUS_ALL);
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 -> {
// 获取所有模块数ID
for (ApiModuleDTO node : apiModules) {
List<String> 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<ApiModuleDTO> {
}
}
node.setCaseNum(countNum);
});
}
return getNodeTrees(apiModules);
}
@ -260,7 +204,7 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
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) {
//遍历出父id等于参数的idadd进子节点集合
if (StringUtils.equals(node.getParentId(), pid)) {
@ -269,7 +213,6 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
nodeList(apiNodes, node.getId(), list);
}
}
return list;
}
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.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<ApiScenarioModuleD
public List<ApiScenarioModuleDTO> getNodeTreeByProjectId(String projectId) {
List<ApiScenarioModuleDTO> nodes = extApiScenarioModuleMapper.getNodeTreeByProjectId(projectId);
ApiScenarioRequest request = new ApiScenarioRequest();
return getApiScenarioModuleDTOS(projectId, nodes, request);
}
private List<ApiScenarioModuleDTO> getApiScenarioModuleDTOS(String projectId, List<ApiScenarioModuleDTO> nodes, ApiScenarioRequest request) {
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<>();
filters.put("status", list);
filters.put(ApiTestConstants.STATUS, ApiTestConstants.STATUS_ALL);
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 -> {
for (ApiScenarioModuleDTO node : nodes) {
List<String> moduleIds = new ArrayList<>();
moduleIds = this.nodeList(nodes, node.getId(), moduleIds);
moduleIds.add(node.getId());
@ -95,47 +86,13 @@ public class ApiScenarioModuleService extends NodeTreeService<ApiScenarioModuleD
}
}
node.setCaseNum(countNum);
});
}
return getNodeTrees(nodes);
}
public List<ApiScenarioModuleDTO> getNodeTreeByProjectId(String projectId, ApiScenarioRequest request) {
List<ApiScenarioModuleDTO> nodes = extApiScenarioModuleMapper.getNodeTreeByProjectId(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<>();
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);
return getApiScenarioModuleDTOS(projectId, nodes, request);
}
public List<ApiScenarioModuleDTO> getTrashNodeTreeByProjectId(String projectId) {