Search in sources :

Example 1 with TenantProfileConfiguration

use of org.thingsboard.server.common.data.tenant.profile.TenantProfileConfiguration in project thingsboard by thingsboard.

the class ApiUsageStateServiceImpl method createDefaultApiUsageState.

@Override
public ApiUsageState createDefaultApiUsageState(TenantId tenantId, EntityId entityId) {
    entityId = Objects.requireNonNullElse(entityId, tenantId);
    log.trace("Executing createDefaultUsageRecord [{}]", entityId);
    validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
    ApiUsageState apiUsageState = new ApiUsageState();
    apiUsageState.setTenantId(tenantId);
    apiUsageState.setEntityId(entityId);
    apiUsageState.setTransportState(ApiUsageStateValue.ENABLED);
    apiUsageState.setReExecState(ApiUsageStateValue.ENABLED);
    apiUsageState.setJsExecState(ApiUsageStateValue.ENABLED);
    apiUsageState.setDbStorageState(ApiUsageStateValue.ENABLED);
    apiUsageState.setSmsExecState(ApiUsageStateValue.ENABLED);
    apiUsageState.setEmailExecState(ApiUsageStateValue.ENABLED);
    apiUsageState.setAlarmExecState(ApiUsageStateValue.ENABLED);
    apiUsageStateValidator.validate(apiUsageState, ApiUsageState::getTenantId);
    ApiUsageState saved = apiUsageStateDao.save(apiUsageState.getTenantId(), apiUsageState);
    List<TsKvEntry> apiUsageStates = new ArrayList<>();
    apiUsageStates.add(new BasicTsKvEntry(saved.getCreatedTime(), new StringDataEntry(ApiFeature.TRANSPORT.getApiStateKey(), ApiUsageStateValue.ENABLED.name())));
    apiUsageStates.add(new BasicTsKvEntry(saved.getCreatedTime(), new StringDataEntry(ApiFeature.DB.getApiStateKey(), ApiUsageStateValue.ENABLED.name())));
    apiUsageStates.add(new BasicTsKvEntry(saved.getCreatedTime(), new StringDataEntry(ApiFeature.RE.getApiStateKey(), ApiUsageStateValue.ENABLED.name())));
    apiUsageStates.add(new BasicTsKvEntry(saved.getCreatedTime(), new StringDataEntry(ApiFeature.JS.getApiStateKey(), ApiUsageStateValue.ENABLED.name())));
    apiUsageStates.add(new BasicTsKvEntry(saved.getCreatedTime(), new StringDataEntry(ApiFeature.EMAIL.getApiStateKey(), ApiUsageStateValue.ENABLED.name())));
    apiUsageStates.add(new BasicTsKvEntry(saved.getCreatedTime(), new StringDataEntry(ApiFeature.SMS.getApiStateKey(), ApiUsageStateValue.ENABLED.name())));
    apiUsageStates.add(new BasicTsKvEntry(saved.getCreatedTime(), new StringDataEntry(ApiFeature.ALARM.getApiStateKey(), ApiUsageStateValue.ENABLED.name())));
    tsService.save(tenantId, saved.getId(), apiUsageStates, 0L);
    if (entityId.getEntityType() == EntityType.TENANT && !entityId.equals(TenantId.SYS_TENANT_ID)) {
        tenantId = (TenantId) entityId;
        Tenant tenant = tenantDao.findById(tenantId, tenantId.getId());
        TenantProfile tenantProfile = tenantProfileDao.findById(tenantId, tenant.getTenantProfileId().getId());
        TenantProfileConfiguration configuration = tenantProfile.getProfileData().getConfiguration();
        List<TsKvEntry> profileThresholds = new ArrayList<>();
        for (ApiUsageRecordKey key : ApiUsageRecordKey.values()) {
            profileThresholds.add(new BasicTsKvEntry(saved.getCreatedTime(), new LongDataEntry(key.getApiLimitKey(), configuration.getProfileThreshold(key))));
        }
        tsService.save(tenantId, saved.getId(), profileThresholds, 0L);
    }
    return saved;
}
Also used : BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) Tenant(org.thingsboard.server.common.data.Tenant) TenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.TenantProfileConfiguration) ApiUsageState(org.thingsboard.server.common.data.ApiUsageState) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) ArrayList(java.util.ArrayList) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) TenantProfile(org.thingsboard.server.common.data.TenantProfile) ApiUsageRecordKey(org.thingsboard.server.common.data.ApiUsageRecordKey)

Aggregations

ArrayList (java.util.ArrayList)1 ApiUsageRecordKey (org.thingsboard.server.common.data.ApiUsageRecordKey)1 ApiUsageState (org.thingsboard.server.common.data.ApiUsageState)1 Tenant (org.thingsboard.server.common.data.Tenant)1 TenantProfile (org.thingsboard.server.common.data.TenantProfile)1 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)1 LongDataEntry (org.thingsboard.server.common.data.kv.LongDataEntry)1 StringDataEntry (org.thingsboard.server.common.data.kv.StringDataEntry)1 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)1 TenantProfileConfiguration (org.thingsboard.server.common.data.tenant.profile.TenantProfileConfiguration)1