added interval validation to the query validator

This commit is contained in:
YevhenBondarenko 2025-03-20 15:48:44 +01:00
parent c3b608492a
commit 77c9628b14
2 changed files with 22 additions and 1 deletions

View File

@ -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 + ". " +

View File

@ -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);