diff --git a/dao/src/main/java/org/thingsboard/server/dao/timeseries/BaseTimeseriesService.java b/dao/src/main/java/org/thingsboard/server/dao/timeseries/BaseTimeseriesService.java index a1137853ab..9eefcaae1e 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/timeseries/BaseTimeseriesService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/timeseries/BaseTimeseriesService.java @@ -295,7 +295,12 @@ public class BaseTimeseriesService implements TimeseriesService { throw new IncorrectParameterException("Incorrect ReadTsKvQuery. Aggregation can't be empty"); } if (!Aggregation.NONE.equals(query.getAggregation())) { - long step = Math.max(query.getInterval(), 1000); + long interval = query.getInterval(); + if (interval < 1) { + throw new IncorrectParameterException("Invalid TsKvQuery: 'interval' must be greater than 0, but got " + interval + + ". Please check your query parameters and ensure 'endTs' is greater than 'startTs' or increase 'interval'."); + } + long step = Math.max(interval, 1000); long intervalCounts = (query.getEndTs() - query.getStartTs()) / step; if (intervalCounts > maxTsIntervals || intervalCounts < 0) { throw new IncorrectParameterException("Incorrect TsKvQuery. Number of intervals is to high - " + intervalCounts + ". " + diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/timeseries/BaseTimeseriesServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/timeseries/BaseTimeseriesServiceTest.java index a070de4d82..6a38159804 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/timeseries/BaseTimeseriesServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/timeseries/BaseTimeseriesServiceTest.java @@ -45,6 +45,7 @@ import org.thingsboard.server.common.data.kv.StringDataEntry; import org.thingsboard.server.common.data.kv.TsKvEntry; import org.thingsboard.server.common.data.objects.TelemetryEntityView; import org.thingsboard.server.dao.entityview.EntityViewService; +import org.thingsboard.server.dao.exception.IncorrectParameterException; import org.thingsboard.server.dao.service.AbstractServiceTest; import org.thingsboard.server.dao.timeseries.TimeseriesService; @@ -757,6 +758,21 @@ public abstract class BaseTimeseriesServiceTest extends AbstractServiceTest { assertThat(fullList).containsOnlyOnceElementsOf(timeseries); } + @Test + public void testFindAllByQueriesWithAggregationAndZeroInterval() throws Exception { + testFindAllByQueriesWithAggregationAndInvalidInterval(0); + } + + @Test + public void testFindAllByQueriesWithAggregationAndNegativeInterval() throws Exception { + testFindAllByQueriesWithAggregationAndInvalidInterval(-1); + } + + private void testFindAllByQueriesWithAggregationAndInvalidInterval(long interval) { + BaseReadTsKvQuery query = new BaseReadTsKvQuery(STRING_KEY, TS, TS, interval, 1000, Aggregation.SUM, "DESC"); + Assert.assertThrows(IncorrectParameterException.class, () -> findAndVerifyQueryId(deviceId, query)); + } + private TsKvEntry save(DeviceId deviceId, long ts, long value) throws Exception { TsKvEntry entry = new BasicTsKvEntry(ts, new LongDataEntry(LONG_KEY, value)); tsService.save(tenantId, deviceId, entry).get(MAX_TIMEOUT, TimeUnit.SECONDS);