Search in sources :

Example 1 with ReadTsKvQuery

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

the class CassandraBaseTimeseriesLatestDao method getNewLatestEntryFuture.

private ListenableFuture<TsKvLatestRemovingResult> getNewLatestEntryFuture(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);
    ListenableFuture<List<TsKvEntry>> future = aggregationTimeseriesDao.findAllAsync(tenantId, entityId, findNewLatestQuery);
    return Futures.transformAsync(future, entryList -> {
        if (entryList.size() == 1) {
            TsKvEntry entry = entryList.get(0);
            return Futures.transform(saveLatest(tenantId, entityId, entryList.get(0)), v -> new TsKvLatestRemovingResult(entry), MoreExecutors.directExecutor());
        } else {
            log.trace("Could not find new latest value for [{}], key - {}", entityId, query.getKey());
        }
        return Futures.immediateFuture(new TsKvLatestRemovingResult(query.getKey(), true));
    }, readResultsProcessingExecutor);
}
Also used : TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) 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) TsKvLatestRemovingResult(org.thingsboard.server.common.data.kv.TsKvLatestRemovingResult)

Example 2 with ReadTsKvQuery

use of org.thingsboard.server.common.data.kv.ReadTsKvQuery 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)

Example 3 with ReadTsKvQuery

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

the class TelemetryController method getTimeseries.

@ApiOperation(value = "Get time-series data (getTimeseries)", notes = "Returns a range of time-series values for specified entity. " + "Returns not aggregated data by default. " + "Use aggregation function ('agg') and aggregation interval ('interval') to enable aggregation of the results on the database / server side. " + "The aggregation is generally more efficient then fetching all records. \n\n" + MARKDOWN_CODE_BLOCK_START + TS_STRICT_DATA_EXAMPLE + MARKDOWN_CODE_BLOCK_END + "\n\n" + INVALID_ENTITY_ID_OR_ENTITY_TYPE_DESCRIPTION + TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/{entityType}/{entityId}/values/timeseries", method = RequestMethod.GET, params = { "keys", "startTs", "endTs" })
@ResponseBody
public DeferredResult<ResponseEntity> getTimeseries(@ApiParam(value = ENTITY_TYPE_PARAM_DESCRIPTION, required = true, defaultValue = "DEVICE") @PathVariable("entityType") String entityType, @ApiParam(value = ENTITY_ID_PARAM_DESCRIPTION, required = true) @PathVariable("entityId") String entityIdStr, @ApiParam(value = TELEMETRY_KEYS_BASE_DESCRIPTION, required = true) @RequestParam(name = "keys") String keys, @ApiParam(value = "A long value representing the start timestamp of the time range in milliseconds, UTC.") @RequestParam(name = "startTs") Long startTs, @ApiParam(value = "A long value representing the end timestamp of the time range in milliseconds, UTC.") @RequestParam(name = "endTs") Long endTs, @ApiParam(value = "A long value representing the aggregation interval range in milliseconds.") @RequestParam(name = "interval", defaultValue = "0") Long interval, @ApiParam(value = "An integer value that represents a max number of timeseries data points to fetch." + " This parameter is used only in the case if 'agg' parameter is set to 'NONE'.", defaultValue = "100") @RequestParam(name = "limit", defaultValue = "100") Integer limit, @ApiParam(value = "A string value representing the aggregation function. " + "If the interval is not specified, 'agg' parameter will use 'NONE' value.", allowableValues = "MIN, MAX, AVG, SUM, COUNT, NONE") @RequestParam(name = "agg", defaultValue = "NONE") String aggStr, @ApiParam(value = SORT_ORDER_DESCRIPTION, allowableValues = SORT_ORDER_ALLOWABLE_VALUES) @RequestParam(name = "orderBy", defaultValue = "DESC") String orderBy, @ApiParam(value = STRICT_DATA_TYPES_DESCRIPTION) @RequestParam(name = "useStrictDataTypes", required = false, defaultValue = "false") Boolean useStrictDataTypes) throws ThingsboardException {
    try {
        return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_TELEMETRY, entityType, entityIdStr, (result, tenantId, entityId) -> {
            // If interval is 0, convert this to a NONE aggregation, which is probably what the user really wanted
            Aggregation agg = interval == 0L ? Aggregation.valueOf(Aggregation.NONE.name()) : Aggregation.valueOf(aggStr);
            List<ReadTsKvQuery> queries = toKeysList(keys).stream().map(key -> new BaseReadTsKvQuery(key, startTs, endTs, interval, limit, agg, orderBy)).collect(Collectors.toList());
            Futures.addCallback(tsService.findAll(tenantId, entityId, queries), getTsKvListCallback(result, useStrictDataTypes), MoreExecutors.directExecutor());
        });
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : Aggregation(org.thingsboard.server.common.data.kv.Aggregation) TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH(org.thingsboard.server.controller.ControllerConstants.TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH) RequestParam(org.springframework.web.bind.annotation.RequestParam) Arrays(java.util.Arrays) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) BaseDeleteTsKvQuery(org.thingsboard.server.common.data.kv.BaseDeleteTsKvQuery) TsData(org.thingsboard.server.service.telemetry.TsData) ENTITY_TYPE_PARAM_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.ENTITY_TYPE_PARAM_DESCRIPTION) SORT_ORDER_ALLOWABLE_VALUES(org.thingsboard.server.controller.ControllerConstants.SORT_ORDER_ALLOWABLE_VALUES) ApiParam(io.swagger.annotations.ApiParam) Autowired(org.springframework.beans.factory.annotation.Autowired) STRICT_DATA_TYPES_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.STRICT_DATA_TYPES_DESCRIPTION) TenantId(org.thingsboard.server.common.data.id.TenantId) Map(java.util.Map) SAVE_ENTITY_TIMESERIES_STATUS_UNAUTHORIZED(org.thingsboard.server.controller.ControllerConstants.SAVE_ENTITY_TIMESERIES_STATUS_UNAUTHORIZED) SAVE_ATTIRIBUTES_STATUS_OK(org.thingsboard.server.controller.ControllerConstants.SAVE_ATTIRIBUTES_STATUS_OK) JsonConverter(org.thingsboard.server.common.transport.adaptor.JsonConverter) INVALID_ENTITY_ID_OR_ENTITY_TYPE_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.INVALID_ENTITY_ID_OR_ENTITY_TYPE_DESCRIPTION) JsonNode(com.fasterxml.jackson.databind.JsonNode) ATTRIBUTES_KEYS_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.ATTRIBUTES_KEYS_DESCRIPTION) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) Set(java.util.Set) RestController(org.springframework.web.bind.annotation.RestController) DeleteTsKvQuery(org.thingsboard.server.common.data.kv.DeleteTsKvQuery) Executors(java.util.concurrent.Executors) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) Slf4j(lombok.extern.slf4j.Slf4j) Operation(org.thingsboard.server.service.security.permission.Operation) BooleanDataEntry(org.thingsboard.server.common.data.kv.BooleanDataEntry) DeferredResult(org.springframework.web.context.request.async.DeferredResult) TELEMETRY_KEYS_BASE_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.TELEMETRY_KEYS_BASE_DESCRIPTION) JsonParser(com.google.gson.JsonParser) DEVICE_ID(org.thingsboard.server.controller.ControllerConstants.DEVICE_ID) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) RequestBody(org.springframework.web.bind.annotation.RequestBody) LinkedHashMap(java.util.LinkedHashMap) EntityIdFactory(org.thingsboard.server.common.data.id.EntityIdFactory) DEVICE_ID_PARAM_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.DEVICE_ID_PARAM_DESCRIPTION) DataType(org.thingsboard.server.common.data.kv.DataType) SAVE_ATTRIBUTES_REQUEST_PAYLOAD(org.thingsboard.server.controller.ControllerConstants.SAVE_ATTRIBUTES_REQUEST_PAYLOAD) Nullable(javax.annotation.Nullable) DataConstants(org.thingsboard.server.common.data.DataConstants) ATTRIBUTES_JSON_REQUEST_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.ATTRIBUTES_JSON_REQUEST_DESCRIPTION) ConstraintValidator(org.thingsboard.server.dao.service.ConstraintValidator) ATTRIBUTES_SCOPE_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.ATTRIBUTES_SCOPE_DESCRIPTION) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) FutureCallback(com.google.common.util.concurrent.FutureCallback) HttpStatus(org.springframework.http.HttpStatus) Futures(com.google.common.util.concurrent.Futures) SAVE_ENTITY_ATTRIBUTES_STATUS_UNAUTHORIZED(org.thingsboard.server.controller.ControllerConstants.SAVE_ENTITY_ATTRIBUTES_STATUS_UNAUTHORIZED) ApiResponse(io.swagger.annotations.ApiResponse) MARKDOWN_CODE_BLOCK_START(org.thingsboard.server.controller.ControllerConstants.MARKDOWN_CODE_BLOCK_START) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) ENTITY_ID_PARAM_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.ENTITY_ID_PARAM_DESCRIPTION) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) PathVariable(org.springframework.web.bind.annotation.PathVariable) SAVE_ENTITY_ATTRIBUTES_STATUS_INTERNAL_SERVER_ERROR(org.thingsboard.server.controller.ControllerConstants.SAVE_ENTITY_ATTRIBUTES_STATUS_INTERNAL_SERVER_ERROR) SAVE_ENTITY_TIMESERIES_STATUS_INTERNAL_SERVER_ERROR(org.thingsboard.server.controller.ControllerConstants.SAVE_ENTITY_TIMESERIES_STATUS_INTERNAL_SERVER_ERROR) DefaultTenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) ApiOperation(io.swagger.annotations.ApiOperation) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) PreDestroy(javax.annotation.PreDestroy) INVALID_STRUCTURE_OF_THE_REQUEST(org.thingsboard.server.controller.ControllerConstants.INVALID_STRUCTURE_OF_THE_REQUEST) EntityType(org.thingsboard.server.common.data.EntityType) TS_STRICT_DATA_EXAMPLE(org.thingsboard.server.controller.ControllerConstants.TS_STRICT_DATA_EXAMPLE) DeviceAttributesEventNotificationMsg(org.thingsboard.rule.engine.api.msg.DeviceAttributesEventNotificationMsg) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Function(com.google.common.base.Function) MARKDOWN_CODE_BLOCK_END(org.thingsboard.server.controller.ControllerConstants.MARKDOWN_CODE_BLOCK_END) AttributeData(org.thingsboard.server.service.telemetry.AttributeData) MediaType(org.springframework.http.MediaType) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) TimeseriesService(org.thingsboard.server.dao.timeseries.TimeseriesService) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) UncheckedApiException(org.thingsboard.server.service.telemetry.exception.UncheckedApiException) LATEST_TS_NON_STRICT_DATA_EXAMPLE(org.thingsboard.server.controller.ControllerConstants.LATEST_TS_NON_STRICT_DATA_EXAMPLE) Collectors(java.util.stream.Collectors) TELEMETRY_KEYS_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.TELEMETRY_KEYS_DESCRIPTION) TELEMETRY_JSON_REQUEST_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.TELEMETRY_JSON_REQUEST_DESCRIPTION) List(java.util.List) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) PostConstruct(javax.annotation.PostConstruct) TELEMETRY_SCOPE_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.TELEMETRY_SCOPE_DESCRIPTION) SAVE_ATTIRIBUTES_STATUS_BAD_REQUEST(org.thingsboard.server.controller.ControllerConstants.SAVE_ATTIRIBUTES_STATUS_BAD_REQUEST) SORT_ORDER_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.SORT_ORDER_DESCRIPTION) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) JsonParseException(com.google.gson.JsonParseException) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ATTRIBUTE_DATA_EXAMPLE(org.thingsboard.server.controller.ControllerConstants.ATTRIBUTE_DATA_EXAMPLE) SAVE_TIMESERIES_REQUEST_PAYLOAD(org.thingsboard.server.controller.ControllerConstants.SAVE_TIMESERIES_REQUEST_PAYLOAD) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) SAVE_ENTITY_ATTRIBUTES_STATUS_OK(org.thingsboard.server.controller.ControllerConstants.SAVE_ENTITY_ATTRIBUTES_STATUS_OK) JacksonUtil(org.thingsboard.common.util.JacksonUtil) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ApiResponses(io.swagger.annotations.ApiResponses) JsonElement(com.google.gson.JsonElement) HashSet(java.util.HashSet) TenantProfile(org.thingsboard.server.common.data.TenantProfile) ActionType(org.thingsboard.server.common.data.audit.ActionType) TbCoreComponent(org.thingsboard.server.queue.util.TbCoreComponent) EntityId(org.thingsboard.server.common.data.id.EntityId) UUIDBased(org.thingsboard.server.common.data.id.UUIDBased) DoubleDataEntry(org.thingsboard.server.common.data.kv.DoubleDataEntry) ExecutorService(java.util.concurrent.ExecutorService) AttributeKey(org.thingsboard.server.common.data.kv.AttributeKey) ATTRIBUTES_SCOPE_ALLOWED_VALUES(org.thingsboard.server.controller.ControllerConstants.ATTRIBUTES_SCOPE_ALLOWED_VALUES) AccessValidator(org.thingsboard.server.service.security.AccessValidator) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) Aggregation(org.thingsboard.server.common.data.kv.Aggregation) LATEST_TS_STRICT_DATA_EXAMPLE(org.thingsboard.server.controller.ControllerConstants.LATEST_TS_STRICT_DATA_EXAMPLE) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) SAVE_ENTITY_TIMESERIES_STATUS_OK(org.thingsboard.server.controller.ControllerConstants.SAVE_ENTITY_TIMESERIES_STATUS_OK) InvalidParametersException(org.thingsboard.server.service.telemetry.exception.InvalidParametersException) TimeUnit(java.util.concurrent.TimeUnit) JsonDataEntry(org.thingsboard.server.common.data.kv.JsonDataEntry) ResponseEntity(org.springframework.http.ResponseEntity) ENTITY_ATTRIBUTE_SCOPES(org.thingsboard.server.controller.ControllerConstants.ENTITY_ATTRIBUTE_SCOPES) StringUtils(org.springframework.util.StringUtils) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) UncheckedApiException(org.thingsboard.server.service.telemetry.exception.UncheckedApiException) JsonParseException(com.google.gson.JsonParseException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) InvalidParametersException(org.thingsboard.server.service.telemetry.exception.InvalidParametersException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with ReadTsKvQuery

use of org.thingsboard.server.common.data.kv.ReadTsKvQuery 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 5 with ReadTsKvQuery

use of org.thingsboard.server.common.data.kv.ReadTsKvQuery 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)

Aggregations

BaseReadTsKvQuery (org.thingsboard.server.common.data.kv.BaseReadTsKvQuery)12 ReadTsKvQuery (org.thingsboard.server.common.data.kv.ReadTsKvQuery)12 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)10 ArrayList (java.util.ArrayList)9 List (java.util.List)8 Futures (com.google.common.util.concurrent.Futures)7 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)7 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)7 Collectors (java.util.stream.Collectors)7 Slf4j (lombok.extern.slf4j.Slf4j)7 Autowired (org.springframework.beans.factory.annotation.Autowired)7 TenantId (org.thingsboard.server.common.data.id.TenantId)7 FutureCallback (com.google.common.util.concurrent.FutureCallback)5 Collections (java.util.Collections)5 EntityView (org.thingsboard.server.common.data.EntityView)5 TimeseriesService (org.thingsboard.server.dao.timeseries.TimeseriesService)5 Function (com.google.common.base.Function)4 TimeUnit (java.util.concurrent.TimeUnit)4 Nullable (javax.annotation.Nullable)4 Value (org.springframework.beans.factory.annotation.Value)4