Commit df27d7acc8b9371a60fd6b2741d2a2f7941118df

Authored by Andrew Shvayka
1 parent 02be0edf

Validation of TsKvQuery

... ... @@ -83,6 +83,7 @@ dashboard:
83 83 max_datapoints_limit: "${DASHBOARD_MAX_DATAPOINTS_LIMIT:50000}"
84 84
85 85 database:
  86 + ts_max_intervals: "${DATABASE_TS_MAX_INTERVALS:700}" # mas number of DB queries generated by single API call to fetch telemetry records
86 87 entities:
87 88 type: "${DATABASE_ENTITIES_TYPE:sql}" # cassandra OR sql
88 89 ts:
... ...
... ... @@ -20,11 +20,13 @@ import com.google.common.util.concurrent.Futures;
20 20 import com.google.common.util.concurrent.ListenableFuture;
21 21 import lombok.extern.slf4j.Slf4j;
22 22 import org.springframework.beans.factory.annotation.Autowired;
  23 +import org.springframework.beans.factory.annotation.Value;
23 24 import org.springframework.stereotype.Service;
24 25 import org.thingsboard.server.common.data.EntityType;
25 26 import org.thingsboard.server.common.data.EntityView;
26 27 import org.thingsboard.server.common.data.id.EntityId;
27 28 import org.thingsboard.server.common.data.id.EntityViewId;
  29 +import org.thingsboard.server.common.data.kv.Aggregation;
28 30 import org.thingsboard.server.common.data.kv.BaseReadTsKvQuery;
29 31 import org.thingsboard.server.common.data.kv.DeleteTsKvQuery;
30 32 import org.thingsboard.server.common.data.kv.ReadTsKvQuery;
... ... @@ -47,8 +49,11 @@ import static org.apache.commons.lang3.StringUtils.isBlank;
47 49 @Slf4j
48 50 public class BaseTimeseriesService implements TimeseriesService {
49 51
50   - public static final int INSERTS_PER_ENTRY = 3;
51   - public static final int DELETES_PER_ENTRY = INSERTS_PER_ENTRY;
  52 + private static final int INSERTS_PER_ENTRY = 3;
  53 + private static final int DELETES_PER_ENTRY = INSERTS_PER_ENTRY;
  54 +
  55 + @Value("${database.ts_max_intervals}")
  56 + private long maxTsIntervals;
52 57
53 58 @Autowired
54 59 private TimeseriesDao timeseriesDao;
... ... @@ -59,7 +64,7 @@ public class BaseTimeseriesService implements TimeseriesService {
59 64 @Override
60 65 public ListenableFuture<List<TsKvEntry>> findAll(EntityId entityId, List<ReadTsKvQuery> queries) {
61 66 validate(entityId);
62   - queries.forEach(BaseTimeseriesService::validate);
  67 + queries.forEach(this::validate);
63 68 if (entityId.getEntityType().equals(EntityType.ENTITY_VIEW)) {
64 69 EntityView entityView = entityViewService.findEntityViewById((EntityViewId) entityId);
65 70 List<ReadTsKvQuery> filteredQueries =
... ... @@ -189,7 +194,7 @@ public class BaseTimeseriesService implements TimeseriesService {
189 194 Validator.validateEntityId(entityId, "Incorrect entityId " + entityId);
190 195 }
191 196
192   - private static void validate(ReadTsKvQuery query) {
  197 + private void validate(ReadTsKvQuery query) {
193 198 if (query == null) {
194 199 throw new IncorrectParameterException("ReadTsKvQuery can't be null");
195 200 } else if (isBlank(query.getKey())) {
... ... @@ -197,6 +202,14 @@ public class BaseTimeseriesService implements TimeseriesService {
197 202 } else if (query.getAggregation() == null) {
198 203 throw new IncorrectParameterException("Incorrect ReadTsKvQuery. Aggregation can't be empty");
199 204 }
  205 + if(!Aggregation.NONE.equals(query.getAggregation())) {
  206 + long step = Math.max(query.getInterval(), 1000);
  207 + long intervalCounts = (query.getEndTs() - query.getStartTs()) / step;
  208 + if (intervalCounts > maxTsIntervals || intervalCounts < 0) {
  209 + throw new IncorrectParameterException("Incorrect TsKvQuery. Number of intervals is to high - " + intervalCounts + ". " +
  210 + "Please increase 'interval' parameter for your query or reduce the time range of the query.");
  211 + }
  212 + }
200 213 }
201 214
202 215 private static void validate(DeleteTsKvQuery query) {
... ...