Search in sources :

Example 1 with ApiUsageStateValue

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

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

the class DefaultTbApiUsageStateService method updateTenantState.

private void updateTenantState(TenantApiUsageState state, TenantProfile profile) {
    TenantProfileData oldProfileData = state.getTenantProfileData();
    state.setTenantProfileId(profile.getId());
    state.setTenantProfileData(profile.getProfileData());
    Map<ApiFeature, ApiUsageStateValue> result = state.checkStateUpdatedDueToThresholds();
    if (!result.isEmpty()) {
        persistAndNotify(state, result);
    }
    updateProfileThresholds(state.getTenantId(), state.getApiUsageState().getId(), oldProfileData.getConfiguration(), profile.getProfileData().getConfiguration());
}
Also used : ApiFeature(org.thingsboard.server.common.data.ApiFeature) TenantProfileData(org.thingsboard.server.common.data.tenant.profile.TenantProfileData) ApiUsageStateValue(org.thingsboard.server.common.data.ApiUsageStateValue)

Example 3 with ApiUsageStateValue

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

ApiUsageStateValue (org.thingsboard.server.common.data.ApiUsageStateValue)3 ApiFeature (org.thingsboard.server.common.data.ApiFeature)2 ApiUsageRecordKey (org.thingsboard.server.common.data.ApiUsageRecordKey)2 HashSet (java.util.HashSet)1 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)1 LongDataEntry (org.thingsboard.server.common.data.kv.LongDataEntry)1 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)1 TenantProfileData (org.thingsboard.server.common.data.tenant.profile.TenantProfileData)1 UsageStatsKVProto (org.thingsboard.server.gen.transport.TransportProtos.UsageStatsKVProto)1