Search in sources :

Example 1 with TimePageLink

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

the class BaseDashboardServiceTest method testFindDashboardsByTenantIdAndCustomerId.

@Test
public void testFindDashboardsByTenantIdAndCustomerId() throws ExecutionException, InterruptedException {
    Tenant tenant = new Tenant();
    tenant.setTitle("Test tenant");
    tenant = tenantService.saveTenant(tenant);
    TenantId tenantId = tenant.getId();
    Customer customer = new Customer();
    customer.setTitle("Test customer");
    customer.setTenantId(tenantId);
    customer = customerService.saveCustomer(customer);
    CustomerId customerId = customer.getId();
    List<DashboardInfo> dashboards = new ArrayList<>();
    for (int i = 0; i < 223; i++) {
        Dashboard dashboard = new Dashboard();
        dashboard.setTenantId(tenantId);
        dashboard.setTitle("Dashboard" + i);
        dashboard = dashboardService.saveDashboard(dashboard);
        dashboards.add(new DashboardInfo(dashboardService.assignDashboardToCustomer(dashboard.getId(), customerId)));
    }
    List<DashboardInfo> loadedDashboards = new ArrayList<>();
    TimePageLink pageLink = new TimePageLink(23);
    TimePageData<DashboardInfo> pageData = null;
    do {
        pageData = dashboardService.findDashboardsByTenantIdAndCustomerId(tenantId, customerId, pageLink).get();
        loadedDashboards.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(dashboards, idComparator);
    Collections.sort(loadedDashboards, idComparator);
    Assert.assertEquals(dashboards, loadedDashboards);
    dashboardService.unassignCustomerDashboards(customerId);
    pageLink = new TimePageLink(42);
    pageData = dashboardService.findDashboardsByTenantIdAndCustomerId(tenantId, customerId, pageLink).get();
    Assert.assertFalse(pageData.hasNext());
    Assert.assertTrue(pageData.getData().isEmpty());
    tenantService.deleteTenant(tenantId);
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Tenant(org.thingsboard.server.common.data.Tenant) Customer(org.thingsboard.server.common.data.Customer) ArrayList(java.util.ArrayList) Dashboard(org.thingsboard.server.common.data.Dashboard) CustomerId(org.thingsboard.server.common.data.id.CustomerId) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo) Test(org.junit.Test)

Example 2 with TimePageLink

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

the class JpaDashboardInfoDao method findDashboardsByTenantIdAndCustomerId.

@Override
public ListenableFuture<List<DashboardInfo>> findDashboardsByTenantIdAndCustomerId(UUID tenantId, UUID customerId, TimePageLink pageLink) {
    log.debug("Try to find dashboards by tenantId [{}], customerId[{}] and pageLink [{}]", tenantId, customerId, pageLink);
    ListenableFuture<List<EntityRelation>> relations = relationDao.findRelations(new CustomerId(customerId), EntityRelation.CONTAINS_TYPE, RelationTypeGroup.DASHBOARD, EntityType.DASHBOARD, pageLink);
    return Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<DashboardInfo>>) input -> {
        List<ListenableFuture<DashboardInfo>> dashboardFutures = new ArrayList<>(input.size());
        for (EntityRelation relation : input) {
            dashboardFutures.add(findByIdAsync(relation.getTo().getId()));
        }
        return Futures.successfulAsList(dashboardFutures);
    });
}
Also used : ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Time(java.sql.Time) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) Autowired(org.springframework.beans.factory.annotation.Autowired) CrudRepository(org.springframework.data.repository.CrudRepository) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) NULL_UUID_STR(org.thingsboard.server.dao.model.ModelConstants.NULL_UUID_STR) EntityType(org.thingsboard.server.common.data.EntityType) SqlDao(org.thingsboard.server.dao.util.SqlDao) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo) PageRequest(org.springframework.data.domain.PageRequest) UUID(java.util.UUID) RelationDao(org.thingsboard.server.dao.relation.RelationDao) Objects(java.util.Objects) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) JpaAbstractSearchTextDao(org.thingsboard.server.dao.sql.JpaAbstractSearchTextDao) List(java.util.List) DaoUtil(org.thingsboard.server.dao.DaoUtil) DashboardInfoDao(org.thingsboard.server.dao.dashboard.DashboardInfoDao) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) UUIDConverter(org.thingsboard.server.common.data.UUIDConverter) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) DashboardInfoEntity(org.thingsboard.server.dao.model.sql.DashboardInfoEntity) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) List(java.util.List) CustomerId(org.thingsboard.server.common.data.id.CustomerId) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo)

Example 3 with TimePageLink

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

the class BaseAlarmService method findHighestAlarmSeverity.

@Override
public AlarmSeverity findHighestAlarmSeverity(EntityId entityId, AlarmSearchStatus alarmSearchStatus, AlarmStatus alarmStatus) {
    TimePageLink nextPageLink = new TimePageLink(100);
    boolean hasNext = true;
    AlarmSeverity highestSeverity = null;
    AlarmQuery query;
    while (hasNext && AlarmSeverity.CRITICAL != highestSeverity) {
        query = new AlarmQuery(entityId, nextPageLink, alarmSearchStatus, alarmStatus, false);
        List<AlarmInfo> alarms;
        try {
            alarms = alarmDao.findAlarms(query).get();
        } catch (ExecutionException | InterruptedException e) {
            log.warn("Failed to find highest alarm severity. EntityId: [{}], AlarmSearchStatus: [{}], AlarmStatus: [{}]", entityId, alarmSearchStatus, alarmStatus);
            throw new RuntimeException(e);
        }
        hasNext = alarms.size() == nextPageLink.getLimit();
        if (hasNext) {
            nextPageLink = new TimePageData<>(alarms, nextPageLink).getNextPageLink();
        }
        AlarmSeverity severity = detectHighestSeverity(alarms);
        if (severity == null) {
            continue;
        }
        if (severity == AlarmSeverity.CRITICAL || highestSeverity == null) {
            highestSeverity = severity;
        } else {
            highestSeverity = highestSeverity.compareTo(severity) < 0 ? highestSeverity : severity;
        }
    }
    return highestSeverity;
}
Also used : TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) ExecutionException(java.util.concurrent.ExecutionException) TimePageData(org.thingsboard.server.common.data.page.TimePageData)

Example 4 with TimePageLink

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

the class CassandraDashboardInfoDao method findDashboardsByTenantIdAndCustomerId.

@Override
public ListenableFuture<List<DashboardInfo>> findDashboardsByTenantIdAndCustomerId(UUID tenantId, UUID customerId, TimePageLink pageLink) {
    log.debug("Try to find dashboards by tenantId [{}], customerId[{}] and pageLink [{}]", tenantId, customerId, pageLink);
    ListenableFuture<List<EntityRelation>> relations = relationDao.findRelations(new CustomerId(customerId), EntityRelation.CONTAINS_TYPE, RelationTypeGroup.DASHBOARD, EntityType.DASHBOARD, pageLink);
    return Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<DashboardInfo>>) input -> {
        List<ListenableFuture<DashboardInfo>> dashboardFutures = new ArrayList<>(input.size());
        for (EntityRelation relation : input) {
            dashboardFutures.add(findByIdAsync(relation.getTo().getId()));
        }
        return Futures.successfulAsList(dashboardFutures);
    });
}
Also used : DashboardInfoEntity(org.thingsboard.server.dao.model.nosql.DashboardInfoEntity) CassandraAbstractSearchTextDao(org.thingsboard.server.dao.nosql.CassandraAbstractSearchTextDao) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo) QueryBuilder.eq(com.datastax.driver.core.querybuilder.QueryBuilder.eq) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) NoSqlDao(org.thingsboard.server.dao.util.NoSqlDao) Autowired(org.springframework.beans.factory.annotation.Autowired) UUID(java.util.UUID) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) RelationDao(org.thingsboard.server.dao.relation.RelationDao) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) EntityType(org.thingsboard.server.common.data.EntityType) DaoUtil(org.thingsboard.server.dao.DaoUtil) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) Collections(java.util.Collections) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) CustomerId(org.thingsboard.server.common.data.id.CustomerId) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) List(java.util.List) CustomerId(org.thingsboard.server.common.data.id.CustomerId) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo)

Example 5 with TimePageLink

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

the class BaseDashboardControllerTest method testFindCustomerDashboards.

@Test
public void testFindCustomerDashboards() throws Exception {
    Customer customer = new Customer();
    customer.setTitle("Test customer");
    customer = doPost("/api/customer", customer, Customer.class);
    CustomerId customerId = customer.getId();
    List<DashboardInfo> dashboards = new ArrayList<>();
    for (int i = 0; i < 173; i++) {
        Dashboard dashboard = new Dashboard();
        dashboard.setTitle("Dashboard" + i);
        dashboard = doPost("/api/dashboard", dashboard, Dashboard.class);
        dashboards.add(new DashboardInfo(doPost("/api/customer/" + customerId.getId().toString() + "/dashboard/" + dashboard.getId().getId().toString(), Dashboard.class)));
    }
    List<DashboardInfo> loadedDashboards = new ArrayList<>();
    TimePageLink pageLink = new TimePageLink(21);
    TimePageData<DashboardInfo> pageData = null;
    do {
        pageData = doGetTypedWithTimePageLink("/api/customer/" + customerId.getId().toString() + "/dashboards?", new TypeReference<TimePageData<DashboardInfo>>() {
        }, pageLink);
        loadedDashboards.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(dashboards, idComparator);
    Collections.sort(loadedDashboards, idComparator);
    Assert.assertEquals(dashboards, loadedDashboards);
}
Also used : ArrayList(java.util.ArrayList) CustomerId(org.thingsboard.server.common.data.id.CustomerId) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Aggregations

TimePageLink (org.thingsboard.server.common.data.page.TimePageLink)20 Test (org.junit.Test)9 TenantId (org.thingsboard.server.common.data.id.TenantId)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)7 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)7 ArrayList (java.util.ArrayList)6 CustomerId (org.thingsboard.server.common.data.id.CustomerId)6 UUID (java.util.UUID)5 Event (org.thingsboard.server.common.data.Event)4 TypeReference (com.fasterxml.jackson.core.type.TypeReference)3 DashboardInfo (org.thingsboard.server.common.data.DashboardInfo)3 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)3 AsyncFunction (com.google.common.util.concurrent.AsyncFunction)2 Futures (com.google.common.util.concurrent.Futures)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 List (java.util.List)2 Slf4j (lombok.extern.slf4j.Slf4j)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Component (org.springframework.stereotype.Component)2 Device (org.thingsboard.server.common.data.Device)2