Search in sources :

Example 11 with LongDataEntry

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

the class DefaultTbApiUsageStateService method saveNewCounts.

private void saveNewCounts(BaseApiUsageState state, List<ApiUsageRecordKey> keys) {
    List<TsKvEntry> counts = keys.stream().map(key -> new BasicTsKvEntry(state.getCurrentCycleTs(), new LongDataEntry(key.getApiCountKey(), 0L))).collect(Collectors.toList());
    tsWsService.saveAndNotifyInternal(state.getTenantId(), state.getApiUsageState().getId(), counts, VOID_CALLBACK);
}
Also used : UsageStatsKVProto(org.thingsboard.server.gen.transport.TransportProtos.UsageStatsKVProto) Arrays(java.util.Arrays) ApiUsageRecordKey(org.thingsboard.server.common.data.ApiUsageRecordKey) Autowired(org.springframework.beans.factory.annotation.Autowired) MailService(org.thingsboard.rule.engine.api.MailService) StringUtils(org.apache.commons.lang3.StringUtils) TenantId(org.thingsboard.server.common.data.id.TenantId) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) PreDestroy(javax.annotation.PreDestroy) TenantService(org.thingsboard.server.dao.tenant.TenantService) Map(java.util.Map) EntityType(org.thingsboard.server.common.data.EntityType) ApiUsageStateService(org.thingsboard.server.dao.usagerecord.ApiUsageStateService) TenantProfileData(org.thingsboard.server.common.data.tenant.profile.TenantProfileData) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TimeseriesService(org.thingsboard.server.dao.timeseries.TimeseriesService) Set(java.util.Set) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) UUID(java.util.UUID) ApiUsageStateId(org.thingsboard.server.common.data.id.ApiUsageStateId) Collectors(java.util.stream.Collectors) InternalTelemetryService(org.thingsboard.server.service.telemetry.InternalTelemetryService) Executors(java.util.concurrent.Executors) SchedulerUtils(org.thingsboard.server.common.msg.tools.SchedulerUtils) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) PostConstruct(javax.annotation.PostConstruct) TbClusterService(org.thingsboard.server.cluster.TbClusterService) TenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.TenantProfileConfiguration) Lazy(org.springframework.context.annotation.Lazy) TbTenantProfileCache(org.thingsboard.server.dao.tenant.TbTenantProfileCache) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) CustomerId(org.thingsboard.server.common.data.id.CustomerId) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) ApiUsageStateMailMessage(org.thingsboard.server.common.data.ApiUsageStateMailMessage) PartitionService(org.thingsboard.server.queue.discovery.PartitionService) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Tenant(org.thingsboard.server.common.data.Tenant) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) HashSet(java.util.HashSet) ToUsageStatsServiceMsg(org.thingsboard.server.gen.transport.TransportProtos.ToUsageStatsServiceMsg) PageDataIterable(org.thingsboard.server.common.data.page.PageDataIterable) TenantProfile(org.thingsboard.server.common.data.TenantProfile) ServiceType(org.thingsboard.server.common.msg.queue.ServiceType) Service(org.springframework.stereotype.Service) EntityId(org.thingsboard.server.common.data.id.EntityId) Nullable(org.checkerframework.checker.nullness.qual.Nullable) TbCallback(org.thingsboard.server.common.msg.queue.TbCallback) ExecutorService(java.util.concurrent.ExecutorService) AbstractPartitionBasedService(org.thingsboard.server.service.partition.AbstractPartitionBasedService) ReentrantLock(java.util.concurrent.locks.ReentrantLock) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) DbCallbackExecutorService(org.thingsboard.server.service.executors.DbCallbackExecutorService) ApiUsageStateValue(org.thingsboard.server.common.data.ApiUsageStateValue) FutureCallback(com.google.common.util.concurrent.FutureCallback) ApiUsageState(org.thingsboard.server.common.data.ApiUsageState) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Futures(com.google.common.util.concurrent.Futures) Lock(java.util.concurrent.locks.Lock) TenantProfileId(org.thingsboard.server.common.data.id.TenantProfileId) ApiFeature(org.thingsboard.server.common.data.ApiFeature) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) Collections(java.util.Collections) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry)

Example 12 with LongDataEntry

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

the class DefaultTbApiUsageStateService method processEntityUsageStats.

private void processEntityUsageStats(TenantId tenantId, EntityId entityId, List<UsageStatsKVProto> values) {
    if (deletedEntities.contains(entityId))
        return;
    BaseApiUsageState usageState;
    List<TsKvEntry> updatedEntries;
    Map<ApiFeature, ApiUsageStateValue> result;
    updateLock.lock();
    try {
        usageState = getOrFetchState(tenantId, entityId);
        long ts = usageState.getCurrentCycleTs();
        long hourTs = usageState.getCurrentHourTs();
        long newHourTs = SchedulerUtils.getStartOfCurrentHour();
        if (newHourTs != hourTs) {
            usageState.setHour(newHourTs);
        }
        updatedEntries = new ArrayList<>(ApiUsageRecordKey.values().length);
        Set<ApiFeature> apiFeatures = new HashSet<>();
        for (UsageStatsKVProto kvProto : values) {
            ApiUsageRecordKey recordKey = ApiUsageRecordKey.valueOf(kvProto.getKey());
            long newValue = usageState.add(recordKey, kvProto.getValue());
            updatedEntries.add(new BasicTsKvEntry(ts, new LongDataEntry(recordKey.getApiCountKey(), newValue)));
            long newHourlyValue = usageState.addToHourly(recordKey, kvProto.getValue());
            updatedEntries.add(new BasicTsKvEntry(newHourTs, new LongDataEntry(recordKey.getApiCountKey() + HOURLY, newHourlyValue)));
            apiFeatures.add(recordKey.getApiFeature());
        }
        if (usageState.getEntityType() == EntityType.TENANT && !usageState.getEntityId().equals(TenantId.SYS_TENANT_ID)) {
            result = ((TenantApiUsageState) usageState).checkStateUpdatedDueToThreshold(apiFeatures);
        } else {
            result = Collections.emptyMap();
        }
    } finally {
        updateLock.unlock();
    }
    tsWsService.saveAndNotifyInternal(tenantId, usageState.getApiUsageState().getId(), updatedEntries, VOID_CALLBACK);
    if (!result.isEmpty()) {
        persistAndNotify(usageState, result);
    }
}
Also used : BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) ApiUsageRecordKey(org.thingsboard.server.common.data.ApiUsageRecordKey) ApiFeature(org.thingsboard.server.common.data.ApiFeature) ApiUsageStateValue(org.thingsboard.server.common.data.ApiUsageStateValue) UsageStatsKVProto(org.thingsboard.server.gen.transport.TransportProtos.UsageStatsKVProto) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) HashSet(java.util.HashSet)

Example 13 with LongDataEntry

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

the class DefaultOtaPackageStateService method updateAttributes.

private void updateAttributes(Device device, OtaPackageInfo otaPackage, long ts, TenantId tenantId, DeviceId deviceId, OtaPackageType otaPackageType) {
    List<AttributeKvEntry> attributes = new ArrayList<>();
    List<String> attrToRemove = new ArrayList<>();
    attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, TITLE), otaPackage.getTitle())));
    attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, VERSION), otaPackage.getVersion())));
    if (StringUtils.isNotEmpty(otaPackage.getTag())) {
        attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, TAG), otaPackage.getTag())));
    } else {
        attrToRemove.add(getAttributeKey(otaPackageType, TAG));
    }
    if (otaPackage.hasUrl()) {
        attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, URL), otaPackage.getUrl())));
        if (otaPackage.getDataSize() == null) {
            attrToRemove.add(getAttributeKey(otaPackageType, SIZE));
        } else {
            attributes.add(new BaseAttributeKvEntry(ts, new LongDataEntry(getAttributeKey(otaPackageType, SIZE), otaPackage.getDataSize())));
        }
        if (otaPackage.getChecksumAlgorithm() != null) {
            attrToRemove.add(getAttributeKey(otaPackageType, CHECKSUM_ALGORITHM));
        } else {
            attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, CHECKSUM_ALGORITHM), otaPackage.getChecksumAlgorithm().name())));
        }
        if (StringUtils.isEmpty(otaPackage.getChecksum())) {
            attrToRemove.add(getAttributeKey(otaPackageType, CHECKSUM));
        } else {
            attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, CHECKSUM), otaPackage.getChecksum())));
        }
    } else {
        attributes.add(new BaseAttributeKvEntry(ts, new LongDataEntry(getAttributeKey(otaPackageType, SIZE), otaPackage.getDataSize())));
        attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, CHECKSUM_ALGORITHM), otaPackage.getChecksumAlgorithm().name())));
        attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, CHECKSUM), otaPackage.getChecksum())));
        attrToRemove.add(getAttributeKey(otaPackageType, URL));
    }
    remove(device, otaPackageType, attrToRemove);
    telemetryService.saveAndNotify(tenantId, deviceId, DataConstants.SHARED_SCOPE, attributes, new FutureCallback<>() {

        @Override
        public void onSuccess(@Nullable Void tmp) {
            log.trace("[{}] Success save attributes with target firmware!", deviceId);
        }

        @Override
        public void onFailure(Throwable t) {
            log.error("[{}] Failed to save attributes with target firmware!", deviceId, t);
        }
    });
}
Also used : AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) ArrayList(java.util.ArrayList) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry)

Example 14 with LongDataEntry

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

the class TbSubscriptionUtils method getKvEntry.

private static KvEntry getKvEntry(KeyValueProto proto) {
    KvEntry entry = null;
    DataType type = DataType.values()[proto.getType().getNumber()];
    switch(type) {
        case BOOLEAN:
            entry = new BooleanDataEntry(proto.getKey(), proto.getBoolV());
            break;
        case LONG:
            entry = new LongDataEntry(proto.getKey(), proto.getLongV());
            break;
        case DOUBLE:
            entry = new DoubleDataEntry(proto.getKey(), proto.getDoubleV());
            break;
        case STRING:
            entry = new StringDataEntry(proto.getKey(), proto.getStringV());
            break;
        case JSON:
            entry = new JsonDataEntry(proto.getKey(), proto.getJsonV());
            break;
    }
    return entry;
}
Also used : BooleanDataEntry(org.thingsboard.server.common.data.kv.BooleanDataEntry) DoubleDataEntry(org.thingsboard.server.common.data.kv.DoubleDataEntry) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) DataType(org.thingsboard.server.common.data.kv.DataType) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) JsonDataEntry(org.thingsboard.server.common.data.kv.JsonDataEntry)

Example 15 with LongDataEntry

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

the class DefaultRuleEngineStatisticsService method reportQueueStats.

@Override
public void reportQueueStats(long ts, TbRuleEngineConsumerStats ruleEngineStats) {
    String queueName = ruleEngineStats.getQueueName();
    ruleEngineStats.getTenantStats().forEach((id, stats) -> {
        TenantId tenantId = TenantId.fromUUID(id);
        try {
            AssetId serviceAssetId = getServiceAssetId(tenantId, queueName);
            if (stats.getTotalMsgCounter().get() > 0) {
                List<TsKvEntry> tsList = stats.getCounters().entrySet().stream().map(kv -> new BasicTsKvEntry(ts, new LongDataEntry(kv.getKey(), (long) kv.getValue().get()))).collect(Collectors.toList());
                if (!tsList.isEmpty()) {
                    tsService.saveAndNotifyInternal(tenantId, serviceAssetId, tsList, CALLBACK);
                }
            }
        } catch (DataValidationException e) {
            if (!e.getMessage().equalsIgnoreCase("Asset is referencing to non-existent tenant!")) {
                throw e;
            }
        }
    });
    ruleEngineStats.getTenantExceptions().forEach((tenantId, e) -> {
        TsKvEntry tsKv = new BasicTsKvEntry(e.getTs(), new JsonDataEntry("ruleEngineException", e.toJsonString()));
        try {
            tsService.saveAndNotifyInternal(tenantId, getServiceAssetId(tenantId, queueName), Collections.singletonList(tsKv), CALLBACK);
        } catch (DataValidationException e2) {
            if (!e2.getMessage().equalsIgnoreCase("Asset is referencing to non-existent tenant!")) {
                throw e2;
            }
        }
    });
}
Also used : JacksonUtil(org.thingsboard.common.util.JacksonUtil) AssetId(org.thingsboard.server.common.data.id.AssetId) TelemetrySubscriptionService(org.thingsboard.server.service.telemetry.TelemetrySubscriptionService) TenantId(org.thingsboard.server.common.data.id.TenantId) ConcurrentMap(java.util.concurrent.ConcurrentMap) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) Service(org.springframework.stereotype.Service) TbRuleEngineComponent(org.thingsboard.server.queue.util.TbRuleEngineComponent) TbRuleEngineConsumerStats(org.thingsboard.server.service.queue.TbRuleEngineConsumerStats) TbServiceInfoProvider(org.thingsboard.server.queue.discovery.TbServiceInfoProvider) Nullable(javax.annotation.Nullable) AssetService(org.thingsboard.server.dao.asset.AssetService) ReentrantLock(java.util.concurrent.locks.ReentrantLock) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Collectors(java.util.stream.Collectors) FutureCallback(com.google.common.util.concurrent.FutureCallback) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Lock(java.util.concurrent.locks.Lock) JsonDataEntry(org.thingsboard.server.common.data.kv.JsonDataEntry) Data(lombok.Data) Collections(java.util.Collections) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) Asset(org.thingsboard.server.common.data.asset.Asset) TenantId(org.thingsboard.server.common.data.id.TenantId) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) AssetId(org.thingsboard.server.common.data.id.AssetId) JsonDataEntry(org.thingsboard.server.common.data.kv.JsonDataEntry)

Aggregations

LongDataEntry (org.thingsboard.server.common.data.kv.LongDataEntry)24 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)16 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)14 BaseAttributeKvEntry (org.thingsboard.server.common.data.kv.BaseAttributeKvEntry)9 StringDataEntry (org.thingsboard.server.common.data.kv.StringDataEntry)9 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)8 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 Device (org.thingsboard.server.common.data.Device)7 DeviceTypeFilter (org.thingsboard.server.common.data.query.DeviceTypeFilter)7 TelemetryPluginCmdsWrapper (org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper)7 DoubleDataEntry (org.thingsboard.server.common.data.kv.DoubleDataEntry)6 EntityData (org.thingsboard.server.common.data.query.EntityData)6 EntityDataPageLink (org.thingsboard.server.common.data.query.EntityDataPageLink)6 EntityDataQuery (org.thingsboard.server.common.data.query.EntityDataQuery)6 TsValue (org.thingsboard.server.common.data.query.TsValue)6 BooleanDataEntry (org.thingsboard.server.common.data.kv.BooleanDataEntry)5 JsonDataEntry (org.thingsboard.server.common.data.kv.JsonDataEntry)5 EntityKey (org.thingsboard.server.common.data.query.EntityKey)5 ApiUsageRecordKey (org.thingsboard.server.common.data.ApiUsageRecordKey)4