Search in sources :

Example 6 with EntityRelation

use of org.thingsboard.server.common.data.relation.EntityRelation in project thingsboard by thingsboard.

the class BaseRelationServiceTest method testFindFrom.

@Test
public void testFindFrom() throws ExecutionException, InterruptedException {
    AssetId parentA = new AssetId(UUIDs.timeBased());
    AssetId parentB = new AssetId(UUIDs.timeBased());
    AssetId childA = new AssetId(UUIDs.timeBased());
    AssetId childB = new AssetId(UUIDs.timeBased());
    EntityRelation relationA1 = new EntityRelation(parentA, childA, EntityRelation.CONTAINS_TYPE);
    EntityRelation relationA2 = new EntityRelation(parentA, childB, EntityRelation.CONTAINS_TYPE);
    EntityRelation relationB1 = new EntityRelation(parentB, childA, EntityRelation.MANAGES_TYPE);
    EntityRelation relationB2 = new EntityRelation(parentB, childB, EntityRelation.MANAGES_TYPE);
    saveRelation(relationA1);
    saveRelation(relationA2);
    saveRelation(relationB1);
    saveRelation(relationB2);
    List<EntityRelation> relations = relationService.findByFrom(parentA, RelationTypeGroup.COMMON);
    Assert.assertEquals(2, relations.size());
    for (EntityRelation relation : relations) {
        Assert.assertEquals(EntityRelation.CONTAINS_TYPE, relation.getType());
        Assert.assertEquals(parentA, relation.getFrom());
        Assert.assertTrue(childA.equals(relation.getTo()) || childB.equals(relation.getTo()));
    }
    relations = relationService.findByFromAndType(parentA, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON);
    Assert.assertEquals(2, relations.size());
    relations = relationService.findByFromAndType(parentA, EntityRelation.MANAGES_TYPE, RelationTypeGroup.COMMON);
    Assert.assertEquals(0, relations.size());
    relations = relationService.findByFrom(parentB, RelationTypeGroup.COMMON);
    Assert.assertEquals(2, relations.size());
    for (EntityRelation relation : relations) {
        Assert.assertEquals(EntityRelation.MANAGES_TYPE, relation.getType());
        Assert.assertEquals(parentB, relation.getFrom());
        Assert.assertTrue(childA.equals(relation.getTo()) || childB.equals(relation.getTo()));
    }
    relations = relationService.findByFromAndType(parentB, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON);
    Assert.assertEquals(0, relations.size());
    relations = relationService.findByFromAndType(parentB, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON);
    Assert.assertEquals(0, relations.size());
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) AssetId(org.thingsboard.server.common.data.id.AssetId) Test(org.junit.Test)

Example 7 with EntityRelation

use of org.thingsboard.server.common.data.relation.EntityRelation in project thingsboard by thingsboard.

the class BaseAlarmServiceTest method testSaveAndFetchAlarm.

@Test
public void testSaveAndFetchAlarm() throws ExecutionException, InterruptedException {
    AssetId parentId = new AssetId(UUIDs.timeBased());
    AssetId childId = new AssetId(UUIDs.timeBased());
    EntityRelation relation = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE);
    Assert.assertTrue(relationService.saveRelationAsync(relation).get());
    long ts = System.currentTimeMillis();
    Alarm alarm = Alarm.builder().tenantId(tenantId).originator(childId).type(TEST_ALARM).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    Alarm created = alarmService.createOrUpdateAlarm(alarm);
    Assert.assertNotNull(created);
    Assert.assertNotNull(created.getId());
    Assert.assertNotNull(created.getOriginator());
    Assert.assertNotNull(created.getSeverity());
    Assert.assertNotNull(created.getStatus());
    Assert.assertEquals(tenantId, created.getTenantId());
    Assert.assertEquals(childId, created.getOriginator());
    Assert.assertEquals(TEST_ALARM, created.getType());
    Assert.assertEquals(AlarmSeverity.CRITICAL, created.getSeverity());
    Assert.assertEquals(AlarmStatus.ACTIVE_UNACK, created.getStatus());
    Assert.assertEquals(ts, created.getStartTs());
    Assert.assertEquals(ts, created.getEndTs());
    Assert.assertEquals(0L, created.getAckTs());
    Assert.assertEquals(0L, created.getClearTs());
    Alarm fetched = alarmService.findAlarmByIdAsync(created.getId()).get();
    Assert.assertEquals(created, fetched);
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) AssetId(org.thingsboard.server.common.data.id.AssetId) Test(org.junit.Test)

Example 8 with EntityRelation

use of org.thingsboard.server.common.data.relation.EntityRelation 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 9 with EntityRelation

use of org.thingsboard.server.common.data.relation.EntityRelation in project thingsboard by thingsboard.

the class BaseAlarmService method createAlarmRelation.

private void createAlarmRelation(EntityId entityId, EntityId alarmId, AlarmStatus status, boolean createAnyRelation) {
    try {
        if (createAnyRelation) {
            createRelation(new EntityRelation(entityId, alarmId, ALARM_RELATION_PREFIX + AlarmSearchStatus.ANY.name(), RelationTypeGroup.ALARM));
        }
        createRelation(new EntityRelation(entityId, alarmId, ALARM_RELATION_PREFIX + status.name(), RelationTypeGroup.ALARM));
        createRelation(new EntityRelation(entityId, alarmId, ALARM_RELATION_PREFIX + status.getClearSearchStatus().name(), RelationTypeGroup.ALARM));
        createRelation(new EntityRelation(entityId, alarmId, ALARM_RELATION_PREFIX + status.getAckSearchStatus().name(), RelationTypeGroup.ALARM));
    } catch (ExecutionException | InterruptedException e) {
        log.warn("[{}] Failed to create relation. Status: [{}]", alarmId, status);
        throw new RuntimeException(e);
    }
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ExecutionException(java.util.concurrent.ExecutionException)

Example 10 with EntityRelation

use of org.thingsboard.server.common.data.relation.EntityRelation in project thingsboard by thingsboard.

the class CassandraAlarmDao method findAlarms.

@Override
public ListenableFuture<List<AlarmInfo>> findAlarms(AlarmQuery query) {
    log.trace("Try to find alarms by entity [{}], searchStatus [{}], status [{}] and pageLink [{}]", query.getAffectedEntityId(), query.getSearchStatus(), query.getStatus(), query.getPageLink());
    EntityId affectedEntity = query.getAffectedEntityId();
    String searchStatusName;
    if (query.getSearchStatus() == null && query.getStatus() == null) {
        searchStatusName = AlarmSearchStatus.ANY.name();
    } else if (query.getSearchStatus() != null) {
        searchStatusName = query.getSearchStatus().name();
    } else {
        searchStatusName = query.getStatus().name();
    }
    String relationType = BaseAlarmService.ALARM_RELATION_PREFIX + searchStatusName;
    ListenableFuture<List<EntityRelation>> relations = relationDao.findRelations(affectedEntity, relationType, RelationTypeGroup.ALARM, EntityType.ALARM, query.getPageLink());
    return Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<AlarmInfo>>) input -> {
        List<ListenableFuture<AlarmInfo>> alarmFutures = new ArrayList<>(input.size());
        for (EntityRelation relation : input) {
            alarmFutures.add(Futures.transform(findAlarmByIdAsync(relation.getTo().getId()), (Function<Alarm, AlarmInfo>) AlarmInfo::new));
        }
        return Futures.successfulAsList(alarmFutures);
    });
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) QueryBuilder(com.datastax.driver.core.querybuilder.QueryBuilder) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) NoSqlDao(org.thingsboard.server.dao.util.NoSqlDao) Autowired(org.springframework.beans.factory.annotation.Autowired) TenantId(org.thingsboard.server.common.data.id.TenantId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) AlarmQuery(org.thingsboard.server.common.data.alarm.AlarmQuery) Function(com.google.common.base.Function) AlarmInfo(org.thingsboard.server.common.data.alarm.AlarmInfo) QueryBuilder.eq(com.datastax.driver.core.querybuilder.QueryBuilder.eq) AlarmSearchStatus(org.thingsboard.server.common.data.alarm.AlarmSearchStatus) UUID(java.util.UUID) Alarm(org.thingsboard.server.common.data.alarm.Alarm) 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) CassandraAbstractModelDao(org.thingsboard.server.dao.nosql.CassandraAbstractModelDao) QueryBuilder.select(com.datastax.driver.core.querybuilder.QueryBuilder.select) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) Select(com.datastax.driver.core.querybuilder.Select) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) AlarmEntity(org.thingsboard.server.dao.model.nosql.AlarmEntity) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Alarm(org.thingsboard.server.common.data.alarm.Alarm) AlarmInfo(org.thingsboard.server.common.data.alarm.AlarmInfo) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)28 AssetId (org.thingsboard.server.common.data.id.AssetId)13 Test (org.junit.Test)12 AsyncFunction (com.google.common.util.concurrent.AsyncFunction)6 Futures (com.google.common.util.concurrent.Futures)6 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)6 Slf4j (lombok.extern.slf4j.Slf4j)6 Autowired (org.springframework.beans.factory.annotation.Autowired)6 EntityId (org.thingsboard.server.common.data.id.EntityId)6 List (java.util.List)5 ExecutionException (java.util.concurrent.ExecutionException)5 EntityType (org.thingsboard.server.common.data.EntityType)5 Function (com.google.common.base.Function)4 ArrayList (java.util.ArrayList)4 UUID (java.util.UUID)4 Component (org.springframework.stereotype.Component)4 CustomerId (org.thingsboard.server.common.data.id.CustomerId)4 TenantId (org.thingsboard.server.common.data.id.TenantId)4 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)4 RelationTypeGroup (org.thingsboard.server.common.data.relation.RelationTypeGroup)4