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);
}
}
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);
}
}
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();
}
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());
}
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());
}
Aggregations