Search in sources :

Example 1 with PageLink

use of org.thingsboard.server.common.data.page.PageLink in project thingsboard by thingsboard.

the class DefaultDeviceStateService method processPageAndSubmitNextPage.

private void processPageAndSubmitNextPage(final Set<TopicPartitionInfo> addedPartitions, final Tenant tenant, final PageLink pageLink) {
    log.trace("[{}] Process page {} from {}", tenant, pageLink.getPage(), pageLink.getPageSize());
    List<ListenableFuture<Void>> fetchFutures = new ArrayList<>();
    PageData<Device> page = deviceService.findDevicesByTenantId(tenant.getId(), pageLink);
    for (Device device : page.getData()) {
        TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, tenant.getId(), device.getId());
        if (addedPartitions.contains(tpi) && !deviceStates.containsKey(device.getId())) {
            log.debug("[{}][{}] Device belong to current partition. tpi [{}]. Fetching state from DB", device.getName(), device.getId(), tpi);
            ListenableFuture<Void> future = Futures.transform(fetchDeviceState(device), new Function<>() {

                @Nullable
                @Override
                public Void apply(@Nullable DeviceStateData state) {
                    if (state != null) {
                        addDeviceUsingState(tpi, state);
                        checkAndUpdateState(device.getId(), state);
                    } else {
                        log.warn("{}][{}] Fetched null state from DB", device.getName(), device.getId());
                    }
                    return null;
                }
            }, deviceStateExecutor);
            fetchFutures.add(future);
        } else {
            log.debug("[{}][{}] Device doesn't belong to current partition. tpi [{}]", device.getName(), device.getId(), tpi);
        }
    }
    Futures.addCallback(Futures.successfulAsList(fetchFutures), new FutureCallback<>() {

        @Override
        public void onSuccess(List<Void> result) {
            log.trace("[{}] Success init device state from DB for batch size {}", tenant.getId(), result.size());
        }

        @Override
        public void onFailure(Throwable t) {
            log.warn("[" + tenant.getId() + "] Failed to init device state service from DB", t);
            log.warn("[{}] Failed to init device state service from DB", tenant.getId(), t);
        }
    }, deviceStateExecutor);
    final PageLink nextPageLink = page.hasNext() ? pageLink.nextPageLink() : null;
    if (nextPageLink != null) {
        log.trace("[{}] Submit next page {} from {}", tenant, nextPageLink.getPage(), nextPageLink.getPageSize());
        processPageAndSubmitNextPage(addedPartitions, tenant, nextPageLink);
    }
}
Also used : Device(org.thingsboard.server.common.data.Device) ArrayList(java.util.ArrayList) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) PageLink(org.thingsboard.server.common.data.page.PageLink) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Nullable(javax.annotation.Nullable)

Example 2 with PageLink

use of org.thingsboard.server.common.data.page.PageLink in project thingsboard by thingsboard.

the class AssetController method getTenantAssetInfos.

@ApiOperation(value = "Get Tenant Asset Infos (getTenantAssetInfos)", notes = "Returns a page of assets info objects owned by tenant. " + PAGE_DATA_PARAMETERS + ASSET_INFO_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/tenant/assetInfos", params = { "pageSize", "page" }, method = RequestMethod.GET)
@ResponseBody
public PageData<AssetInfo> getTenantAssetInfos(@ApiParam(value = PAGE_SIZE_DESCRIPTION) @RequestParam int pageSize, @ApiParam(value = PAGE_NUMBER_DESCRIPTION) @RequestParam int page, @ApiParam(value = ASSET_TYPE_DESCRIPTION) @RequestParam(required = false) String type, @ApiParam(value = ASSET_TEXT_SEARCH_DESCRIPTION) @RequestParam(required = false) String textSearch, @ApiParam(value = SORT_PROPERTY_DESCRIPTION, allowableValues = ASSET_SORT_PROPERTY_ALLOWABLE_VALUES) @RequestParam(required = false) String sortProperty, @ApiParam(value = SORT_ORDER_DESCRIPTION, allowableValues = SORT_ORDER_ALLOWABLE_VALUES) @RequestParam(required = false) String sortOrder) throws ThingsboardException {
    try {
        TenantId tenantId = getCurrentUser().getTenantId();
        PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder);
        if (type != null && type.trim().length() > 0) {
            return checkNotNull(assetService.findAssetInfosByTenantIdAndType(tenantId, type, pageLink));
        } else {
            return checkNotNull(assetService.findAssetInfosByTenantId(tenantId, pageLink));
        }
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) PageLink(org.thingsboard.server.common.data.page.PageLink) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with PageLink

use of org.thingsboard.server.common.data.page.PageLink in project thingsboard by thingsboard.

the class AbstractOAuth2ClientMapper method getDashboardId.

private Optional<DashboardId> getDashboardId(TenantId tenantId, CustomerId customerId, String dashboardName) {
    PageData<DashboardInfo> dashboardsPage;
    PageLink pageLink = null;
    do {
        pageLink = pageLink == null ? new PageLink(DASHBOARDS_REQUEST_LIMIT) : pageLink.nextPageLink();
        dashboardsPage = dashboardService.findDashboardsByTenantIdAndCustomerId(tenantId, customerId, pageLink);
        Optional<DashboardInfo> dashboardInfoOpt = dashboardsPage.getData().stream().filter(dashboardInfo -> dashboardName.equals(dashboardInfo.getName())).findAny();
        if (dashboardInfoOpt.isPresent()) {
            return dashboardInfoOpt.map(DashboardInfo::getId);
        }
    } while (dashboardsPage.hasNext());
    return Optional.empty();
}
Also used : UserService(org.thingsboard.server.dao.user.UserService) OAuth2User(org.thingsboard.server.dao.oauth2.OAuth2User) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) Getter(lombok.Getter) Customer(org.thingsboard.server.common.data.Customer) Autowired(org.springframework.beans.factory.annotation.Autowired) Tenant(org.thingsboard.server.common.data.Tenant) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TenantId(org.thingsboard.server.common.data.id.TenantId) Value(org.springframework.beans.factory.annotation.Value) IdBased(org.thingsboard.server.common.data.id.IdBased) User(org.thingsboard.server.common.data.User) TenantService(org.thingsboard.server.dao.tenant.TenantService) CustomerService(org.thingsboard.server.dao.customer.CustomerService) InstallScripts(org.thingsboard.server.service.install.InstallScripts) UserPrincipal(org.thingsboard.server.service.security.model.UserPrincipal) DashboardId(org.thingsboard.server.common.data.id.DashboardId) PageLink(org.thingsboard.server.common.data.page.PageLink) UserCredentials(org.thingsboard.server.common.data.security.UserCredentials) ReentrantLock(java.util.concurrent.locks.ReentrantLock) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Authority(org.thingsboard.server.common.data.security.Authority) ComponentLifecycleEvent(org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Lock(java.util.concurrent.locks.Lock) OAuth2MapperConfig(org.thingsboard.server.common.data.oauth2.OAuth2MapperConfig) PageData(org.thingsboard.server.common.data.page.PageData) DashboardService(org.thingsboard.server.dao.dashboard.DashboardService) TbClusterService(org.thingsboard.server.cluster.TbClusterService) Optional(java.util.Optional) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) OAuth2Registration(org.thingsboard.server.common.data.oauth2.OAuth2Registration) TbTenantProfileCache(org.thingsboard.server.dao.tenant.TbTenantProfileCache) StringUtils(org.springframework.util.StringUtils) CustomerId(org.thingsboard.server.common.data.id.CustomerId) PageLink(org.thingsboard.server.common.data.page.PageLink) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo)

Example 4 with PageLink

use of org.thingsboard.server.common.data.page.PageLink in project thingsboard by thingsboard.

the class JpaAssetDaoTest method testFindAssetsByTenantId.

@Test
public void testFindAssetsByTenantId() {
    UUID tenantId1 = Uuids.timeBased();
    UUID tenantId2 = Uuids.timeBased();
    UUID customerId1 = Uuids.timeBased();
    UUID customerId2 = Uuids.timeBased();
    for (int i = 0; i < 60; i++) {
        UUID assetId = Uuids.timeBased();
        UUID tenantId = i % 2 == 0 ? tenantId1 : tenantId2;
        UUID customerId = i % 2 == 0 ? customerId1 : customerId2;
        saveAsset(assetId, tenantId, customerId, "ASSET_" + i, "TYPE_1");
    }
    assertEquals(60, assetDao.find(TenantId.fromUUID(tenantId1)).size());
    PageLink pageLink = new PageLink(20, 0, "ASSET_");
    PageData<Asset> assets1 = assetDao.findAssetsByTenantId(tenantId1, pageLink);
    assertEquals(20, assets1.getData().size());
    pageLink = pageLink.nextPageLink();
    PageData<Asset> assets2 = assetDao.findAssetsByTenantId(tenantId1, pageLink);
    assertEquals(10, assets2.getData().size());
    pageLink = pageLink.nextPageLink();
    PageData<Asset> assets3 = assetDao.findAssetsByTenantId(tenantId1, pageLink);
    assertEquals(0, assets3.getData().size());
}
Also used : PageLink(org.thingsboard.server.common.data.page.PageLink) Asset(org.thingsboard.server.common.data.asset.Asset) UUID(java.util.UUID) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test)

Example 5 with PageLink

use of org.thingsboard.server.common.data.page.PageLink in project thingsboard by thingsboard.

the class JpaAssetDaoTest method testFindAssetsByTenantIdAndCustomerId.

@Test
public void testFindAssetsByTenantIdAndCustomerId() {
    UUID tenantId1 = Uuids.timeBased();
    UUID tenantId2 = Uuids.timeBased();
    UUID customerId1 = Uuids.timeBased();
    UUID customerId2 = Uuids.timeBased();
    for (int i = 0; i < 60; i++) {
        UUID assetId = Uuids.timeBased();
        UUID tenantId = i % 2 == 0 ? tenantId1 : tenantId2;
        UUID customerId = i % 2 == 0 ? customerId1 : customerId2;
        saveAsset(assetId, tenantId, customerId, "ASSET_" + i, "TYPE_1");
    }
    PageLink pageLink = new PageLink(20, 0, "ASSET_");
    PageData<Asset> assets1 = assetDao.findAssetsByTenantIdAndCustomerId(tenantId1, customerId1, pageLink);
    assertEquals(20, assets1.getData().size());
    pageLink = pageLink.nextPageLink();
    PageData<Asset> assets2 = assetDao.findAssetsByTenantIdAndCustomerId(tenantId1, customerId1, pageLink);
    assertEquals(10, assets2.getData().size());
    pageLink = pageLink.nextPageLink();
    PageData<Asset> assets3 = assetDao.findAssetsByTenantIdAndCustomerId(tenantId1, customerId1, pageLink);
    assertEquals(0, assets3.getData().size());
}
Also used : PageLink(org.thingsboard.server.common.data.page.PageLink) Asset(org.thingsboard.server.common.data.asset.Asset) UUID(java.util.UUID) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test)

Aggregations

PageLink (org.thingsboard.server.common.data.page.PageLink)187 Test (org.junit.Test)117 ArrayList (java.util.ArrayList)106 TenantId (org.thingsboard.server.common.data.id.TenantId)67 TypeReference (com.fasterxml.jackson.core.type.TypeReference)54 CustomerId (org.thingsboard.server.common.data.id.CustomerId)47 Tenant (org.thingsboard.server.common.data.Tenant)45 Customer (org.thingsboard.server.common.data.Customer)43 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)34 ApiOperation (io.swagger.annotations.ApiOperation)33 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)33 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)33 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)33 Edge (org.thingsboard.server.common.data.edge.Edge)32 Device (org.thingsboard.server.common.data.Device)28 PageData (org.thingsboard.server.common.data.page.PageData)28 Matchers.containsString (org.hamcrest.Matchers.containsString)22 UUID (java.util.UUID)20 Asset (org.thingsboard.server.common.data.asset.Asset)20 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)20