From 86e05bc381b811c5245ae6aea83f7da6353f1ca2 Mon Sep 17 00:00:00 2001 From: peng-yongsheng <8082209@qq.com> Date: Wed, 31 Jan 2018 14:57:44 +0800 Subject: [PATCH] Provide the getServerTPSTrend query. --- .../storage/dao/IInstanceMetricUIDAO.java | 6 +- .../storage/ui/common/ThroughputTrend.java | 8 +++ .../storage/utils/DurationPoint.java | 40 +++++++++++ .../storage/es/dao/InstanceMetricEsUIDAO.java | 37 ++++++----- .../storage/h2/dao/InstanceMetricH2UIDAO.java | 42 +++++------- .../apm/collector/ui/query/ServerQuery.java | 6 +- .../ui/service/InstanceJVMService.java | 1 - .../collector/ui/service/ServerService.java | 14 +++- .../apm/collector/ui/utils/DurationUtils.java | 38 ++++++++--- .../ui/utils/DurationUtilsTestCase.java | 66 +++++++++++++++++-- 10 files changed, 192 insertions(+), 66 deletions(-) create mode 100644 apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/utils/DurationPoint.java diff --git a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/dao/IInstanceMetricUIDAO.java b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/dao/IInstanceMetricUIDAO.java index a2df9ff98..5d8603ecf 100644 --- a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/dao/IInstanceMetricUIDAO.java +++ b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/dao/IInstanceMetricUIDAO.java @@ -18,10 +18,10 @@ package org.apache.skywalking.apm.collector.storage.dao; -import com.google.gson.JsonArray; import java.util.List; import org.apache.skywalking.apm.collector.storage.base.dao.DAO; import org.apache.skywalking.apm.collector.storage.ui.common.Step; +import org.apache.skywalking.apm.collector.storage.utils.DurationPoint; /** * @author peng-yongsheng @@ -31,11 +31,11 @@ public interface IInstanceMetricUIDAO extends DAO { long getTpsMetric(int instanceId, long timeBucket); - JsonArray getTpsMetric(int instanceId, long startTimeBucket, long endTimeBucket); + List getServerTPSTrend(int instanceId, Step step, List durationPoints); long getRespTimeMetric(int instanceId, long timeBucket); - List getResponseTimeTrend(int instanceId, Step step, Long[] timeBuckets); + List getResponseTimeTrend(int instanceId, Step step, List durationPoints); class InstanceMetric { private final int instanceId; diff --git a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/common/ThroughputTrend.java b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/common/ThroughputTrend.java index e6cf37b99..f20866964 100644 --- a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/common/ThroughputTrend.java +++ b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/common/ThroughputTrend.java @@ -25,4 +25,12 @@ import java.util.List; */ public class ThroughputTrend { private List trendList; + + public List getTrendList() { + return trendList; + } + + public void setTrendList(List trendList) { + this.trendList = trendList; + } } diff --git a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/utils/DurationPoint.java b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/utils/DurationPoint.java new file mode 100644 index 000000000..a8f5ecebf --- /dev/null +++ b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/utils/DurationPoint.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.collector.storage.utils; + +/** + * @author peng-yongsheng + */ +public class DurationPoint { + private long point; + private long secondsBetween; + + public DurationPoint(long point, long secondsBetween) { + this.point = point; + this.secondsBetween = secondsBetween; + } + + public long getPoint() { + return point; + } + + public long getSecondsBetween() { + return secondsBetween; + } +} diff --git a/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/apache/skywalking/apm/collector/storage/es/dao/InstanceMetricEsUIDAO.java b/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/apache/skywalking/apm/collector/storage/es/dao/InstanceMetricEsUIDAO.java index d997d0314..d0bf73f1c 100644 --- a/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/apache/skywalking/apm/collector/storage/es/dao/InstanceMetricEsUIDAO.java +++ b/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/apache/skywalking/apm/collector/storage/es/dao/InstanceMetricEsUIDAO.java @@ -18,7 +18,6 @@ package org.apache.skywalking.apm.collector.storage.es.dao; -import com.google.gson.JsonArray; import java.util.LinkedList; import java.util.List; import org.apache.skywalking.apm.collector.client.elasticsearch.ElasticSearchClient; @@ -28,6 +27,7 @@ import org.apache.skywalking.apm.collector.storage.es.base.dao.EsDAO; import org.apache.skywalking.apm.collector.storage.table.MetricSource; import org.apache.skywalking.apm.collector.storage.table.instance.InstanceMetricTable; import org.apache.skywalking.apm.collector.storage.ui.common.Step; +import org.apache.skywalking.apm.collector.storage.utils.DurationPoint; import org.apache.skywalking.apm.collector.storage.utils.TimePyramidTableNameBuilder; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.get.MultiGetItemResponse; @@ -83,27 +83,28 @@ public class InstanceMetricEsUIDAO extends EsDAO implements IInstanceMetricUIDAO return 0; } - @Override public JsonArray getTpsMetric(int instanceId, long startTimeBucket, long endTimeBucket) { + @Override public List getServerTPSTrend(int instanceId, Step step, List durationPoints) { MultiGetRequestBuilder prepareMultiGet = getClient().prepareMultiGet(); + String tableName = TimePyramidTableNameBuilder.build(step, InstanceMetricTable.TABLE); - long timeBucket = startTimeBucket; - do { - String id = timeBucket + Const.ID_SPLIT + instanceId; - prepareMultiGet.add(InstanceMetricTable.TABLE, InstanceMetricTable.TABLE_TYPE, id); -// timeBucket = TimeBucketUtils.INSTANCE.addSecondForSecondTimeBucket(TimeBucketUtils.TimeBucketType.SECOND, timeBucket, 1); - } - while (timeBucket <= endTimeBucket); + durationPoints.forEach(durationPoint -> { + String id = durationPoint.getPoint() + Const.ID_SPLIT + instanceId + Const.ID_SPLIT + MetricSource.Callee.getValue(); + prepareMultiGet.add(tableName, InstanceMetricTable.TABLE_TYPE, id); + }); - JsonArray metrics = new JsonArray(); + List throughputTrend = new LinkedList<>(); MultiGetResponse multiGetResponse = prepareMultiGet.get(); - for (MultiGetItemResponse response : multiGetResponse.getResponses()) { + + for (int i = 0; i < multiGetResponse.getResponses().length; i++) { + MultiGetItemResponse response = multiGetResponse.getResponses()[i]; if (response.getResponse().isExists()) { - metrics.add(((Number)response.getResponse().getSource().get(InstanceMetricTable.COLUMN_TRANSACTION_CALLS)).longValue()); + long callTimes = ((Number)response.getResponse().getSource().get(InstanceMetricTable.COLUMN_TRANSACTION_CALLS)).longValue(); + throughputTrend.add((int)(callTimes / durationPoints.get(i).getSecondsBetween())); } else { - metrics.add(0); + throughputTrend.add(0); } } - return metrics; + return throughputTrend; } @Override public long getRespTimeMetric(int instanceId, long timeBucket) { @@ -118,14 +119,14 @@ public class InstanceMetricEsUIDAO extends EsDAO implements IInstanceMetricUIDAO return 0; } - @Override public List getResponseTimeTrend(int instanceId, Step step, Long[] timeBuckets) { + @Override public List getResponseTimeTrend(int instanceId, Step step, List durationPoints) { MultiGetRequestBuilder prepareMultiGet = getClient().prepareMultiGet(); String tableName = TimePyramidTableNameBuilder.build(step, InstanceMetricTable.TABLE); - for (long timeBucket : timeBuckets) { - String id = timeBucket + Const.ID_SPLIT + instanceId + Const.ID_SPLIT + MetricSource.Callee.getValue(); + durationPoints.forEach(durationPoint -> { + String id = durationPoint.getPoint() + Const.ID_SPLIT + instanceId + Const.ID_SPLIT + MetricSource.Callee.getValue(); prepareMultiGet.add(tableName, InstanceMetricTable.TABLE_TYPE, id); - } + }); List responseTimeTrends = new LinkedList<>(); MultiGetResponse multiGetResponse = prepareMultiGet.get(); diff --git a/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/apache/skywalking/apm/collector/storage/h2/dao/InstanceMetricH2UIDAO.java b/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/apache/skywalking/apm/collector/storage/h2/dao/InstanceMetricH2UIDAO.java index d52f8f9ee..21d5b5bb3 100644 --- a/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/apache/skywalking/apm/collector/storage/h2/dao/InstanceMetricH2UIDAO.java +++ b/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/apache/skywalking/apm/collector/storage/h2/dao/InstanceMetricH2UIDAO.java @@ -18,10 +18,8 @@ package org.apache.skywalking.apm.collector.storage.h2.dao; -import com.google.gson.JsonArray; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import org.apache.skywalking.apm.collector.client.h2.H2Client; @@ -33,6 +31,7 @@ import org.apache.skywalking.apm.collector.storage.h2.base.dao.H2DAO; import org.apache.skywalking.apm.collector.storage.table.MetricSource; import org.apache.skywalking.apm.collector.storage.table.instance.InstanceMetricTable; import org.apache.skywalking.apm.collector.storage.ui.common.Step; +import org.apache.skywalking.apm.collector.storage.utils.DurationPoint; import org.apache.skywalking.apm.collector.storage.utils.TimePyramidTableNameBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -55,7 +54,7 @@ public class InstanceMetricH2UIDAO extends H2DAO implements IInstanceMetricUIDAO logger.info("the inst performance inst id = {}", instanceId); String sql = SqlBuilder.buildSql(GET_INSTANCE_METRIC_SQL, InstanceMetricTable.TABLE, InstanceMetricTable.COLUMN_INSTANCE_ID, InstanceMetricTable.COLUMN_TIME_BUCKET); StringBuilder builder = new StringBuilder(); - for (int i = 0; i < timeBuckets.length; i++) { + for (long timeBucket : timeBuckets) { builder.append("?,"); } builder.delete(builder.length() - 1, builder.length()); @@ -93,34 +92,29 @@ public class InstanceMetricH2UIDAO extends H2DAO implements IInstanceMetricUIDAO return 0; } - @Override public JsonArray getTpsMetric(int instanceId, long startTimeBucket, long endTimeBucket) { - logger.info("getTpsMetric instanceId = {}, startTimeBucket = {}, endTimeBucket = {}", instanceId, startTimeBucket, endTimeBucket); + @Override + public List getServerTPSTrend(int instanceId, Step step, List durationPoints) { H2Client client = getClient(); - String sql = SqlBuilder.buildSql(GET_TPS_METRIC_SQL, InstanceMetricTable.TABLE, InstanceMetricTable.COLUMN_ID); + String tableName = TimePyramidTableNameBuilder.build(step, InstanceMetricTable.TABLE); - long timeBucket = startTimeBucket; - List idList = new ArrayList<>(); - do { - String id = timeBucket + Const.ID_SPLIT + instanceId; -// timeBucket = TimeBucketUtils.INSTANCE.addSecondForSecondTimeBucket(TimeBucketUtils.TimeBucketType.SECOND, timeBucket, 1); - idList.add(id); - } - while (timeBucket <= endTimeBucket); + String sql = SqlBuilder.buildSql(GET_TPS_METRIC_SQL, tableName, InstanceMetricTable.COLUMN_ID); - JsonArray metrics = new JsonArray(); - idList.forEach(id -> { + List throughputTrend = new LinkedList<>(); + durationPoints.forEach(durationPoint -> { + String id = durationPoint.getPoint() + Const.ID_SPLIT + instanceId + Const.ID_SPLIT + MetricSource.Callee.getValue(); try (ResultSet rs = client.executeQuery(sql, new Object[] {id})) { if (rs.next()) { - long calls = rs.getLong(InstanceMetricTable.COLUMN_TRANSACTION_CALLS); - metrics.add(calls); + long callTimes = rs.getLong(InstanceMetricTable.COLUMN_TRANSACTION_CALLS); + throughputTrend.add((int)(callTimes / durationPoint.getSecondsBetween())); } else { - metrics.add(0); + throughputTrend.add(0); } } catch (SQLException | H2ClientException e) { logger.error(e.getMessage(), e); } }); - return metrics; + + return throughputTrend; } @Override public long getRespTimeMetric(int instanceId, long timeBucket) { @@ -139,15 +133,15 @@ public class InstanceMetricH2UIDAO extends H2DAO implements IInstanceMetricUIDAO return 0; } - @Override public List getResponseTimeTrend(int instanceId, Step step, Long[] timeBuckets) { + @Override public List getResponseTimeTrend(int instanceId, Step step, List durationPoints) { H2Client client = getClient(); String tableName = TimePyramidTableNameBuilder.build(step, InstanceMetricTable.TABLE); String sql = SqlBuilder.buildSql(GET_TPS_METRIC_SQL, tableName, InstanceMetricTable.COLUMN_ID); List responseTimeTrends = new LinkedList<>(); - for (long timeBucket : timeBuckets) { - String id = timeBucket + Const.ID_SPLIT + instanceId + Const.ID_SPLIT + MetricSource.Callee.getValue(); + durationPoints.forEach(durationPoint -> { + String id = durationPoint.getPoint() + Const.ID_SPLIT + instanceId + Const.ID_SPLIT + MetricSource.Callee.getValue(); try (ResultSet rs = client.executeQuery(sql, new Object[] {id})) { if (rs.next()) { long callTimes = rs.getLong(InstanceMetricTable.COLUMN_TRANSACTION_CALLS); @@ -161,7 +155,7 @@ public class InstanceMetricH2UIDAO extends H2DAO implements IInstanceMetricUIDAO } catch (SQLException | H2ClientException e) { logger.error(e.getMessage(), e); } - } + }); return responseTimeTrends; } } diff --git a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/query/ServerQuery.java b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/query/ServerQuery.java index e7a6f604b..c3c4af039 100644 --- a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/query/ServerQuery.java +++ b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/query/ServerQuery.java @@ -70,8 +70,10 @@ public class ServerQuery implements Query { return getServerService().getServerResponseTimeTrend(serverId, duration.getStep(), start, end); } - public ThroughputTrend getServerTPSTrend(int serverId, Duration duration) { - return null; + public ThroughputTrend getServerTPSTrend(int serverId, Duration duration) throws ParseException { + long start = DurationUtils.INSTANCE.exchangeToTimeBucket(duration.getStart()); + long end = DurationUtils.INSTANCE.exchangeToTimeBucket(duration.getEnd()); + return getServerService().getServerTPSTrend(serverId, duration.getStep(), start, end); } public CPUTrend getCPUTrend(int serverId, Duration duration) { diff --git a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/service/InstanceJVMService.java b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/service/InstanceJVMService.java index 182388026..779a31fbd 100644 --- a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/service/InstanceJVMService.java +++ b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/service/InstanceJVMService.java @@ -112,7 +112,6 @@ public class InstanceJVMService { } else if (metricType.toLowerCase().equals(MetricType.gc.name())) { metrics.add(MetricType.gc.name(), gcMetricDAO.getMetric(instanceId, startTimeBucket, endTimeBucket)); } else if (metricType.toLowerCase().equals(MetricType.tps.name())) { - metrics.add(MetricType.tps.name(), instanceMetricUIDAO.getTpsMetric(instanceId, startTimeBucket, endTimeBucket)); } else if (metricType.toLowerCase().equals(MetricType.resptime.name())) { } else if (metricType.toLowerCase().equals(MetricType.heapmemory.name())) { metrics.add(MetricType.heapmemory.name(), memoryMetricDAO.getMetric(instanceId, startTimeBucket, endTimeBucket, true)); diff --git a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/service/ServerService.java b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/service/ServerService.java index d7b3af08b..739760780 100644 --- a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/service/ServerService.java +++ b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/service/ServerService.java @@ -32,7 +32,9 @@ import org.apache.skywalking.apm.collector.storage.dao.IInstanceMetricUIDAO; import org.apache.skywalking.apm.collector.storage.dao.IInstanceUIDAO; import org.apache.skywalking.apm.collector.storage.ui.common.ResponseTimeTrend; import org.apache.skywalking.apm.collector.storage.ui.common.Step; +import org.apache.skywalking.apm.collector.storage.ui.common.ThroughputTrend; import org.apache.skywalking.apm.collector.storage.ui.server.AppServerInfo; +import org.apache.skywalking.apm.collector.storage.utils.DurationPoint; import org.apache.skywalking.apm.collector.ui.utils.DurationUtils; /** @@ -70,12 +72,20 @@ public class ServerService { public ResponseTimeTrend getServerResponseTimeTrend(int instanceId, Step step, long start, long end) throws ParseException { ResponseTimeTrend responseTimeTrend = new ResponseTimeTrend(); - Long[] timeBuckets = DurationUtils.INSTANCE.getDurationPoints(step, start, end); - List trends = instanceMetricUIDAO.getResponseTimeTrend(instanceId, step, timeBuckets); + List durationPoints = DurationUtils.INSTANCE.getDurationPoints(step, start, end); + List trends = instanceMetricUIDAO.getResponseTimeTrend(instanceId, step, durationPoints); responseTimeTrend.setTrendList(trends); return responseTimeTrend; } + public ThroughputTrend getServerTPSTrend(int instanceId, Step step, long start, long end) throws ParseException { + ThroughputTrend throughputTrend = new ThroughputTrend(); + List durationPoints = DurationUtils.INSTANCE.getDurationPoints(step, start, end); + List trends = instanceMetricUIDAO.getServerTPSTrend(instanceId, step, durationPoints); + throughputTrend.setTrendList(trends); + return throughputTrend; + } + private void buildAppServerInfo(List serverInfos) { serverInfos.forEach(serverInfo -> { if (StringUtils.isNotEmpty(serverInfo.getOsInfo())) { diff --git a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/utils/DurationUtils.java b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/utils/DurationUtils.java index 53a7b3793..ffeb3322c 100644 --- a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/utils/DurationUtils.java +++ b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/utils/DurationUtils.java @@ -26,6 +26,7 @@ import org.apache.skywalking.apm.collector.core.UnexpectedException; import org.apache.skywalking.apm.collector.core.util.Const; import org.apache.skywalking.apm.collector.core.util.TimeBucketUtils; import org.apache.skywalking.apm.collector.storage.ui.common.Step; +import org.apache.skywalking.apm.collector.storage.utils.DurationPoint; import org.joda.time.DateTime; import org.joda.time.Seconds; @@ -92,6 +93,23 @@ public enum DurationUtils { return Seconds.secondsBetween(new DateTime(startDate), new DateTime(endDate)).getSeconds(); } + public long secondsBetween(Step step, DateTime dateTime) throws ParseException { + switch (step) { + case MONTH: + return dateTime.dayOfMonth().getMaximumValue() * 24 * 60 * 60; + case DAY: + return 24 * 60 * 60; + case HOUR: + return 60 * 60; + case MINUTE: + return 60; + case SECOND: + return 1; + default: + return 1; + } + } + public DateTime parseToDateTime(Step step, long time) throws ParseException { DateTime dateTime = null; @@ -121,11 +139,11 @@ public enum DurationUtils { return dateTime; } - public Long[] getDurationPoints(Step step, long start, long end) throws ParseException { + public List getDurationPoints(Step step, long start, long end) throws ParseException { DateTime dateTime = parseToDateTime(step, start); - List durations = new LinkedList<>(); - durations.add(start); + List durations = new LinkedList<>(); + durations.add(new DurationPoint(start, secondsBetween(step, dateTime))); int i = 0; do { @@ -133,27 +151,27 @@ public enum DurationUtils { case MONTH: dateTime = dateTime.plusMonths(1); String timeBucket = TimeBucketUtils.MONTH_DATE_FORMAT.format(dateTime.toDate()); - durations.add(Long.valueOf(timeBucket)); + durations.add(new DurationPoint(Long.valueOf(timeBucket), secondsBetween(step, dateTime))); break; case DAY: dateTime = dateTime.plusDays(1); timeBucket = TimeBucketUtils.DAY_DATE_FORMAT.format(dateTime.toDate()); - durations.add(Long.valueOf(timeBucket)); + durations.add(new DurationPoint(Long.valueOf(timeBucket), secondsBetween(step, dateTime))); break; case HOUR: dateTime = dateTime.plusHours(1); timeBucket = TimeBucketUtils.HOUR_DATE_FORMAT.format(dateTime.toDate()); - durations.add(Long.valueOf(timeBucket)); + durations.add(new DurationPoint(Long.valueOf(timeBucket), secondsBetween(step, dateTime))); break; case MINUTE: dateTime = dateTime.plusMinutes(1); timeBucket = TimeBucketUtils.MINUTE_DATE_FORMAT.format(dateTime.toDate()); - durations.add(Long.valueOf(timeBucket)); + durations.add(new DurationPoint(Long.valueOf(timeBucket), secondsBetween(step, dateTime))); break; case SECOND: dateTime = dateTime.plusSeconds(1); timeBucket = TimeBucketUtils.SECOND_DATE_FORMAT.format(dateTime.toDate()); - durations.add(Long.valueOf(timeBucket)); + durations.add(new DurationPoint(Long.valueOf(timeBucket), secondsBetween(step, dateTime))); break; } i++; @@ -161,8 +179,8 @@ public enum DurationUtils { throw new UnexpectedException("Duration data error, step: " + step.name() + ", start: " + start + ", end: " + end); } } - while (end != durations.get(durations.size() - 1)); + while (end != durations.get(durations.size() - 1).getPoint()); - return durations.toArray(new Long[durations.size()]); + return durations; } } diff --git a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/ui/utils/DurationUtilsTestCase.java b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/ui/utils/DurationUtilsTestCase.java index 30abe75be..1ab46eaab 100644 --- a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/ui/utils/DurationUtilsTestCase.java +++ b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/ui/utils/DurationUtilsTestCase.java @@ -19,8 +19,10 @@ package org.apache.skywalking.apm.collector.ui.utils; import java.text.ParseException; +import java.util.List; import org.apache.skywalking.apm.collector.core.UnexpectedException; import org.apache.skywalking.apm.collector.storage.ui.common.Step; +import org.apache.skywalking.apm.collector.storage.utils.DurationPoint; import org.junit.Assert; import org.junit.Test; @@ -35,20 +37,72 @@ public class DurationUtilsTestCase { @Test public void testGetDurationPoints() throws ParseException { - Long[] durationPoints = DurationUtils.INSTANCE.getDurationPoints(Step.MONTH, 201710, 201803); - Assert.assertArrayEquals(new Long[] {201710L, 201711L, 201712L, 201801L, 201802L, 201803L}, durationPoints); + List durationPoints = DurationUtils.INSTANCE.getDurationPoints(Step.MONTH, 201710, 201803); + Assert.assertEquals(6, durationPoints.size()); + Assert.assertEquals(201710L, durationPoints.get(0).getPoint()); + Assert.assertEquals(2678400, durationPoints.get(0).getSecondsBetween()); + Assert.assertEquals(201711L, durationPoints.get(1).getPoint()); + Assert.assertEquals(2592000, durationPoints.get(1).getSecondsBetween()); + Assert.assertEquals(201712L, durationPoints.get(2).getPoint()); + Assert.assertEquals(2678400, durationPoints.get(2).getSecondsBetween()); + Assert.assertEquals(201801L, durationPoints.get(3).getPoint()); + Assert.assertEquals(2678400, durationPoints.get(3).getSecondsBetween()); + Assert.assertEquals(201802L, durationPoints.get(4).getPoint()); + Assert.assertEquals(2419200, durationPoints.get(4).getSecondsBetween()); + Assert.assertEquals(201803L, durationPoints.get(5).getPoint()); + Assert.assertEquals(2678400, durationPoints.get(5).getSecondsBetween()); durationPoints = DurationUtils.INSTANCE.getDurationPoints(Step.DAY, 20180129, 20180202); - Assert.assertArrayEquals(new Long[] {20180129L, 20180130L, 20180131L, 20180201L, 20180202L}, durationPoints); + Assert.assertEquals(5, durationPoints.size()); + Assert.assertEquals(20180129L, durationPoints.get(0).getPoint()); + Assert.assertEquals(86400, durationPoints.get(0).getSecondsBetween()); + Assert.assertEquals(20180130L, durationPoints.get(1).getPoint()); + Assert.assertEquals(86400, durationPoints.get(1).getSecondsBetween()); + Assert.assertEquals(20180131L, durationPoints.get(2).getPoint()); + Assert.assertEquals(86400, durationPoints.get(2).getSecondsBetween()); + Assert.assertEquals(20180201L, durationPoints.get(3).getPoint()); + Assert.assertEquals(86400, durationPoints.get(3).getSecondsBetween()); + Assert.assertEquals(20180202L, durationPoints.get(4).getPoint()); + Assert.assertEquals(86400, durationPoints.get(4).getSecondsBetween()); durationPoints = DurationUtils.INSTANCE.getDurationPoints(Step.HOUR, 2018012922, 2018013002); - Assert.assertArrayEquals(new Long[] {2018012922L, 2018012923L, 2018013000L, 2018013001L, 2018013002L}, durationPoints); + Assert.assertEquals(5, durationPoints.size()); + Assert.assertEquals(2018012922L, durationPoints.get(0).getPoint()); + Assert.assertEquals(3600, durationPoints.get(0).getSecondsBetween()); + Assert.assertEquals(2018012923L, durationPoints.get(1).getPoint()); + Assert.assertEquals(3600, durationPoints.get(1).getSecondsBetween()); + Assert.assertEquals(2018013000L, durationPoints.get(2).getPoint()); + Assert.assertEquals(3600, durationPoints.get(2).getSecondsBetween()); + Assert.assertEquals(2018013001L, durationPoints.get(3).getPoint()); + Assert.assertEquals(3600, durationPoints.get(3).getSecondsBetween()); + Assert.assertEquals(2018013002L, durationPoints.get(4).getPoint()); + Assert.assertEquals(3600, durationPoints.get(4).getSecondsBetween()); durationPoints = DurationUtils.INSTANCE.getDurationPoints(Step.MINUTE, 201801292258L, 201801292302L); - Assert.assertArrayEquals(new Long[] {201801292258L, 201801292259L, 201801292300L, 201801292301L, 201801292302L}, durationPoints); + Assert.assertEquals(5, durationPoints.size()); + Assert.assertEquals(201801292258L, durationPoints.get(0).getPoint()); + Assert.assertEquals(60, durationPoints.get(0).getSecondsBetween()); + Assert.assertEquals(201801292259L, durationPoints.get(1).getPoint()); + Assert.assertEquals(60, durationPoints.get(1).getSecondsBetween()); + Assert.assertEquals(201801292300L, durationPoints.get(2).getPoint()); + Assert.assertEquals(60, durationPoints.get(2).getSecondsBetween()); + Assert.assertEquals(201801292301L, durationPoints.get(3).getPoint()); + Assert.assertEquals(60, durationPoints.get(3).getSecondsBetween()); + Assert.assertEquals(201801292302L, durationPoints.get(4).getPoint()); + Assert.assertEquals(60, durationPoints.get(4).getSecondsBetween()); durationPoints = DurationUtils.INSTANCE.getDurationPoints(Step.SECOND, 20180129225858L, 20180129225902L); - Assert.assertArrayEquals(new Long[] {20180129225858L, 20180129225859L, 20180129225900L, 20180129225901L, 20180129225902L}, durationPoints); + Assert.assertEquals(5, durationPoints.size()); + Assert.assertEquals(20180129225858L, durationPoints.get(0).getPoint()); + Assert.assertEquals(1, durationPoints.get(0).getSecondsBetween()); + Assert.assertEquals(20180129225859L, durationPoints.get(1).getPoint()); + Assert.assertEquals(1, durationPoints.get(1).getSecondsBetween()); + Assert.assertEquals(20180129225900L, durationPoints.get(2).getPoint()); + Assert.assertEquals(1, durationPoints.get(2).getSecondsBetween()); + Assert.assertEquals(20180129225901L, durationPoints.get(3).getPoint()); + Assert.assertEquals(1, durationPoints.get(3).getSecondsBetween()); + Assert.assertEquals(20180129225902L, durationPoints.get(4).getPoint()); + Assert.assertEquals(1, durationPoints.get(4).getSecondsBetween()); } @Test(expected = UnexpectedException.class)