Search in sources :

Example 1 with BaseReadTsKvQuery

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

the class DefaultSubscriptionManagerService method handleNewTelemetrySubscription.

private void handleNewTelemetrySubscription(TbTimeseriesSubscription subscription) {
    log.trace("[{}][{}][{}] Processing remote telemetry subscription for entity [{}]", serviceId, subscription.getSessionId(), subscription.getSubscriptionId(), subscription.getEntityId());
    long curTs = System.currentTimeMillis();
    if (subscription.isLatestValues()) {
        DonAsynchron.withCallback(tsService.findLatest(subscription.getTenantId(), subscription.getEntityId(), subscription.getKeyStates().keySet()), missedUpdates -> {
            if (missedUpdates != null && !missedUpdates.isEmpty()) {
                TopicPartitionInfo tpi = partitionService.getNotificationsTopic(ServiceType.TB_CORE, subscription.getServiceId());
                toCoreNotificationsProducer.send(tpi, toProto(subscription, missedUpdates), null);
            }
        }, e -> log.error("Failed to fetch missed updates.", e), tsCallBackExecutor);
    } else {
        List<ReadTsKvQuery> queries = new ArrayList<>();
        subscription.getKeyStates().forEach((key, value) -> {
            if (curTs > value) {
                long startTs = subscription.getStartTime() > 0 ? Math.max(subscription.getStartTime(), value + 1L) : (value + 1L);
                long endTs = subscription.getEndTime() > 0 ? Math.min(subscription.getEndTime(), curTs) : curTs;
                queries.add(new BaseReadTsKvQuery(key, startTs, endTs, 0, 1000, Aggregation.NONE));
            }
        });
        if (!queries.isEmpty()) {
            DonAsynchron.withCallback(tsService.findAll(subscription.getTenantId(), subscription.getEntityId(), queries), missedUpdates -> {
                if (missedUpdates != null && !missedUpdates.isEmpty()) {
                    TopicPartitionInfo tpi = partitionService.getNotificationsTopic(ServiceType.TB_CORE, subscription.getServiceId());
                    toCoreNotificationsProducer.send(tpi, toProto(subscription, missedUpdates), null);
                }
            }, e -> log.error("Failed to fetch missed updates.", e), tsCallBackExecutor);
        }
    }
}
Also used : TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) ArrayList(java.util.ArrayList) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery)

Example 2 with BaseReadTsKvQuery

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

the class BaseTimeseriesServiceTest method testFindDeviceLongAndDoubleTsData.

@Test
public void testFindDeviceLongAndDoubleTsData() 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.0));
    entries.add(save(deviceId, 25000, 300));
    entries.add(save(deviceId, 35000, 400.0));
    entries.add(save(deviceId, 45000, 500));
    entries.add(save(deviceId, 55000, 600.0));
    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(600.0), list.get(0).getDoubleValue());
    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(400.0), list.get(2).getDoubleValue());
    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(300.0), list.get(0).getDoubleValue());
    assertEquals(30000, list.get(1).getTs());
    assertEquals(java.util.Optional.of(700.0), list.get(1).getDoubleValue());
    assertEquals(50000, list.get(2).getTs());
    assertEquals(java.util.Optional.of(1100.0), list.get(2).getDoubleValue());
    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(100.0), list.get(0).getDoubleValue());
    assertEquals(30000, list.get(1).getTs());
    assertEquals(java.util.Optional.of(300.0), list.get(1).getDoubleValue());
    assertEquals(50000, list.get(2).getTs());
    assertEquals(java.util.Optional.of(500.0), list.get(2).getDoubleValue());
    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(200.0), list.get(0).getDoubleValue());
    assertEquals(30000, list.get(1).getTs());
    assertEquals(java.util.Optional.of(400.0), list.get(1).getDoubleValue());
    assertEquals(50000, list.get(2).getTs());
    assertEquals(java.util.Optional.of(600.0), list.get(2).getDoubleValue());
    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());
}
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 3 with BaseReadTsKvQuery

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

the class BaseTimeseriesServiceTest method testFindByQueryAscOrder.

@Test
public void testFindByQueryAscOrder() 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, "ASC"));
    List<TsKvEntry> entries = tsService.findAll(tenantId, deviceId, queries).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    Assert.assertEquals(3, entries.size());
    Assert.assertEquals(toTsEntry(TS - 3, stringKvEntry), entries.get(0));
    Assert.assertEquals(toTsEntry(TS - 2, stringKvEntry), entries.get(1));
    Assert.assertEquals(toTsEntry(TS - 1, 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 - 3, stringKvEntry), entries.get(0));
    Assert.assertEquals(toTsEntry(TS - 2, stringKvEntry), entries.get(1));
    Assert.assertEquals(toTsEntry(TS - 1, 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)

Example 4 with BaseReadTsKvQuery

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

the class BaseTimeseriesServiceTest method testDeleteDeviceTsDataWithOverwritingLatest.

@Test
public void testDeleteDeviceTsDataWithOverwritingLatest() throws Exception {
    DeviceId deviceId = new DeviceId(Uuids.timeBased());
    saveEntries(deviceId, 10000);
    saveEntries(deviceId, 20000);
    saveEntries(deviceId, 30000);
    saveEntries(deviceId, 40000);
    tsService.remove(tenantId, deviceId, Collections.singletonList(new BaseDeleteTsKvQuery(STRING_KEY, 25000, 45000, true))).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    List<TsKvEntry> list = tsService.findAll(tenantId, deviceId, Collections.singletonList(new BaseReadTsKvQuery(STRING_KEY, 5000, 45000, 10000, 10, Aggregation.NONE))).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    Assert.assertEquals(2, list.size());
    List<TsKvEntry> latest = tsService.findLatest(tenantId, deviceId, Collections.singletonList(STRING_KEY)).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    Assert.assertEquals(20000, latest.get(0).getTs());
}
Also used : BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BaseDeleteTsKvQuery(org.thingsboard.server.common.data.kv.BaseDeleteTsKvQuery) DeviceId(org.thingsboard.server.common.data.id.DeviceId) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) AbstractServiceTest(org.thingsboard.server.dao.service.AbstractServiceTest) Test(org.junit.Test)

Example 5 with BaseReadTsKvQuery

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

the class DefaultDataUpdateService method updateEntityViewLatestTelemetry.

private ListenableFuture<List<Void>> updateEntityViewLatestTelemetry(EntityView entityView) {
    EntityViewId entityId = entityView.getId();
    List<String> keys = entityView.getKeys() != null && entityView.getKeys().getTimeseries() != null ? entityView.getKeys().getTimeseries() : Collections.emptyList();
    long startTs = entityView.getStartTimeMs();
    long endTs = entityView.getEndTimeMs() == 0 ? Long.MAX_VALUE : entityView.getEndTimeMs();
    ListenableFuture<List<String>> keysFuture;
    if (keys.isEmpty()) {
        keysFuture = Futures.transform(tsService.findAllLatest(TenantId.SYS_TENANT_ID, entityView.getEntityId()), latest -> latest.stream().map(TsKvEntry::getKey).collect(Collectors.toList()), MoreExecutors.directExecutor());
    } else {
        keysFuture = Futures.immediateFuture(keys);
    }
    ListenableFuture<List<TsKvEntry>> latestFuture = Futures.transformAsync(keysFuture, fetchKeys -> {
        List<ReadTsKvQuery> queries = fetchKeys.stream().filter(key -> !isBlank(key)).map(key -> new BaseReadTsKvQuery(key, startTs, endTs, 1, "DESC")).collect(Collectors.toList());
        if (!queries.isEmpty()) {
            return tsService.findAll(TenantId.SYS_TENANT_ID, entityView.getEntityId(), queries);
        } else {
            return Futures.immediateFuture(null);
        }
    }, MoreExecutors.directExecutor());
    return Futures.transformAsync(latestFuture, latestValues -> {
        if (latestValues != null && !latestValues.isEmpty()) {
            ListenableFuture<List<Void>> saveFuture = tsService.saveLatest(TenantId.SYS_TENANT_ID, entityId, latestValues);
            return saveFuture;
        }
        return Futures.immediateFuture(null);
    }, MoreExecutors.directExecutor());
}
Also used : TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) Autowired(org.springframework.beans.factory.annotation.Autowired) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) TenantId(org.thingsboard.server.common.data.id.TenantId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) TenantService(org.thingsboard.server.dao.tenant.TenantService) TbRuleChainInputNodeConfiguration(org.thingsboard.rule.engine.flow.TbRuleChainInputNodeConfiguration) OAuth2Service(org.thingsboard.server.dao.oauth2.OAuth2Service) EntityService(org.thingsboard.server.dao.entity.EntityService) JsonNode(com.fasterxml.jackson.databind.JsonNode) DeviceProfileRepository(org.thingsboard.server.dao.sql.device.DeviceProfileRepository) EntityViewService(org.thingsboard.server.dao.entityview.EntityViewService) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) PageLink(org.thingsboard.server.common.data.page.PageLink) RelationEntity(org.thingsboard.server.dao.model.sql.RelationEntity) TimeseriesService(org.thingsboard.server.dao.timeseries.TimeseriesService) Collectors(java.util.stream.Collectors) Alarm(org.thingsboard.server.common.data.alarm.Alarm) AlarmDao(org.thingsboard.server.dao.alarm.AlarmDao) EntityView(org.thingsboard.server.common.data.EntityView) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) Slf4j(lombok.extern.slf4j.Slf4j) TbDeviceProfileNodeConfiguration(org.thingsboard.rule.engine.profile.TbDeviceProfileNodeConfiguration) List(java.util.List) DeviceProfileEntity(org.thingsboard.server.dao.model.sql.DeviceProfileEntity) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) EntityViewId(org.thingsboard.server.common.data.id.EntityViewId) TbDeviceProfileNode(org.thingsboard.rule.engine.profile.TbDeviceProfileNode) AlarmSeverity(org.thingsboard.server.common.data.alarm.AlarmSeverity) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) DynamicValue(org.thingsboard.server.common.data.query.DynamicValue) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) JacksonUtil(org.thingsboard.common.util.JacksonUtil) TbRuleChainInputNode(org.thingsboard.rule.engine.flow.TbRuleChainInputNode) Tenant(org.thingsboard.server.common.data.Tenant) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RuleChainService(org.thingsboard.server.dao.rule.RuleChainService) ArrayList(java.util.ArrayList) RelationService(org.thingsboard.server.dao.relation.RelationService) Service(org.springframework.stereotype.Service) AlarmService(org.thingsboard.server.dao.alarm.AlarmService) AlarmQuery(org.thingsboard.server.common.data.alarm.AlarmQuery) InstallScripts(org.thingsboard.server.service.install.InstallScripts) AlarmInfo(org.thingsboard.server.common.data.alarm.AlarmInfo) FilterPredicateValue(org.thingsboard.server.common.data.query.FilterPredicateValue) RuleChainType(org.thingsboard.server.common.data.rule.RuleChainType) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) Profile(org.springframework.context.annotation.Profile) ExecutionException(java.util.concurrent.ExecutionException) AtomicLong(java.util.concurrent.atomic.AtomicLong) Futures(com.google.common.util.concurrent.Futures) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) PageData(org.thingsboard.server.common.data.page.PageData) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) DaoUtil(org.thingsboard.server.dao.DaoUtil) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) Collections(java.util.Collections) EntityViewId(org.thingsboard.server.common.data.id.EntityViewId) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) List(java.util.List) ArrayList(java.util.ArrayList)

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