Search in sources :

Example 16 with EntityDataQuery

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

the class BaseEntityServiceTest method testBuildSimplePredicateQueryOperations.

@Test
public void testBuildSimplePredicateQueryOperations() throws InterruptedException {
    List<Device> devices = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        Device device = new Device();
        device.setTenantId(tenantId);
        device.setName("Device" + i);
        device.setType("default");
        device.setLabel("testLabel" + (int) (Math.random() * 1000));
        devices.add(deviceService.saveDevice(device));
        // TO make sure devices have different created time
        Thread.sleep(1);
    }
    DeviceTypeFilter filter = new DeviceTypeFilter();
    filter.setDeviceType("default");
    filter.setDeviceNameFilter("");
    EntityDataSortOrder sortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"), EntityDataSortOrder.Direction.DESC);
    List<KeyFilter> deviceTypeFilters = createStringKeyFilters("type", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.EQUAL, "default");
    KeyFilter createdTimeFilter = createNumericKeyFilter("createdTime", EntityKeyType.ENTITY_FIELD, NumericFilterPredicate.NumericOperation.GREATER, 1L);
    List<KeyFilter> createdTimeFilters = Collections.singletonList(createdTimeFilter);
    List<KeyFilter> nameFilters = createStringKeyFilters("name", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.CONTAINS, "Device");
    List<EntityKey> entityFields = Arrays.asList(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"), new EntityKey(EntityKeyType.ENTITY_FIELD, "type"));
    // Device type filters
    EntityDataPageLink pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    EntityDataQuery query = new EntityDataQuery(filter, pageLink, entityFields, null, deviceTypeFilters);
    PageData<EntityData> data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    List<EntityData> loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    // Device create time filters
    pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, createdTimeFilters);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    // Device name filters
    pageLink = new EntityDataPageLink(100, 0, null, null);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, nameFilters);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    deviceService.deleteDevicesByTenantId(tenantId);
}
Also used : Device(org.thingsboard.server.common.data.Device) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) ArrayList(java.util.ArrayList) EntityData(org.thingsboard.server.common.data.query.EntityData) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityKey(org.thingsboard.server.common.data.query.EntityKey) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) Test(org.junit.Test)

Example 17 with EntityDataQuery

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

the class BaseEntityServiceTest method testBuildStringPredicateQueryOperationsForEntityType.

@Test
public void testBuildStringPredicateQueryOperationsForEntityType() throws ExecutionException, InterruptedException {
    List<Device> devices = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        Device device = new Device();
        device.setTenantId(tenantId);
        device.setName("Device" + i);
        device.setType("default");
        device.setLabel("testLabel" + (int) (Math.random() * 1000));
        devices.add(deviceService.saveDevice(device));
        // TO make sure devices have different created time
        Thread.sleep(1);
    }
    DeviceTypeFilter filter = new DeviceTypeFilter();
    filter.setDeviceType("default");
    filter.setDeviceNameFilter("");
    EntityDataSortOrder sortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime"), EntityDataSortOrder.Direction.DESC);
    List<EntityKey> entityFields = Arrays.asList(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"), new EntityKey(EntityKeyType.ENTITY_FIELD, "entityType"));
    List<KeyFilter> keyFiltersEqualString = createStringKeyFilters("entityType", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.EQUAL, "device");
    List<KeyFilter> keyFiltersNotEqualString = createStringKeyFilters("entityType", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.NOT_EQUAL, "asset");
    List<KeyFilter> keyFiltersStartsWithString = createStringKeyFilters("entityType", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.STARTS_WITH, "dev");
    List<KeyFilter> keyFiltersEndsWithString = createStringKeyFilters("entityType", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.ENDS_WITH, "ice");
    List<KeyFilter> keyFiltersContainsString = createStringKeyFilters("entityType", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.CONTAINS, "vic");
    List<KeyFilter> keyFiltersNotContainsString = createStringKeyFilters("entityType", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.NOT_CONTAINS, "dolphin");
    // Equal Operation
    EntityDataPageLink pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    EntityDataQuery query = new EntityDataQuery(filter, pageLink, entityFields, null, keyFiltersEqualString);
    PageData<EntityData> data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    List<EntityData> loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    List<String> loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
    List<String> devicesNames = devices.stream().map(Device::getName).collect(Collectors.toList());
    Assert.assertTrue(listEqualWithoutOrder(devicesNames, loadedStrings));
    // Not equal Operation
    pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, keyFiltersNotEqualString);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
    Assert.assertTrue(listEqualWithoutOrder(devicesNames, loadedStrings));
    // Starts with Operation
    pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, keyFiltersStartsWithString);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
    Assert.assertTrue(listEqualWithoutOrder(devicesNames, loadedStrings));
    // Ends with Operation
    pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, keyFiltersEndsWithString);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
    Assert.assertTrue(listEqualWithoutOrder(devicesNames, loadedStrings));
    // Contains Operation
    pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, keyFiltersContainsString);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
    Assert.assertTrue(listEqualWithoutOrder(devicesNames, loadedStrings));
    // Not contains Operation
    pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, keyFiltersNotContainsString);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
    Assert.assertTrue(listEqualWithoutOrder(devicesNames, loadedStrings));
    deviceService.deleteDevicesByTenantId(tenantId);
}
Also used : Arrays(java.util.Arrays) Edge(org.thingsboard.server.common.data.edge.Edge) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) Autowired(org.springframework.beans.factory.annotation.Autowired) Random(java.util.Random) StringUtils(org.apache.commons.lang3.StringUtils) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) TenantId(org.thingsboard.server.common.data.id.TenantId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) IdBased(org.thingsboard.server.common.data.id.IdBased) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) StringFilterPredicate(org.thingsboard.server.common.data.query.StringFilterPredicate) EntityListFilter(org.thingsboard.server.common.data.query.EntityListFilter) After(org.junit.After) RelationsQueryFilter(org.thingsboard.server.common.data.query.RelationsQueryFilter) Map(java.util.Map) EntityType(org.thingsboard.server.common.data.EntityType) AttributesService(org.thingsboard.server.dao.attributes.AttributesService) EdgeId(org.thingsboard.server.common.data.id.EdgeId) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) DeviceId(org.thingsboard.server.common.data.id.DeviceId) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) RelationRepository(org.thingsboard.server.dao.sql.relation.RelationRepository) TimeseriesService(org.thingsboard.server.dao.timeseries.TimeseriesService) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) AssetSearchQueryFilter(org.thingsboard.server.common.data.query.AssetSearchQueryFilter) Stream(java.util.stream.Stream) RelationEntityTypeFilter(org.thingsboard.server.common.data.relation.RelationEntityTypeFilter) StringOperation(org.thingsboard.server.common.data.query.StringFilterPredicate.StringOperation) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) CustomerId(org.thingsboard.server.common.data.id.CustomerId) RandomUtils(org.apache.commons.lang3.RandomUtils) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Device(org.thingsboard.server.common.data.Device) Tenant(org.thingsboard.server.common.data.Tenant) HashMap(java.util.HashMap) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) DeviceSearchQueryFilter(org.thingsboard.server.common.data.query.DeviceSearchQueryFilter) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) TsKvEntity(org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) EdgeTypeFilter(org.thingsboard.server.common.data.query.EdgeTypeFilter) EntityKey(org.thingsboard.server.common.data.query.EntityKey) DoubleDataEntry(org.thingsboard.server.common.data.kv.DoubleDataEntry) Before(org.junit.Before) DataConstants(org.thingsboard.server.common.data.DataConstants) EntityData(org.thingsboard.server.common.data.query.EntityData) FilterPredicateValue(org.thingsboard.server.common.data.query.FilterPredicateValue) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) EdgeSearchQueryFilter(org.thingsboard.server.common.data.query.EdgeSearchQueryFilter) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) ExecutionException(java.util.concurrent.ExecutionException) Futures(com.google.common.util.concurrent.Futures) PageData(org.thingsboard.server.common.data.page.PageData) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) Assert(org.junit.Assert) Comparator(java.util.Comparator) Collections(java.util.Collections) ResultSetExtractor(org.springframework.jdbc.core.ResultSetExtractor) Assert.assertEquals(org.junit.Assert.assertEquals) Asset(org.thingsboard.server.common.data.asset.Asset) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) Device(org.thingsboard.server.common.data.Device) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) ArrayList(java.util.ArrayList) EntityData(org.thingsboard.server.common.data.query.EntityData) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityKey(org.thingsboard.server.common.data.query.EntityKey) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) Test(org.junit.Test)

Example 18 with EntityDataQuery

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

the class BaseEntityServiceTest method testFindEntityDataByQuery_operationNotContains_emptySearchQuery.

@Test
public void testFindEntityDataByQuery_operationNotContains_emptySearchQuery() {
    List<Device> devices = createMockDevices(10);
    devices.get(0).setLabel("");
    devices.get(1).setLabel(null);
    devices.forEach(deviceService::saveDevice);
    String searchQuery = "";
    EntityDataQuery query = createDeviceSearchQuery("label", StringOperation.NOT_CONTAINS, searchQuery);
    PageData<EntityData> result = searchEntities(query);
    assertEquals(devices.size(), result.getTotalElements());
}
Also used : EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) Device(org.thingsboard.server.common.data.Device) EntityData(org.thingsboard.server.common.data.query.EntityData) Test(org.junit.Test)

Example 19 with EntityDataQuery

use of org.thingsboard.server.common.data.query.EntityDataQuery 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 20 with EntityDataQuery

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

the class DefaultEntityQueryService method findAlarmDataByQuery.

@Override
public PageData<AlarmData> findAlarmDataByQuery(SecurityUser securityUser, AlarmDataQuery query) {
    EntityDataQuery entityDataQuery = this.buildEntityDataQuery(query);
    PageData<EntityData> entities = entityService.findEntityDataByQuery(securityUser.getTenantId(), securityUser.getCustomerId(), entityDataQuery);
    if (entities.getTotalElements() > 0) {
        LinkedHashMap<EntityId, EntityData> entitiesMap = new LinkedHashMap<>();
        for (EntityData entityData : entities.getData()) {
            entitiesMap.put(entityData.getEntityId(), entityData);
        }
        PageData<AlarmData> alarms = alarmService.findAlarmDataByQueryForEntities(securityUser.getTenantId(), query, entitiesMap.keySet());
        for (AlarmData alarmData : alarms.getData()) {
            EntityId entityId = alarmData.getEntityId();
            if (entityId != null) {
                EntityData entityData = entitiesMap.get(entityId);
                if (entityData != null) {
                    alarmData.getLatest().putAll(entityData.getLatest());
                }
            }
        }
        return alarms;
    } else {
        return new PageData<>();
    }
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) PageData(org.thingsboard.server.common.data.page.PageData) EntityData(org.thingsboard.server.common.data.query.EntityData) LinkedHashMap(java.util.LinkedHashMap) AlarmData(org.thingsboard.server.common.data.query.AlarmData)

Aggregations

EntityDataQuery (org.thingsboard.server.common.data.query.EntityDataQuery)35 EntityData (org.thingsboard.server.common.data.query.EntityData)30 Device (org.thingsboard.server.common.data.Device)28 Test (org.junit.Test)27 EntityDataPageLink (org.thingsboard.server.common.data.query.EntityDataPageLink)24 EntityKey (org.thingsboard.server.common.data.query.EntityKey)22 DeviceTypeFilter (org.thingsboard.server.common.data.query.DeviceTypeFilter)21 EntityDataSortOrder (org.thingsboard.server.common.data.query.EntityDataSortOrder)18 LongDataEntry (org.thingsboard.server.common.data.kv.LongDataEntry)16 ArrayList (java.util.ArrayList)15 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 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)14 PageData (org.thingsboard.server.common.data.page.PageData)14 EntityCountQuery (org.thingsboard.server.common.data.query.EntityCountQuery)14 EntityKeyType (org.thingsboard.server.common.data.query.EntityKeyType)14 EntityListFilter (org.thingsboard.server.common.data.query.EntityListFilter)13