From ca1217b72442356743342d8daebb64bff00b1d9a Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Fri, 29 Dec 2023 17:49:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E6=8A=A5=E5=91=8A=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sdk/constants/ApiReportType.java | 6 ++ .../sdk/dto/api/result/ProcessResultDTO.java | 26 ++++++ .../sdk/dto/api/result/TaskResult.java | 18 +++- .../sdk/dto/api/task/TaskRequest.java | 7 ++ .../sdk/util/RetryResultUtils.java | 82 +++++++++++++++++++ 5 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/ApiReportType.java create mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/ProcessResultDTO.java create mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/util/RetryResultUtils.java diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/ApiReportType.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/ApiReportType.java new file mode 100644 index 0000000000..9f692ae4d7 --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/ApiReportType.java @@ -0,0 +1,6 @@ +package io.metersphere.sdk.constants; + +public enum ApiReportType { + // 集成报告,独立报告 + INTEGRATED, INDEPENDENT +} diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/ProcessResultDTO.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/ProcessResultDTO.java new file mode 100644 index 0000000000..0f7513d9c5 --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/ProcessResultDTO.java @@ -0,0 +1,26 @@ +package io.metersphere.sdk.dto.api.result; + +import lombok.Data; + +@Data +public class ProcessResultDTO { + private String status; + private long scenarioSuccess; + private int scenarioTotal; + + public void computerTotal(int total) { + this.scenarioTotal += total; + } + + public void computerSuccess(long success) { + this.scenarioSuccess += success; + } + + public long computerPassRate() { + if (this.scenarioTotal == 0) return 0; + return this.scenarioSuccess / this.scenarioTotal; + } +} + + + diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/TaskResult.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/TaskResult.java index 8cc8516d57..59522359d8 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/TaskResult.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/result/TaskResult.java @@ -5,7 +5,6 @@ import lombok.Data; import java.io.Serial; import java.io.Serializable; import java.util.List; -import java.util.Map; @Data public class TaskResult implements Serializable { @@ -27,6 +26,19 @@ public class TaskResult implements Serializable { private String runningDebugSampler; private Boolean hasEnded; private boolean retryEnable; - private Map arbitraryData; - Map> fakeErrorMap; + private String projectId; + private String environmentId; + /** + * 执行过程状态 + */ + private ProcessResultDTO processResultDTO; + /** + * 资源类型 + * ApiResourceType + */ + private String resourceType; + /** + * 环境变量处理信息 + */ + List environmentList; } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/TaskRequest.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/TaskRequest.java index bb3e226db1..450497df76 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/TaskRequest.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/TaskRequest.java @@ -1,10 +1,12 @@ package io.metersphere.sdk.dto.api.task; +import io.metersphere.sdk.dto.api.result.MsRegexDTO; import lombok.Data; import java.io.Serial; import java.io.Serializable; import java.util.List; +import java.util.Map; /** * 任务请求参数数据 @@ -51,5 +53,10 @@ public class TaskRequest implements Serializable { * 执行时下载文件后,按原文件命名 */ private List refFiles; + /** + * 误报规则 + */ + Map> fakeErrorMap; + // TODO 其它执行参数 } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/RetryResultUtils.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/RetryResultUtils.java new file mode 100644 index 0000000000..a46c9711ae --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/RetryResultUtils.java @@ -0,0 +1,82 @@ +package io.metersphere.sdk.util; + +import io.metersphere.sdk.dto.api.result.RequestResult; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.jmeter.samplers.SampleResult; + +import java.util.*; +import java.util.stream.Collectors; + +/** + * 重试报告处理util + */ +public class RetryResultUtils { + public final static String RETRY = "MsRetry_"; + public final static String RETRY_CN = "RETRY"; + public final static String RETRY_FIRST_CN = "RETRY_FIRST"; + public final static String MS_CLEAR_LOOPS_VAR = "MS_CLEAR_LOOPS_VAR_"; + public final static int RETRY_RES_NUM = 11; + + /** + * 合并掉重试结果;保留最后十次重试结果 + */ + public static void mergeRetryResults(List results) { + if (CollectionUtils.isNotEmpty(results)) { + Map> resultMap = results.stream().collect(Collectors.groupingBy(RequestResult::getResourceId)); + List list = new LinkedList<>(); + resultMap.forEach((k, v) -> { + if (CollectionUtils.isNotEmpty(v)) { + // 校验是否含重试结果 + List isRetryResults = v + .stream() + .filter(c -> StringUtils.isNotEmpty(c.getName()) && c.getName().startsWith(RETRY)) + .collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(isRetryResults)) { + // 取最后执行的10 条 + if (v.size() > 10) { + v.sort(Comparator.comparing(RequestResult::getResourceId)); + RequestResult sampleResult = v.getFirst(); + List topTens = v.subList(v.size() - RETRY_RES_NUM, v.size()); + topTens.set(0, sampleResult); + assembleName(topTens); + list.addAll(topTens); + } else { + assembleName(v); + list.addAll(v); + } + } else { + // 成功的结果 + list.addAll(v); + } + } + }); + results.clear(); + results.addAll(list); + } + } + + private static void assembleName(List list) { + // 名称排序处理 + for (int i = 0; i < list.size(); i++) { + list.get(i).setName(list.get(i).getName().replaceAll(RETRY, RETRY_CN)); + if (list.get(i).getName().endsWith("_")) { + list.get(i).setName(list.get(i).getName().substring(0, list.get(i).getName().length() - 1)); + } + if (i == 0) { + list.get(i).setName(StringUtils.isNotEmpty(list.get(i).getName()) + ? RETRY_FIRST_CN + "_" + list.get(i).getName() : RETRY_FIRST_CN); + } + } + } + + public static List clearLoops(List results) { + if (CollectionUtils.isNotEmpty(results)) { + return results.stream().filter(sampleResult -> + StringUtils.isNotEmpty(sampleResult.getSampleLabel()) + && !sampleResult.getSampleLabel().startsWith(MS_CLEAR_LOOPS_VAR)) + .collect(Collectors.toList()); + } + return results; + } +}