Search in sources :

Example 11 with DefaultTenantProfileConfiguration

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

the class AbstractBulkImportService method saveTelemetry.

@SneakyThrows
private void saveTelemetry(SecurityUser user, E entity, Map.Entry<BulkImportColumnType, JsonObject> kvsEntry) {
    List<TsKvEntry> timeseries = JsonConverter.convertToTelemetry(kvsEntry.getValue(), System.currentTimeMillis()).entrySet().stream().flatMap(entry -> entry.getValue().stream().map(kvEntry -> new BasicTsKvEntry(entry.getKey(), kvEntry))).collect(Collectors.toList());
    accessValidator.validateEntityAndCallback(user, Operation.WRITE_TELEMETRY, entity.getId(), (result, tenantId, entityId) -> {
        TenantProfile tenantProfile = tenantProfileCache.get(tenantId);
        long tenantTtl = TimeUnit.DAYS.toSeconds(((DefaultTenantProfileConfiguration) tenantProfile.getProfileData().getConfiguration()).getDefaultStorageTtlDays());
        tsSubscriptionService.saveAndNotify(tenantId, user.getCustomerId(), entityId, timeseries, tenantTtl, new FutureCallback<Void>() {

            @Override
            public void onSuccess(@Nullable Void tmp) {
                entityActionService.logEntityAction(user, (UUIDBased & EntityId) entityId, null, null, ActionType.TIMESERIES_UPDATED, null, timeseries);
            }

            @Override
            public void onFailure(Throwable t) {
                entityActionService.logEntityAction(user, (UUIDBased & EntityId) entityId, null, null, ActionType.TIMESERIES_UPDATED, BaseController.toException(t), timeseries);
                throw new RuntimeException(t);
            }
        });
    });
}
Also used : JsonObject(com.google.gson.JsonObject) Arrays(java.util.Arrays) CsvUtils(org.thingsboard.server.utils.CsvUtils) SneakyThrows(lombok.SneakyThrows) DonAsynchron(org.thingsboard.common.util.DonAsynchron) Autowired(org.springframework.beans.factory.annotation.Autowired) DefaultTenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration) TelemetrySubscriptionService(org.thingsboard.server.service.telemetry.TelemetrySubscriptionService) StringUtils(org.apache.commons.lang3.StringUtils) TenantId(org.thingsboard.server.common.data.id.TenantId) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) PreDestroy(javax.annotation.PreDestroy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) EntityType(org.thingsboard.server.common.data.EntityType) JsonConverter(org.thingsboard.server.common.transport.adaptor.JsonConverter) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) TypeCastUtil(org.thingsboard.server.utils.TypeCastUtil) HasTenantId(org.thingsboard.server.common.data.HasTenantId) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) SecurityContext(org.springframework.security.core.context.SecurityContext) PostConstruct(javax.annotation.PostConstruct) HasId(org.thingsboard.server.common.data.id.HasId) Operation(org.thingsboard.server.service.security.permission.Operation) TbTenantProfileCache(org.thingsboard.server.dao.tenant.TbTenantProfileCache) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) TenantProfile(org.thingsboard.server.common.data.TenantProfile) ActionType(org.thingsboard.server.common.data.audit.ActionType) DataType(org.thingsboard.server.common.data.kv.DataType) EntityId(org.thingsboard.server.common.data.id.EntityId) BaseController(org.thingsboard.server.controller.BaseController) JsonPrimitive(com.google.gson.JsonPrimitive) UUIDBased(org.thingsboard.server.common.data.id.UUIDBased) Nullable(javax.annotation.Nullable) AccessValidator(org.thingsboard.server.service.security.AccessValidator) FutureCallback(com.google.common.util.concurrent.FutureCallback) ColumnMapping(org.thingsboard.server.service.importing.BulkImportRequest.ColumnMapping) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) EntityActionService(org.thingsboard.server.service.action.EntityActionService) Data(lombok.Data) Resource(org.thingsboard.server.service.security.permission.Resource) AccessControlService(org.thingsboard.server.service.security.permission.AccessControlService) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TenantProfile(org.thingsboard.server.common.data.TenantProfile) SneakyThrows(lombok.SneakyThrows)

Example 12 with DefaultTenantProfileConfiguration

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

the class AlarmsCleanUpService method cleanUp.

@Scheduled(initialDelayString = "#{T(org.apache.commons.lang3.RandomUtils).nextLong(0, ${sql.ttl.alarms.checking_interval})}", fixedDelayString = "${sql.ttl.alarms.checking_interval}")
public void cleanUp() {
    PageLink tenantsBatchRequest = new PageLink(10_000, 0);
    PageLink removalBatchRequest = new PageLink(removalBatchSize, 0);
    PageData<TenantId> tenantsIds;
    do {
        tenantsIds = tenantDao.findTenantsIds(tenantsBatchRequest);
        for (TenantId tenantId : tenantsIds.getData()) {
            if (!partitionService.resolve(ServiceType.TB_CORE, tenantId, tenantId).isMyPartition()) {
                continue;
            }
            Optional<DefaultTenantProfileConfiguration> tenantProfileConfiguration = tenantProfileCache.get(tenantId).getProfileConfiguration();
            if (tenantProfileConfiguration.isEmpty() || tenantProfileConfiguration.get().getAlarmsTtlDays() == 0) {
                continue;
            }
            long ttl = TimeUnit.DAYS.toMillis(tenantProfileConfiguration.get().getAlarmsTtlDays());
            long expirationTime = System.currentTimeMillis() - ttl;
            long totalRemoved = 0;
            while (true) {
                PageData<AlarmId> toRemove = alarmDao.findAlarmsIdsByEndTsBeforeAndTenantId(expirationTime, tenantId, removalBatchRequest);
                toRemove.getData().forEach(alarmId -> {
                    relationService.deleteEntityRelations(tenantId, alarmId);
                    Alarm alarm = alarmService.deleteAlarm(tenantId, alarmId).getAlarm();
                    entityActionService.pushEntityActionToRuleEngine(alarm.getOriginator(), alarm, tenantId, null, ActionType.ALARM_DELETE, null);
                });
                totalRemoved += toRemove.getTotalElements();
                if (!toRemove.hasNext()) {
                    break;
                }
            }
            if (totalRemoved > 0) {
                log.info("Removed {} outdated alarm(s) for tenant {} older than {}", totalRemoved, tenantId, new Date(expirationTime));
            }
        }
        tenantsBatchRequest = tenantsBatchRequest.nextPageLink();
    } while (tenantsIds.hasNext());
}
Also used : AlarmId(org.thingsboard.server.common.data.id.AlarmId) TenantId(org.thingsboard.server.common.data.id.TenantId) Alarm(org.thingsboard.server.common.data.alarm.Alarm) PageLink(org.thingsboard.server.common.data.page.PageLink) DefaultTenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration) Date(java.util.Date) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 13 with DefaultTenantProfileConfiguration

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

the class RpcCleanUpService method cleanUp.

@Scheduled(initialDelayString = "#{T(org.apache.commons.lang3.RandomUtils).nextLong(0, ${sql.ttl.rpc.checking_interval})}", fixedDelayString = "${sql.ttl.rpc.checking_interval}")
public void cleanUp() {
    if (ttlTaskExecutionEnabled) {
        PageLink tenantsBatchRequest = new PageLink(10_000, 0);
        PageData<TenantId> tenantsIds;
        do {
            tenantsIds = tenantDao.findTenantsIds(tenantsBatchRequest);
            for (TenantId tenantId : tenantsIds.getData()) {
                if (!partitionService.resolve(ServiceType.TB_CORE, tenantId, tenantId).isMyPartition()) {
                    continue;
                }
                Optional<DefaultTenantProfileConfiguration> tenantProfileConfiguration = tenantProfileCache.get(tenantId).getProfileConfiguration();
                if (tenantProfileConfiguration.isEmpty() || tenantProfileConfiguration.get().getRpcTtlDays() == 0) {
                    continue;
                }
                long ttl = TimeUnit.DAYS.toMillis(tenantProfileConfiguration.get().getRpcTtlDays());
                long expirationTime = System.currentTimeMillis() - ttl;
                long totalRemoved = rpcDao.deleteOutdatedRpcByTenantId(tenantId, expirationTime);
                if (totalRemoved > 0) {
                    log.info("Removed {} outdated rpc(s) for tenant {} older than {}", totalRemoved, tenantId, new Date(expirationTime));
                }
            }
            tenantsBatchRequest = tenantsBatchRequest.nextPageLink();
        } while (tenantsIds.hasNext());
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) PageLink(org.thingsboard.server.common.data.page.PageLink) DefaultTenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration) Date(java.util.Date) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 14 with DefaultTenantProfileConfiguration

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

the class BaseTenantProfileControllerTest method createTenantProfile.

private TenantProfile createTenantProfile(String name) {
    TenantProfile tenantProfile = new TenantProfile();
    tenantProfile.setName(name);
    tenantProfile.setDescription(name + " Test");
    TenantProfileData tenantProfileData = new TenantProfileData();
    tenantProfileData.setConfiguration(new DefaultTenantProfileConfiguration());
    tenantProfile.setProfileData(tenantProfileData);
    tenantProfile.setDefault(false);
    tenantProfile.setIsolatedTbCore(false);
    tenantProfile.setIsolatedTbRuleEngine(false);
    return tenantProfile;
}
Also used : TenantProfileData(org.thingsboard.server.common.data.tenant.profile.TenantProfileData) TenantProfile(org.thingsboard.server.common.data.TenantProfile) DefaultTenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration)

Example 15 with DefaultTenantProfileConfiguration

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

the class BaseEdgeTest method testDeviceReachedMaximumAllowedOnCloud.

@Test
public void testDeviceReachedMaximumAllowedOnCloud() throws Exception {
    // update tenant profile configuration
    loginSysAdmin();
    TenantProfile tenantProfile = doGet("/api/tenantProfile/" + savedTenant.getTenantProfileId().getId(), TenantProfile.class);
    DefaultTenantProfileConfiguration profileConfiguration = (DefaultTenantProfileConfiguration) tenantProfile.getProfileData().getConfiguration();
    profileConfiguration.setMaxDevices(1);
    tenantProfile.getProfileData().setConfiguration(profileConfiguration);
    doPost("/api/tenantProfile/", tenantProfile, TenantProfile.class);
    loginTenantAdmin();
    UUID uuid = Uuids.timeBased();
    UplinkMsg.Builder uplinkMsgBuilder = UplinkMsg.newBuilder();
    DeviceUpdateMsg.Builder deviceUpdateMsgBuilder = DeviceUpdateMsg.newBuilder();
    deviceUpdateMsgBuilder.setIdMSB(uuid.getMostSignificantBits());
    deviceUpdateMsgBuilder.setIdLSB(uuid.getLeastSignificantBits());
    deviceUpdateMsgBuilder.setName("Edge Device");
    deviceUpdateMsgBuilder.setType("default");
    deviceUpdateMsgBuilder.setMsgType(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE);
    uplinkMsgBuilder.addDeviceUpdateMsg(deviceUpdateMsgBuilder.build());
    edgeImitator.expectResponsesAmount(1);
    edgeImitator.sendUplinkMsg(uplinkMsgBuilder.build());
    Assert.assertTrue(edgeImitator.waitForResponses());
    UplinkResponseMsg latestResponseMsg = edgeImitator.getLatestResponseMsg();
    Assert.assertTrue(latestResponseMsg.getSuccess());
}
Also used : UplinkMsg(org.thingsboard.server.gen.edge.v1.UplinkMsg) DeviceUpdateMsg(org.thingsboard.server.gen.edge.v1.DeviceUpdateMsg) TenantProfile(org.thingsboard.server.common.data.TenantProfile) UUID(java.util.UUID) DefaultTenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration) UplinkResponseMsg(org.thingsboard.server.gen.edge.v1.UplinkResponseMsg) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest) Test(org.junit.Test)

Aggregations

DefaultTenantProfileConfiguration (org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration)22 TenantProfile (org.thingsboard.server.common.data.TenantProfile)8 TenantProfileData (org.thingsboard.server.common.data.tenant.profile.TenantProfileData)7 TenantId (org.thingsboard.server.common.data.id.TenantId)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 Test (org.junit.Test)2 Scheduled (org.springframework.scheduling.annotation.Scheduled)2 PageLink (org.thingsboard.server.common.data.page.PageLink)2 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)2 FutureCallback (com.google.common.util.concurrent.FutureCallback)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 JsonParser (com.google.gson.JsonParser)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 Arrays (java.util.Arrays)1