Search in sources :

Example 11 with BaseReadTsKvQuery

use of org.thingsboard.server.common.data.kv.BaseReadTsKvQuery in project thingsboard by thingsboard.

the class CassandraBaseTimeseriesDao method findAllAsync.

@Override
public ListenableFuture<List<TsKvEntry>> findAllAsync(TenantId tenantId, EntityId entityId, ReadTsKvQuery query) {
    if (query.getAggregation() == Aggregation.NONE) {
        return findAllAsyncWithLimit(tenantId, entityId, query);
    } else {
        long step = Math.max(query.getInterval(), MIN_AGGREGATION_STEP_MS);
        long stepTs = query.getStartTs();
        List<ListenableFuture<Optional<TsKvEntry>>> futures = new ArrayList<>();
        while (stepTs < query.getEndTs()) {
            long startTs = stepTs;
            long endTs = stepTs + step;
            ReadTsKvQuery subQuery = new BaseReadTsKvQuery(query.getKey(), startTs, endTs, step, 1, query.getAggregation(), query.getOrder());
            futures.add(findAndAggregateAsync(tenantId, entityId, subQuery, toPartitionTs(startTs), toPartitionTs(endTs)));
            stepTs = endTs;
        }
        ListenableFuture<List<Optional<TsKvEntry>>> future = Futures.allAsList(futures);
        return Futures.transform(future, new Function<List<Optional<TsKvEntry>>, List<TsKvEntry>>() {

            @Nullable
            @Override
            public List<TsKvEntry> apply(@Nullable List<Optional<TsKvEntry>> input) {
                return input == null ? Collections.emptyList() : input.stream().filter(v -> v.isPresent()).map(v -> v.get()).collect(Collectors.toList());
            }
        }, readResultsProcessingExecutor);
    }
}
Also used : QueryBuilder(com.datastax.oss.driver.api.querybuilder.QueryBuilder) Arrays(java.util.Arrays) Autowired(org.springframework.beans.factory.annotation.Autowired) TenantId(org.thingsboard.server.common.data.id.TenantId) AggregationTimeseriesDao(org.thingsboard.server.dao.sqlts.AggregationTimeseriesDao) TbResultSet(org.thingsboard.server.dao.nosql.TbResultSet) PreDestroy(javax.annotation.PreDestroy) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) Profiles(org.springframework.core.env.Profiles) ZoneOffset(java.time.ZoneOffset) Select(com.datastax.oss.driver.api.querybuilder.select.Select) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) Function(com.google.common.base.Function) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) DeleteTsKvQuery(org.thingsboard.server.common.data.kv.DeleteTsKvQuery) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) QueryBuilder.literal(com.datastax.oss.driver.api.querybuilder.QueryBuilder.literal) Environment(org.springframework.core.env.Environment) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) LocalDateTime(java.time.LocalDateTime) NoSqlTsDao(org.thingsboard.server.dao.util.NoSqlTsDao) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) DataType(org.thingsboard.server.common.data.kv.DataType) EntityId(org.thingsboard.server.common.data.id.EntityId) Row(com.datastax.oss.driver.api.core.cql.Row) Nullable(javax.annotation.Nullable) TbResultSetFuture(org.thingsboard.server.dao.nosql.TbResultSetFuture) ReentrantLock(java.util.concurrent.locks.ReentrantLock) BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) Aggregation(org.thingsboard.server.common.data.kv.Aggregation) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) FutureCallback(com.google.common.util.concurrent.FutureCallback) TimeUnit(java.util.concurrent.TimeUnit) Futures(com.google.common.util.concurrent.Futures) Component(org.springframework.stereotype.Component) Lock(java.util.concurrent.locks.Lock) ChronoUnit(java.time.temporal.ChronoUnit) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) Collections(java.util.Collections) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) Optional(java.util.Optional) ArrayList(java.util.ArrayList) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) List(java.util.List) ArrayList(java.util.ArrayList) Nullable(javax.annotation.Nullable)

Example 12 with BaseReadTsKvQuery

use of org.thingsboard.server.common.data.kv.BaseReadTsKvQuery in project thingsboard by thingsboard.

the class SqlTimeseriesLatestDao method findNewLatestEntryFuture.

private ListenableFuture<List<TsKvEntry>> findNewLatestEntryFuture(TenantId tenantId, EntityId entityId, DeleteTsKvQuery query) {
    long startTs = 0;
    long endTs = query.getStartTs() - 1;
    ReadTsKvQuery findNewLatestQuery = new BaseReadTsKvQuery(query.getKey(), startTs, endTs, endTs - startTs, 1, Aggregation.NONE, DESC_ORDER);
    return aggregationTimeseriesDao.findAllAsync(tenantId, entityId, findNewLatestQuery);
}
Also used : BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery)

Example 13 with BaseReadTsKvQuery

use of org.thingsboard.server.common.data.kv.BaseReadTsKvQuery in project thingsboard by thingsboard.

the class BaseTimeseriesServiceTest method testFindDeviceTsData.

@Test
public void testFindDeviceTsData() throws Exception {
    DeviceId deviceId = new DeviceId(Uuids.timeBased());
    List<TsKvEntry> entries = new ArrayList<>();
    entries.add(save(deviceId, 5000, 100));
    entries.add(save(deviceId, 15000, 200));
    entries.add(save(deviceId, 25000, 300));
    entries.add(save(deviceId, 35000, 400));
    entries.add(save(deviceId, 45000, 500));
    entries.add(save(deviceId, 55000, 600));
    List<TsKvEntry> list = tsService.findAll(tenantId, deviceId, Collections.singletonList(new BaseReadTsKvQuery(LONG_KEY, 0, 60000, 20000, 3, Aggregation.NONE))).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    assertEquals(3, list.size());
    assertEquals(55000, list.get(0).getTs());
    assertEquals(java.util.Optional.of(600L), list.get(0).getLongValue());
    assertEquals(45000, list.get(1).getTs());
    assertEquals(java.util.Optional.of(500L), list.get(1).getLongValue());
    assertEquals(35000, list.get(2).getTs());
    assertEquals(java.util.Optional.of(400L), list.get(2).getLongValue());
    list = tsService.findAll(tenantId, deviceId, Collections.singletonList(new BaseReadTsKvQuery(LONG_KEY, 0, 60000, 20000, 3, Aggregation.AVG))).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    assertEquals(3, list.size());
    assertEquals(10000, list.get(0).getTs());
    assertEquals(java.util.Optional.of(150.0), list.get(0).getDoubleValue());
    assertEquals(30000, list.get(1).getTs());
    assertEquals(java.util.Optional.of(350.0), list.get(1).getDoubleValue());
    assertEquals(50000, list.get(2).getTs());
    assertEquals(java.util.Optional.of(550.0), list.get(2).getDoubleValue());
    list = tsService.findAll(tenantId, deviceId, Collections.singletonList(new BaseReadTsKvQuery(LONG_KEY, 0, 60000, 20000, 3, Aggregation.SUM))).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    assertEquals(3, list.size());
    assertEquals(10000, list.get(0).getTs());
    assertEquals(java.util.Optional.of(300L), list.get(0).getLongValue());
    assertEquals(30000, list.get(1).getTs());
    assertEquals(java.util.Optional.of(700L), list.get(1).getLongValue());
    assertEquals(50000, list.get(2).getTs());
    assertEquals(java.util.Optional.of(1100L), list.get(2).getLongValue());
    list = tsService.findAll(tenantId, deviceId, Collections.singletonList(new BaseReadTsKvQuery(LONG_KEY, 0, 60000, 20000, 3, Aggregation.MIN))).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    assertEquals(3, list.size());
    assertEquals(10000, list.get(0).getTs());
    assertEquals(java.util.Optional.of(100L), list.get(0).getLongValue());
    assertEquals(30000, list.get(1).getTs());
    assertEquals(java.util.Optional.of(300L), list.get(1).getLongValue());
    assertEquals(50000, list.get(2).getTs());
    assertEquals(java.util.Optional.of(500L), list.get(2).getLongValue());
    list = tsService.findAll(tenantId, deviceId, Collections.singletonList(new BaseReadTsKvQuery(LONG_KEY, 0, 60000, 20000, 3, Aggregation.MAX))).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    assertEquals(3, list.size());
    assertEquals(10000, list.get(0).getTs());
    assertEquals(java.util.Optional.of(200L), list.get(0).getLongValue());
    assertEquals(30000, list.get(1).getTs());
    assertEquals(java.util.Optional.of(400L), list.get(1).getLongValue());
    assertEquals(50000, list.get(2).getTs());
    assertEquals(java.util.Optional.of(600L), list.get(2).getLongValue());
    list = tsService.findAll(tenantId, deviceId, Collections.singletonList(new BaseReadTsKvQuery(LONG_KEY, 0, 60000, 20000, 3, Aggregation.COUNT))).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    assertEquals(3, list.size());
    assertEquals(10000, list.get(0).getTs());
    assertEquals(java.util.Optional.of(2L), list.get(0).getLongValue());
    assertEquals(30000, list.get(1).getTs());
    assertEquals(java.util.Optional.of(2L), list.get(1).getLongValue());
    assertEquals(50000, list.get(2).getTs());
    assertEquals(java.util.Optional.of(2L), list.get(2).getLongValue());
    entries.add(save(deviceId, 65000, "A1"));
    entries.add(save(deviceId, 75000, "A2"));
    entries.add(save(deviceId, 85000, "B1"));
    entries.add(save(deviceId, 95000, "B2"));
    entries.add(save(deviceId, 105000, "C1"));
    entries.add(save(deviceId, 115000, "C2"));
    list = tsService.findAll(tenantId, deviceId, Collections.singletonList(new BaseReadTsKvQuery(LONG_KEY, 60000, 120000, 20000, 3, Aggregation.NONE))).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    assertEquals(3, list.size());
    assertEquals(115000, list.get(0).getTs());
    assertEquals(java.util.Optional.of("C2"), list.get(0).getStrValue());
    assertEquals(105000, list.get(1).getTs());
    assertEquals(java.util.Optional.of("C1"), list.get(1).getStrValue());
    assertEquals(95000, list.get(2).getTs());
    assertEquals(java.util.Optional.of("B2"), list.get(2).getStrValue());
    list = tsService.findAll(tenantId, deviceId, Collections.singletonList(new BaseReadTsKvQuery(LONG_KEY, 60000, 120000, 20000, 3, Aggregation.MIN))).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    assertEquals(3, list.size());
    assertEquals(70000, list.get(0).getTs());
    assertEquals(java.util.Optional.of("A1"), list.get(0).getStrValue());
    assertEquals(90000, list.get(1).getTs());
    assertEquals(java.util.Optional.of("B1"), list.get(1).getStrValue());
    assertEquals(110000, list.get(2).getTs());
    assertEquals(java.util.Optional.of("C1"), list.get(2).getStrValue());
    list = tsService.findAll(tenantId, deviceId, Collections.singletonList(new BaseReadTsKvQuery(LONG_KEY, 60000, 120000, 20000, 3, Aggregation.MAX))).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    assertEquals(3, list.size());
    assertEquals(70000, list.get(0).getTs());
    assertEquals(java.util.Optional.of("A2"), list.get(0).getStrValue());
    assertEquals(90000, list.get(1).getTs());
    assertEquals(java.util.Optional.of("B2"), list.get(1).getStrValue());
    assertEquals(110000, list.get(2).getTs());
    assertEquals(java.util.Optional.of("C2"), list.get(2).getStrValue());
    list = tsService.findAll(tenantId, deviceId, Collections.singletonList(new BaseReadTsKvQuery(LONG_KEY, 60000, 120000, 20000, 3, Aggregation.COUNT))).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    assertEquals(3, list.size());
    assertEquals(70000, list.get(0).getTs());
    assertEquals(java.util.Optional.of(2L), list.get(0).getLongValue());
    assertEquals(90000, list.get(1).getTs());
    assertEquals(java.util.Optional.of(2L), list.get(1).getLongValue());
    assertEquals(110000, list.get(2).getTs());
    assertEquals(java.util.Optional.of(2L), list.get(2).getLongValue());
}
Also used : BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) DeviceId(org.thingsboard.server.common.data.id.DeviceId) ArrayList(java.util.ArrayList) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) AbstractServiceTest(org.thingsboard.server.dao.service.AbstractServiceTest) Test(org.junit.Test)

Example 14 with BaseReadTsKvQuery

use of org.thingsboard.server.common.data.kv.BaseReadTsKvQuery in project thingsboard by thingsboard.

the class BaseTimeseriesServiceTest method testFindByQueryDescOrder.

@Test
public void testFindByQueryDescOrder() throws Exception {
    DeviceId deviceId = new DeviceId(Uuids.timeBased());
    saveEntries(deviceId, TS - 3);
    saveEntries(deviceId, TS - 2);
    saveEntries(deviceId, TS - 1);
    List<ReadTsKvQuery> queries = new ArrayList<>();
    queries.add(new BaseReadTsKvQuery(STRING_KEY, TS - 3, TS, 0, 1000, Aggregation.NONE, "DESC"));
    List<TsKvEntry> entries = tsService.findAll(tenantId, deviceId, queries).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    Assert.assertEquals(3, entries.size());
    Assert.assertEquals(toTsEntry(TS - 1, stringKvEntry), entries.get(0));
    Assert.assertEquals(toTsEntry(TS - 2, stringKvEntry), entries.get(1));
    Assert.assertEquals(toTsEntry(TS - 3, stringKvEntry), entries.get(2));
    EntityView entityView = saveAndCreateEntityView(deviceId, Arrays.asList(STRING_KEY));
    entries = tsService.findAll(tenantId, entityView.getId(), queries).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    Assert.assertEquals(3, entries.size());
    Assert.assertEquals(toTsEntry(TS - 1, stringKvEntry), entries.get(0));
    Assert.assertEquals(toTsEntry(TS - 2, stringKvEntry), entries.get(1));
    Assert.assertEquals(toTsEntry(TS - 3, stringKvEntry), entries.get(2));
}
Also used : BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) TelemetryEntityView(org.thingsboard.server.common.data.objects.TelemetryEntityView) EntityView(org.thingsboard.server.common.data.EntityView) DeviceId(org.thingsboard.server.common.data.id.DeviceId) ArrayList(java.util.ArrayList) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) AbstractServiceTest(org.thingsboard.server.dao.service.AbstractServiceTest) Test(org.junit.Test)

Aggregations

BaseReadTsKvQuery (org.thingsboard.server.common.data.kv.BaseReadTsKvQuery)14 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)12 ArrayList (java.util.ArrayList)11 ReadTsKvQuery (org.thingsboard.server.common.data.kv.ReadTsKvQuery)11 List (java.util.List)7 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)7 Futures (com.google.common.util.concurrent.Futures)6 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)6 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)6 Collectors (java.util.stream.Collectors)6 Slf4j (lombok.extern.slf4j.Slf4j)6 Autowired (org.springframework.beans.factory.annotation.Autowired)6 TenantId (org.thingsboard.server.common.data.id.TenantId)6 FutureCallback (com.google.common.util.concurrent.FutureCallback)5 Test (org.junit.Test)5 TimeseriesService (org.thingsboard.server.dao.timeseries.TimeseriesService)5 Collections (java.util.Collections)4 DeviceId (org.thingsboard.server.common.data.id.DeviceId)4 Function (com.google.common.base.Function)3 Arrays (java.util.Arrays)3