fix(接口测试): 修复执行接口测试时由于拿不到所属项目ID引起的统计缺陷

--bug=1027143 --user=宋天阳 【接口测试】接口测试首页-接口用例数量统计中的本周执行次数和历史执行次数没有统计数据
https://www.tapd.cn/55049933/s/1383214
This commit is contained in:
song-tianyang 2023-06-19 14:42:45 +08:00 committed by 建国
parent 6a075e56f1
commit aa4ad894f9
4 changed files with 37 additions and 7 deletions

View File

@ -31,6 +31,7 @@ import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.service.RemakeReportService;
import io.metersphere.utils.LoggerUtil;
import jakarta.annotation.Resource;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jorphan.collections.HashTree;
import org.json.JSONObject;
@ -99,9 +100,13 @@ public class ApiCaseSerialService {
protected void updateDefinitionExecResultToRunning(ApiExecutionQueueDetail queue, JmeterRunRequestDTO runRequest) {
ApiDefinitionExecResultWithBLOBs execResult = apiDefinitionExecResultMapper.selectByPrimaryKey(queue.getReportId());
if (execResult != null) {
runRequest.setExtendedParameters(new HashMap<String, Object>() {{
this.put(CommonConstants.USER_ID, execResult.getUserId());
}});
if (MapUtils.isNotEmpty(runRequest.getExtendedParameters())) {
runRequest.getExtendedParameters().put(CommonConstants.USER_ID, execResult.getUserId());
} else {
runRequest.setExtendedParameters(new HashMap<String, Object>() {{
this.put(CommonConstants.USER_ID, execResult.getUserId());
}});
}
execResult.setStartTime(System.currentTimeMillis());
execResult.setStatus(ApiReportStatus.RUNNING.name());
apiDefinitionExecResultMapper.updateByPrimaryKeySelective(execResult);

View File

@ -27,6 +27,7 @@ import io.metersphere.commons.utils.*;
import io.metersphere.dto.*;
import io.metersphere.environment.service.BaseEnvironmentService;
import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.service.MsHashTreeService;
import io.metersphere.service.RemakeReportService;
import io.metersphere.service.SystemParameterService;
import io.metersphere.service.definition.TcpApiParamService;
@ -138,9 +139,6 @@ public class ApiExecuteService {
// 调用执行方法
runRequest.setHashTree(jmeterHashTree);
}
if (MapUtils.isNotEmpty(extendedParameters)) {
runRequest.setExtendedParameters(extendedParameters);
}
if (StringUtils.isNotBlank(runModeConfigDTO.getResourcePoolId())) {
runRequest.setPoolId(runModeConfigDTO.getResourcePoolId());
BooleanPool pool = GenerateHashTreeUtil.isResourcePool(runModeConfigDTO.getResourcePoolId());
@ -149,6 +147,14 @@ public class ApiExecuteService {
runRequest.setPlatformUrl(GenerateHashTreeUtil.getPlatformUrl(baseInfo, runRequest, null));
}
String projectId = testCase.getProjectId();
if (MapUtils.isNotEmpty(extendedParameters)) {
extendedParameters.put(MsHashTreeService.PROJECT_ID, projectId);
runRequest.setExtendedParameters(extendedParameters);
} else {
runRequest.setExtendedParameters(new HashMap<>() {{
this.put(MsHashTreeService.PROJECT_ID, projectId);
}});
}
runRequest.setFakeErrorMap(ApiFakeErrorUtil.get(new ArrayList<>() {{
this.add(projectId);
}}));
@ -251,6 +257,7 @@ public class ApiExecuteService {
this.put(ExtendedParameter.SYNC_STATUS, request.isSyncResult());
this.put(CommonConstants.USER_ID, SessionUtils.getUser().getId());
this.put("userName", SessionUtils.getUser().getName());
this.put(MsHashTreeService.PROJECT_ID, request.getProjectId());
}});
// 开始执行
if (StringUtils.isNotEmpty(request.getConfig().getResourcePoolId())) {

View File

@ -20,6 +20,7 @@ import io.metersphere.dto.RequestResult;
import io.metersphere.dto.ResultDTO;
import io.metersphere.notice.sender.NoticeModel;
import io.metersphere.notice.service.NoticeSendService;
import io.metersphere.service.MsHashTreeService;
import io.metersphere.service.RedisTemplateService;
import io.metersphere.service.ServiceUtils;
import io.metersphere.utils.LoggerUtil;
@ -138,6 +139,9 @@ public class ApiDefinitionExecResultService {
if (!StringUtils.startsWithAny(item.getName(), "PRE_PROCESSOR_ENV_", "POST_PROCESSOR_ENV_")) {
ApiDefinitionExecResult result = this.editResult(item, dto.getReportId(), dto.getConsole(), dto.getRunMode(), dto.getTestId(), definitionExecResultMapper);
if (result != null) {
if (StringUtils.isBlank(result.getProjectId()) && dto.getExtendedParameters().containsKey(MsHashTreeService.PROJECT_ID)) {
result.setProjectId(this.getProjectIdByResultDTO(dto.getExtendedParameters().get(MsHashTreeService.PROJECT_ID).toString()));
}
result.setResourceId(dto.getTestId());
apiExecutionInfoService.insertExecutionInfo(result);
// 批量更新关联关系状态
@ -164,6 +168,18 @@ public class ApiDefinitionExecResultService {
}
}
private String getProjectIdByResultDTO(String projectIdFromResultDTO) {
String returnStr = projectIdFromResultDTO;
if (StringUtils.startsWith(projectIdFromResultDTO, "[") && StringUtils.endsWith(projectIdFromResultDTO, "]")) {
try {
List<String> projectIdList = JSON.parseArray(projectIdFromResultDTO, String.class);
returnStr = projectIdList.get(0);
} catch (Exception ignore) {
}
}
return returnStr;
}
private User getUser(ResultDTO dto, ApiDefinitionExecResult result) {
User user = null;
if (MapUtils.isNotEmpty(dto.getExtendedParameters())) {

View File

@ -6,6 +6,7 @@ import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs;
import io.metersphere.base.domain.EnvironmentGroup;
import io.metersphere.commons.constants.OperLogConstants;
import io.metersphere.commons.constants.OperLogModule;
import io.metersphere.commons.constants.PermissionConstants;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.*;
import io.metersphere.environment.dto.*;
@ -18,6 +19,7 @@ import io.metersphere.i18n.Translator;
import io.metersphere.log.annotation.MsAuditLog;
import io.metersphere.request.EnvironmentRequest;
import jakarta.annotation.Resource;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONArray;
import org.json.JSONObject;
@ -69,7 +71,7 @@ public class TestEnvironmentController {
}
@PostMapping("/add")
@RequiresPermissions("PROJECT_ENVIRONMENT:READ+CREATE")
@RequiresPermissions(value = {PermissionConstants.PROJECT_ENVIRONMENT_READ_CREATE, PermissionConstants.PROJECT_ENVIRONMENT_READ_COPY}, logical = Logical.OR)
@MsAuditLog(module = OperLogModule.PROJECT_ENVIRONMENT_SETTING, type = OperLogConstants.CREATE, title = "#apiTestEnvironmentWithBLOBs.name", project = "#apiTestEnvironmentWithBLOBs.projectId", msClass = BaseEnvironmentService.class)
public String create(@RequestPart("request") TestEnvironmentDTO apiTestEnvironmentWithBLOBs, @RequestPart(value = "files", required = false) List<MultipartFile> sslFiles, @RequestPart(value = "variablesFiles", required = false) List<MultipartFile> variableFile) {
checkParams(apiTestEnvironmentWithBLOBs);