feat(接口测试): API报告处理

This commit is contained in:
fit2-zhao 2024-01-02 17:14:54 +08:00 committed by Craftsman
parent 64d21031e2
commit 6493eecfa7
6 changed files with 118 additions and 8 deletions

View File

@ -1,11 +1,16 @@
package io.metersphere.sdk.dto.api.notice;
import io.metersphere.sdk.constants.ApiExecuteResourceType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serial;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ApiNoticeDTO implements java.io.Serializable {
@Serial
private static final long serialVersionUID = 1L;
@ -15,6 +20,6 @@ public class ApiNoticeDTO implements java.io.Serializable {
private String reportStatus;
private String userId;
private String projectId;
private String environmentId;
private List<String> environmentIds;
private String reportId;
}

View File

@ -26,8 +26,14 @@ public class TaskResult implements Serializable {
private String runningDebugSampler;
private Boolean hasEnded;
private boolean retryEnable;
/**
* 项目id
*/
private String projectId;
private String environmentId;
/**
* 执行环境id
*/
private List<String> environmentIds;
/**
* 执行过程状态
*/

View File

@ -58,5 +58,14 @@ public class TaskRequest implements Serializable {
*/
Map<String, List<MsRegexDTO>> fakeErrorMap;
/**
* 项目id
*/
private String projectId;
/**
* 执行环境id
*/
private List<String> environmentIds;
// TODO 其它执行参数
}

View File

@ -0,0 +1,83 @@
package io.metersphere.sdk.util;
import io.metersphere.sdk.constants.ApiReportStatus;
import io.metersphere.sdk.dto.api.result.ProcessResultDTO;
import io.metersphere.sdk.dto.api.result.RequestResult;
import io.metersphere.sdk.dto.api.result.TaskResult;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ReportStatusUtils {
public static List<RequestResult> filterRetryResults(List<RequestResult> results) {
List<RequestResult> list = new LinkedList<>();
if (CollectionUtils.isNotEmpty(results)) {
Map<String, List<RequestResult>> resultMap = results.stream().collect(Collectors.groupingBy(RequestResult::getResourceId));
resultMap.forEach((k, v) -> {
if (CollectionUtils.isNotEmpty(v)) {
// 校验是否含重试结果
List<RequestResult> isRetryResults = v.stream().filter(c ->
StringUtils.isNotEmpty(c.getName()) && c.getName().startsWith(RetryResultUtils.RETRY_CN))
.collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(isRetryResults)) {
list.add(isRetryResults.getLast());
} else {
// 成功的结果
list.addAll(v);
}
}
});
}
return list;
}
/**
* 返回正确的报告状态
*
* @param dto jmeter返回
*/
public static ProcessResultDTO getStatus(TaskResult dto, ProcessResultDTO resultVO) {
resultVO.computerTotal(dto.getRequestResults().size());
resultVO.computerSuccess(dto.getRequestResults().stream().filter(requestResult -> StringUtils.equalsIgnoreCase(requestResult.getStatus(), ApiReportStatus.SUCCESS.name())).count());
if (StringUtils.equals(resultVO.getStatus(), ApiReportStatus.ERROR.name())) {
return resultVO;
}
if (ObjectUtils.isNotEmpty(dto.getProcessResultDTO())) {
// 资源池执行整体传输失败单条传输内容获取资源池执行统计的状态
resultVO.setStatus(dto.getProcessResultDTO().getStatus());
}
// 过滤掉重试结果后进行统计
List<RequestResult> requestResults = filterRetryResults(dto.getRequestResults());
long errorSize = requestResults.stream().filter(requestResult ->
StringUtils.equalsIgnoreCase(requestResult.getStatus(), ApiReportStatus.ERROR.name())).count();
// 误报
long errorReportResultSize = dto.getRequestResults().stream().filter(
requestResult -> StringUtils.equalsIgnoreCase(requestResult.getStatus(), ApiReportStatus.FAKE_ERROR.name())).count();
// 默认状态
String status = dto.getRequestResults().isEmpty() && StringUtils.isEmpty(resultVO.getStatus())
? ApiReportStatus.PENDING.name()
: StringUtils.defaultIfEmpty(resultVO.getStatus(), ApiReportStatus.SUCCESS.name());
if (errorSize > 0) {
status = ApiReportStatus.ERROR.name();
} else if (errorReportResultSize > 0) {
status = ApiReportStatus.FAKE_ERROR.name();
}
resultVO.setStatus(status);
return resultVO;
}
public static ProcessResultDTO computedProcess(TaskResult dto) {
ProcessResultDTO result = dto.getProcessResultDTO();
result = getStatus(dto, result);
if (result.getScenarioTotal() > 0 && result.getScenarioTotal() == result.getScenarioSuccess()) {
result.setStatus(ApiReportStatus.SUCCESS.name());
}
return result;
}
}

View File

@ -23,6 +23,7 @@ import io.metersphere.system.service.NoticeSendService;
import io.metersphere.system.service.SystemParameterService;
import jakarta.annotation.Resource;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
@ -94,9 +95,13 @@ public class ApiReportSendNoticeService {
Map<String, Object> paramMap = new HashMap(beanMap);
paramMap.put("operator", user != null ? user.getName() : "");
paramMap.put("status", noticeDTO.getReportStatus());
Environment environment = environmentMapper.selectByPrimaryKey(noticeDTO.getEnvironmentId());
if (environment != null) {
paramMap.put("environment", environment.getName());
// TODO 暂时取一个环境处理
if (CollectionUtils.isNotEmpty(noticeDTO.getEnvironmentIds())) {
Environment environment = environmentMapper.selectByPrimaryKey(noticeDTO.getEnvironmentIds().getFirst());
if (environment != null) {
paramMap.put("environment", environment.getName());
}
} else {
paramMap.put("environment", "未配置");
}

View File

@ -12,6 +12,8 @@ import org.mockito.InjectMocks;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@AutoConfigureMockMvc
@ -31,7 +33,7 @@ public class MessageListenerTest {
api.setReportStatus("exampleReportStatus");
api.setUserId("exampleUserId");
api.setProjectId("exampleProjectId");
api.setEnvironmentId("exampleEnvironmentId");
api.setEnvironmentIds(new ArrayList<>(){{this.add("exampleEnvironmentId");}});
api.setReportId("exampleReportId");
ConsumerRecord<Object, String> record = new ConsumerRecord<>(KafkaTopicConstants.API_REPORT_TASK_TOPIC, 0, 0, "123", JSON.toJSONString(api));
@ -47,7 +49,7 @@ public class MessageListenerTest {
scenario.setReportStatus("exampleReportStatus");
scenario.setUserId("exampleUserId");
scenario.setProjectId("exampleProjectId");
scenario.setEnvironmentId("exampleEnvironmentId");
api.setEnvironmentIds(new ArrayList<>(){{this.add("exampleEnvironmentId");}});
scenario.setReportId("exampleReportId");
ConsumerRecord<Object, String> scenarioRecord = new ConsumerRecord<>(KafkaTopicConstants.API_REPORT_TASK_TOPIC, 0, 0, "123", JSON.toJSONString(scenario));
@ -63,7 +65,7 @@ public class MessageListenerTest {
testCase.setReportStatus("exampleReportStatus");
testCase.setUserId("exampleUserId");
testCase.setProjectId("exampleProjectId");
testCase.setEnvironmentId("exampleEnvironmentId");
api.setEnvironmentIds(new ArrayList<>(){{this.add("exampleEnvironmentId");}});
testCase.setReportId("exampleReportId");
ConsumerRecord<Object, String> testCaseRecord = new ConsumerRecord<>(KafkaTopicConstants.API_REPORT_TASK_TOPIC, 0, 0, "123", JSON.toJSONString(testCase));