Search in sources :

Example 1 with UsageStatsKVProto

use of org.thingsboard.server.gen.transport.TransportProtos.UsageStatsKVProto 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

HashSet (java.util.HashSet)1 ApiFeature (org.thingsboard.server.common.data.ApiFeature)1 ApiUsageRecordKey (org.thingsboard.server.common.data.ApiUsageRecordKey)1 ApiUsageStateValue (org.thingsboard.server.common.data.ApiUsageStateValue)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 UsageStatsKVProto (org.thingsboard.server.gen.transport.TransportProtos.UsageStatsKVProto)1