Search in sources :

Example 1 with ApiUsageRecordKey

use of org.thingsboard.server.common.data.ApiUsageRecordKey in project thingsboard by thingsboard.

the class DefaultTbApiUsageStateService method updateProfileThresholds.

private void updateProfileThresholds(TenantId tenantId, ApiUsageStateId id, TenantProfileConfiguration oldData, TenantProfileConfiguration newData) {
    long ts = System.currentTimeMillis();
    List<TsKvEntry> profileThresholds = new ArrayList<>();
    for (ApiUsageRecordKey key : ApiUsageRecordKey.values()) {
        long newProfileThreshold = newData.getProfileThreshold(key);
        if (oldData == null || oldData.getProfileThreshold(key) != newProfileThreshold) {
            log.info("[{}] Updating profile threshold [{}]:[{}]", tenantId, key, newProfileThreshold);
            profileThresholds.add(new BasicTsKvEntry(ts, new LongDataEntry(key.getApiLimitKey(), newProfileThreshold)));
        }
    }
    if (!profileThresholds.isEmpty()) {
        tsWsService.saveAndNotifyInternal(tenantId, id, profileThresholds, VOID_CALLBACK);
    }
}
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) ArrayList(java.util.ArrayList) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) ApiUsageRecordKey(org.thingsboard.server.common.data.ApiUsageRecordKey)

Example 2 with ApiUsageRecordKey

use of org.thingsboard.server.common.data.ApiUsageRecordKey in project thingsboard by thingsboard.

the class TenantApiUsageState method checkStateUpdatedDueToThreshold.

private Pair<ApiFeature, ApiUsageStateValue> checkStateUpdatedDueToThreshold(ApiFeature feature) {
    ApiUsageStateValue featureValue = ApiUsageStateValue.ENABLED;
    for (ApiUsageRecordKey recordKey : ApiUsageRecordKey.getKeys(feature)) {
        long value = get(recordKey);
        long threshold = getProfileThreshold(recordKey);
        long warnThreshold = getProfileWarnThreshold(recordKey);
        ApiUsageStateValue tmpValue;
        if (threshold == 0 || value == 0 || value < warnThreshold) {
            tmpValue = ApiUsageStateValue.ENABLED;
        } else if (value < threshold) {
            tmpValue = ApiUsageStateValue.WARNING;
        } else {
            tmpValue = ApiUsageStateValue.DISABLED;
        }
        featureValue = ApiUsageStateValue.toMoreRestricted(featureValue, tmpValue);
    }
    return setFeatureValue(feature, featureValue) ? Pair.of(feature, featureValue) : null;
}
Also used : ApiUsageStateValue(org.thingsboard.server.common.data.ApiUsageStateValue) ApiUsageRecordKey(org.thingsboard.server.common.data.ApiUsageRecordKey)

Example 3 with ApiUsageRecordKey

use of org.thingsboard.server.common.data.ApiUsageRecordKey in project thingsboard by thingsboard.

the class BaseApiUsageState method setCycles.

public void setCycles(long currentCycleTs, long nextCycleTs) {
    this.currentCycleTs = currentCycleTs;
    this.nextCycleTs = nextCycleTs;
    for (ApiUsageRecordKey key : ApiUsageRecordKey.values()) {
        currentCycleValues.put(key, 0L);
    }
}
Also used : ApiUsageRecordKey(org.thingsboard.server.common.data.ApiUsageRecordKey)

Example 4 with ApiUsageRecordKey

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

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

Aggregations

ApiUsageRecordKey (org.thingsboard.server.common.data.ApiUsageRecordKey)10 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)5 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)5 ArrayList (java.util.ArrayList)4 LongDataEntry (org.thingsboard.server.common.data.kv.LongDataEntry)4 PostConstruct (javax.annotation.PostConstruct)3 ApiUsageState (org.thingsboard.server.common.data.ApiUsageState)3 ApiUsageStateValue (org.thingsboard.server.common.data.ApiUsageStateValue)3 TopicPartitionInfo (org.thingsboard.server.common.msg.queue.TopicPartitionInfo)3 UsageStatsKVProto (org.thingsboard.server.gen.transport.TransportProtos.UsageStatsKVProto)3 HashSet (java.util.HashSet)2 Random (java.util.Random)2 UUID (java.util.UUID)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeUnit (java.util.concurrent.TimeUnit)2 Slf4j (lombok.extern.slf4j.Slf4j)2 Value (org.springframework.beans.factory.annotation.Value)2 ApiFeature (org.thingsboard.server.common.data.ApiFeature)2 ApiUsageStateMailMessage (org.thingsboard.server.common.data.ApiUsageStateMailMessage)2