Search in sources :

Example 11 with EntityDataSortOrder

use of org.thingsboard.server.common.data.query.EntityDataSortOrder in project thingsboard by thingsboard.

the class BaseEntityServiceTest method createDeviceSearchQuery.

private EntityDataQuery createDeviceSearchQuery(String deviceField, StringOperation operation, String searchQuery) {
    DeviceTypeFilter deviceTypeFilter = new DeviceTypeFilter();
    deviceTypeFilter.setDeviceType("default");
    deviceTypeFilter.setDeviceNameFilter("");
    EntityDataSortOrder sortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime"), EntityDataSortOrder.Direction.ASC);
    EntityDataPageLink pageLink = new EntityDataPageLink(1000, 0, null, sortOrder);
    List<EntityKey> entityFields = Arrays.asList(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"), new EntityKey(EntityKeyType.ENTITY_FIELD, "label"));
    List<KeyFilter> keyFilters = createStringKeyFilters(deviceField, EntityKeyType.ENTITY_FIELD, operation, searchQuery);
    return new EntityDataQuery(deviceTypeFilter, pageLink, entityFields, null, keyFilters);
}
Also used : EntityKey(org.thingsboard.server.common.data.query.EntityKey) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink)

Example 12 with EntityDataSortOrder

use of org.thingsboard.server.common.data.query.EntityDataSortOrder in project thingsboard by thingsboard.

the class BaseAlarmServiceTest method testFindPropagatedToOwnerAndTenantAlarm.

@Test
public void testFindPropagatedToOwnerAndTenantAlarm() {
    Customer customer = new Customer();
    customer.setTitle("TestCustomer");
    customer.setTenantId(tenantId);
    customer = customerService.saveCustomer(customer);
    Device device = new Device();
    device.setName("TestTenantDevice");
    device.setType("default");
    device.setTenantId(tenantId);
    device.setCustomerId(customer.getId());
    device = deviceService.saveDevice(device);
    long ts = System.currentTimeMillis();
    Alarm tenantAlarm = Alarm.builder().tenantId(tenantId).originator(device.getId()).type("Propagated To Tenant").propagateToTenant(true).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    AlarmOperationResult result = alarmService.createOrUpdateAlarm(tenantAlarm);
    tenantAlarm = result.getAlarm();
    Alarm customerAlarm = Alarm.builder().tenantId(tenantId).originator(device.getId()).type("Propagated to Customer").propagate(false).propagateToOwner(true).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    result = alarmService.createOrUpdateAlarm(customerAlarm);
    customerAlarm = result.getAlarm();
    AlarmDataPageLink pageLink = new AlarmDataPageLink();
    pageLink.setPage(0);
    pageLink.setPageSize(10);
    pageLink.setSortOrder(new EntityDataSortOrder(new EntityKey(EntityKeyType.ALARM_FIELD, "createdTime")));
    pageLink.setStartTs(0L);
    pageLink.setEndTs(System.currentTimeMillis());
    pageLink.setSearchPropagatedAlarms(true);
    pageLink.setSeverityList(Arrays.asList(AlarmSeverity.CRITICAL, AlarmSeverity.WARNING));
    pageLink.setStatusList(Arrays.asList(AlarmSearchStatus.ACTIVE));
    // TEST that propagated alarms are visible on the asset level.
    PageData<AlarmData> tenantAlarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(tenantId));
    Assert.assertEquals(1, tenantAlarms.getData().size());
    Assert.assertEquals(tenantAlarm, tenantAlarms.getData().get(0));
    // TEST that propagated alarms are visible on the asset level.
    PageData<AlarmData> customerAlarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(customer.getId()));
    Assert.assertEquals(1, customerAlarms.getData().size());
    Assert.assertEquals(customerAlarm, customerAlarms.getData().get(0));
}
Also used : EntityKey(org.thingsboard.server.common.data.query.EntityKey) Customer(org.thingsboard.server.common.data.Customer) Device(org.thingsboard.server.common.data.Device) Alarm(org.thingsboard.server.common.data.alarm.Alarm) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) AlarmOperationResult(org.thingsboard.server.dao.alarm.AlarmOperationResult) AlarmDataPageLink(org.thingsboard.server.common.data.query.AlarmDataPageLink) AlarmData(org.thingsboard.server.common.data.query.AlarmData) Test(org.junit.Test)

Example 13 with EntityDataSortOrder

use of org.thingsboard.server.common.data.query.EntityDataSortOrder in project thingsboard by thingsboard.

the class BaseAlarmServiceTest method testFindPropagatedCustomerAssetAlarm.

@Test
public void testFindPropagatedCustomerAssetAlarm() throws ExecutionException, InterruptedException {
    Customer customer = new Customer();
    customer.setTitle("TestCustomer");
    customer.setTenantId(tenantId);
    customer = customerService.saveCustomer(customer);
    Device tenantDevice = new Device();
    tenantDevice.setName("TestTenantDevice");
    tenantDevice.setType("default");
    tenantDevice.setTenantId(tenantId);
    tenantDevice = deviceService.saveDevice(tenantDevice);
    Asset customerAsset = new Asset();
    customerAsset.setName("TestCustomerDevice");
    customerAsset.setType("default");
    customerAsset.setTenantId(tenantId);
    customerAsset.setCustomerId(customer.getId());
    customerAsset = assetService.saveAsset(customerAsset);
    EntityRelation relation = new EntityRelation();
    relation.setFrom(customerAsset.getId());
    relation.setTo(tenantDevice.getId());
    relation.setAdditionalInfo(JacksonUtil.newObjectNode());
    relation.setType("Contains");
    relation.setTypeGroup(RelationTypeGroup.COMMON);
    relationService.saveRelation(tenantId, relation);
    long ts = System.currentTimeMillis();
    Alarm tenantAlarm = Alarm.builder().tenantId(tenantId).originator(tenantDevice.getId()).type("Not Propagated").propagate(false).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    AlarmOperationResult result = alarmService.createOrUpdateAlarm(tenantAlarm);
    tenantAlarm = result.getAlarm();
    Alarm customerAlarm = Alarm.builder().tenantId(tenantId).originator(tenantDevice.getId()).type("Propagated").propagate(true).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    result = alarmService.createOrUpdateAlarm(customerAlarm);
    customerAlarm = result.getAlarm();
    AlarmDataPageLink pageLink = new AlarmDataPageLink();
    pageLink.setPage(0);
    pageLink.setPageSize(10);
    pageLink.setSortOrder(new EntityDataSortOrder(new EntityKey(EntityKeyType.ALARM_FIELD, "createdTime")));
    pageLink.setStartTs(0L);
    pageLink.setEndTs(System.currentTimeMillis());
    pageLink.setSearchPropagatedAlarms(true);
    pageLink.setSeverityList(Arrays.asList(AlarmSeverity.CRITICAL, AlarmSeverity.WARNING));
    pageLink.setStatusList(Arrays.asList(AlarmSearchStatus.ACTIVE));
    // TEST that propagated alarms are visible on the asset level.
    PageData<AlarmData> customerAlarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(customerAsset.getId()));
    Assert.assertEquals(1, customerAlarms.getData().size());
    Assert.assertEquals(customerAlarm, customerAlarms.getData().get(0));
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) EntityKey(org.thingsboard.server.common.data.query.EntityKey) Customer(org.thingsboard.server.common.data.Customer) Device(org.thingsboard.server.common.data.Device) Alarm(org.thingsboard.server.common.data.alarm.Alarm) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) Asset(org.thingsboard.server.common.data.asset.Asset) AlarmOperationResult(org.thingsboard.server.dao.alarm.AlarmOperationResult) AlarmDataPageLink(org.thingsboard.server.common.data.query.AlarmDataPageLink) AlarmData(org.thingsboard.server.common.data.query.AlarmData) Test(org.junit.Test)

Example 14 with EntityDataSortOrder

use of org.thingsboard.server.common.data.query.EntityDataSortOrder in project thingsboard by thingsboard.

the class TbAlarmDataSubCtx method buildEntityDataQuery.

@Override
protected EntityDataQuery buildEntityDataQuery() {
    EntityDataSortOrder sortOrder = query.getPageLink().getSortOrder();
    EntityDataSortOrder entitiesSortOrder;
    if (sortOrder == null || sortOrder.getKey().getType().equals(EntityKeyType.ALARM_FIELD)) {
        entitiesSortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, ModelConstants.CREATED_TIME_PROPERTY));
    } else {
        entitiesSortOrder = sortOrder;
    }
    EntityDataPageLink edpl = new EntityDataPageLink(maxEntitiesPerAlarmSubscription, 0, null, entitiesSortOrder);
    return new EntityDataQuery(query.getEntityFilter(), edpl, query.getEntityFields(), query.getLatestValues(), query.getKeyFilters());
}
Also used : EntityKey(org.thingsboard.server.common.data.query.EntityKey) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink)

Example 15 with EntityDataSortOrder

use of org.thingsboard.server.common.data.query.EntityDataSortOrder in project thingsboard by thingsboard.

the class BaseAlarmServiceTest method testFindAlarmUsingAlarmDataQuery.

@Test
public void testFindAlarmUsingAlarmDataQuery() throws ExecutionException, InterruptedException {
    AssetId parentId = new AssetId(Uuids.timeBased());
    AssetId parentId2 = new AssetId(Uuids.timeBased());
    AssetId childId = new AssetId(Uuids.timeBased());
    EntityRelation relation = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE);
    EntityRelation relation2 = new EntityRelation(parentId2, childId, EntityRelation.CONTAINS_TYPE);
    Assert.assertTrue(relationService.saveRelationAsync(tenantId, relation).get());
    Assert.assertTrue(relationService.saveRelationAsync(tenantId, relation2).get());
    long ts = System.currentTimeMillis();
    Alarm alarm = Alarm.builder().tenantId(tenantId).originator(childId).type(TEST_ALARM).propagate(false).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    AlarmOperationResult result = alarmService.createOrUpdateAlarm(alarm);
    Alarm created = result.getAlarm();
    AlarmDataPageLink pageLink = new AlarmDataPageLink();
    pageLink.setPage(0);
    pageLink.setPageSize(10);
    pageLink.setSortOrder(new EntityDataSortOrder(new EntityKey(EntityKeyType.ALARM_FIELD, "createdTime")));
    pageLink.setStartTs(0L);
    pageLink.setEndTs(System.currentTimeMillis());
    pageLink.setSearchPropagatedAlarms(false);
    pageLink.setSeverityList(Arrays.asList(AlarmSeverity.CRITICAL, AlarmSeverity.WARNING));
    pageLink.setStatusList(Arrays.asList(AlarmSearchStatus.ACTIVE));
    PageData<AlarmData> alarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(childId));
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, alarms.getData().get(0));
    pageLink.setPage(0);
    pageLink.setPageSize(10);
    pageLink.setSortOrder(new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime")));
    pageLink.setStartTs(0L);
    pageLink.setEndTs(System.currentTimeMillis());
    pageLink.setSearchPropagatedAlarms(false);
    pageLink.setSeverityList(Arrays.asList(AlarmSeverity.CRITICAL, AlarmSeverity.WARNING));
    pageLink.setStatusList(Arrays.asList(AlarmSearchStatus.ACTIVE));
    alarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(childId));
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, new Alarm(alarms.getData().get(0)));
    pageLink.setSearchPropagatedAlarms(true);
    alarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(childId));
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, new Alarm(alarms.getData().get(0)));
    // Check child relation
    created.setPropagate(true);
    result = alarmService.createOrUpdateAlarm(created);
    created = result.getAlarm();
    // Check child relation
    pageLink.setPage(0);
    pageLink.setPageSize(10);
    pageLink.setSortOrder(new EntityDataSortOrder(new EntityKey(EntityKeyType.ALARM_FIELD, "createdTime")));
    pageLink.setStartTs(0L);
    pageLink.setEndTs(System.currentTimeMillis());
    pageLink.setSearchPropagatedAlarms(true);
    pageLink.setSeverityList(Arrays.asList(AlarmSeverity.CRITICAL, AlarmSeverity.WARNING));
    pageLink.setStatusList(Arrays.asList(AlarmSearchStatus.ACTIVE));
    alarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(childId));
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, alarms.getData().get(0));
    // Check parent relation
    pageLink.setPage(0);
    pageLink.setPageSize(10);
    pageLink.setSortOrder(new EntityDataSortOrder(new EntityKey(EntityKeyType.ALARM_FIELD, "createdTime")));
    pageLink.setStartTs(0L);
    pageLink.setEndTs(System.currentTimeMillis());
    pageLink.setSearchPropagatedAlarms(true);
    pageLink.setSeverityList(Arrays.asList(AlarmSeverity.CRITICAL, AlarmSeverity.WARNING));
    pageLink.setStatusList(Arrays.asList(AlarmSearchStatus.ACTIVE));
    alarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(parentId));
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, alarms.getData().get(0));
    PageData<AlarmInfo> alarmsInfoData = alarmService.findAlarms(tenantId, AlarmQuery.builder().affectedEntityId(childId).status(AlarmStatus.ACTIVE_UNACK).pageLink(new TimePageLink(10, 0, "", new SortOrder("createdTime", SortOrder.Direction.DESC), 0L, System.currentTimeMillis())).build()).get();
    Assert.assertNotNull(alarmsInfoData.getData());
    Assert.assertEquals(1, alarmsInfoData.getData().size());
    Assert.assertEquals(created, alarmsInfoData.getData().get(0));
    alarmsInfoData = alarmService.findAlarms(tenantId, AlarmQuery.builder().affectedEntityId(parentId).status(AlarmStatus.ACTIVE_UNACK).pageLink(new TimePageLink(10, 0, "", new SortOrder("createdTime", SortOrder.Direction.DESC), 0L, System.currentTimeMillis())).build()).get();
    Assert.assertNotNull(alarmsInfoData.getData());
    Assert.assertEquals(1, alarmsInfoData.getData().size());
    Assert.assertEquals(created, alarmsInfoData.getData().get(0));
    alarmsInfoData = alarmService.findAlarms(tenantId, AlarmQuery.builder().affectedEntityId(parentId2).status(AlarmStatus.ACTIVE_UNACK).pageLink(new TimePageLink(10, 0, "", new SortOrder("createdTime", SortOrder.Direction.DESC), 0L, System.currentTimeMillis())).build()).get();
    Assert.assertNotNull(alarmsInfoData.getData());
    Assert.assertEquals(1, alarmsInfoData.getData().size());
    Assert.assertEquals(created, alarmsInfoData.getData().get(0));
    pageLink.setPage(0);
    pageLink.setPageSize(10);
    pageLink.setSortOrder(new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime")));
    pageLink.setStartTs(0L);
    pageLink.setEndTs(System.currentTimeMillis());
    pageLink.setSearchPropagatedAlarms(true);
    pageLink.setSeverityList(Arrays.asList(AlarmSeverity.CRITICAL, AlarmSeverity.WARNING));
    pageLink.setStatusList(Arrays.asList(AlarmSearchStatus.ACTIVE));
    alarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(parentId));
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, alarms.getData().get(0));
    alarmService.ackAlarm(tenantId, created.getId(), System.currentTimeMillis()).get();
    created = alarmService.findAlarmByIdAsync(tenantId, created.getId()).get();
    pageLink.setPage(0);
    pageLink.setPageSize(10);
    pageLink.setSortOrder(new EntityDataSortOrder(new EntityKey(EntityKeyType.ALARM_FIELD, "createdTime")));
    pageLink.setStartTs(0L);
    pageLink.setEndTs(System.currentTimeMillis());
    pageLink.setSearchPropagatedAlarms(true);
    pageLink.setSeverityList(Arrays.asList(AlarmSeverity.CRITICAL, AlarmSeverity.WARNING));
    pageLink.setStatusList(Arrays.asList(AlarmSearchStatus.ACTIVE));
    alarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(childId));
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, alarms.getData().get(0));
}
Also used : SortOrder(org.thingsboard.server.common.data.page.SortOrder) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) EntityKey(org.thingsboard.server.common.data.query.EntityKey) Alarm(org.thingsboard.server.common.data.alarm.Alarm) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) AlarmInfo(org.thingsboard.server.common.data.alarm.AlarmInfo) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) AssetId(org.thingsboard.server.common.data.id.AssetId) AlarmOperationResult(org.thingsboard.server.dao.alarm.AlarmOperationResult) AlarmDataPageLink(org.thingsboard.server.common.data.query.AlarmDataPageLink) AlarmData(org.thingsboard.server.common.data.query.AlarmData) Test(org.junit.Test)

Aggregations

EntityDataSortOrder (org.thingsboard.server.common.data.query.EntityDataSortOrder)23 EntityKey (org.thingsboard.server.common.data.query.EntityKey)21 EntityDataQuery (org.thingsboard.server.common.data.query.EntityDataQuery)18 Test (org.junit.Test)17 EntityDataPageLink (org.thingsboard.server.common.data.query.EntityDataPageLink)17 Device (org.thingsboard.server.common.data.Device)16 DeviceTypeFilter (org.thingsboard.server.common.data.query.DeviceTypeFilter)15 ArrayList (java.util.ArrayList)14 Collections (java.util.Collections)14 List (java.util.List)14 Collectors (java.util.stream.Collectors)14 EntityType (org.thingsboard.server.common.data.EntityType)14 EntityId (org.thingsboard.server.common.data.id.EntityId)14 EntityCountQuery (org.thingsboard.server.common.data.query.EntityCountQuery)14 EntityData (org.thingsboard.server.common.data.query.EntityData)14 EntityKeyType (org.thingsboard.server.common.data.query.EntityKeyType)14 HashMap (java.util.HashMap)13 Map (java.util.Map)13 PageData (org.thingsboard.server.common.data.page.PageData)13 EntityListFilter (org.thingsboard.server.common.data.query.EntityListFilter)13