diff --git a/backend/src/main/java/io/metersphere/performance/base/GranularityData.java b/backend/src/main/java/io/metersphere/performance/base/GranularityData.java new file mode 100644 index 0000000000..07aa3de863 --- /dev/null +++ b/backend/src/main/java/io/metersphere/performance/base/GranularityData.java @@ -0,0 +1,39 @@ +package io.metersphere.performance.base; + +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +public class GranularityData { + + public static List dataList = new ArrayList<>(); + + static { + dataList.add(new Data(0, 100, 1)); + dataList.add(new Data(101, 500, 5)); + dataList.add(new Data(501, 1000, 10)); + dataList.add(new Data(1001, 3000, 30)); + dataList.add(new Data(3001, 6000, 60)); + dataList.add(new Data(6001, 30000, 300)); + dataList.add(new Data(30001, 60000, 600)); + dataList.add(new Data(60001, 180000, 1800)); + dataList.add(new Data(180001, 360000, 3600)); + dataList.add(new Data(360000, Integer.MAX_VALUE, 3600)); + } + + @Getter + @Setter + public static class Data { + private Integer start; + private Integer end; + private Integer granularity; + + Data(Integer start, Integer end, Integer granularity) { + this.start = start; + this.end = end; + this.granularity = granularity; + } + } +} diff --git a/backend/src/main/java/io/metersphere/performance/service/MetricQueryService.java b/backend/src/main/java/io/metersphere/performance/service/MetricQueryService.java index 74e60c75ef..fc4f536036 100644 --- a/backend/src/main/java/io/metersphere/performance/service/MetricQueryService.java +++ b/backend/src/main/java/io/metersphere/performance/service/MetricQueryService.java @@ -15,6 +15,7 @@ import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.DateUtils; import io.metersphere.commons.utils.LogUtil; import io.metersphere.dto.NodeDTO; +import io.metersphere.performance.base.GranularityData; import io.metersphere.performance.base.ReportTimeInfo; import io.metersphere.performance.controller.request.MetricDataRequest; import io.metersphere.performance.controller.request.MetricQuery; @@ -57,13 +58,13 @@ public class MetricQueryService { List metricDataList = new ArrayList<>(); long endTime = metricRequest.getEndTime(); long startTime = metricRequest.getStartTime(); - int step = metricRequest.getStep(); long reliableEndTime; if (endTime > System.currentTimeMillis()) { reliableEndTime = System.currentTimeMillis(); } else { reliableEndTime = endTime; } + int step = getGranularity(startTime, reliableEndTime); Optional.ofNullable(metricRequest.getMetricDataQueries()).ifPresent(metricDataQueries -> metricDataQueries.forEach(query -> { String promQL = query.getPromQL(); @@ -256,4 +257,21 @@ public class MetricQueryService { return result; } + + private Integer getGranularity(long start, long end) { + Integer granularity = 15; + try { + int duration = (int) (end - start) / 1000; + Optional dataOptional = GranularityData.dataList.stream() + .filter(data -> duration >= data.getStart() && duration <= data.getEnd()) + .findFirst(); + if (dataOptional.isPresent()) { + GranularityData.Data data = dataOptional.get(); + granularity = data.getGranularity(); + } + } catch (Exception e) { + LogUtil.error(e.getMessage() ,e); + } + return granularity; + } }