From f7bd3d443d0acd26e09f545b0b7722c6a5a94f58 Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Thu, 15 Apr 2021 11:55:04 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E6=9B=B4=E6=94=B9=E6=9F=A5=E8=AF=A2=E6=AD=A5=E9=95=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../performance/base/GranularityData.java | 39 +++++++++++++++++++ .../service/MetricQueryService.java | 20 +++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 backend/src/main/java/io/metersphere/performance/base/GranularityData.java 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; + } }