Search in sources :

Example 1 with KeyFilter

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

the class BaseEntityQueryControllerTest method testFindEntityDataByQueryWithAttributes.

@Test
public void testFindEntityDataByQueryWithAttributes() throws Exception {
    List<Device> devices = new ArrayList<>();
    List<Long> temperatures = new ArrayList<>();
    List<Long> highTemperatures = new ArrayList<>();
    for (int i = 0; i < 67; i++) {
        Device device = new Device();
        String name = "Device" + i;
        device.setName(name);
        device.setType("default");
        device.setLabel("testLabel" + (int) (Math.random() * 1000));
        devices.add(doPost("/api/device?accessToken=" + name, device, Device.class));
        Thread.sleep(1);
        long temperature = (long) (Math.random() * 100);
        temperatures.add(temperature);
        if (temperature > 45) {
            highTemperatures.add(temperature);
        }
    }
    for (int i = 0; i < devices.size(); i++) {
        Device device = devices.get(i);
        String payload = "{\"temperature\":" + temperatures.get(i) + "}";
        doPost("/api/plugins/telemetry/" + device.getId() + "/" + DataConstants.SHARED_SCOPE, payload, String.class, status().isOk());
    }
    Thread.sleep(1000);
    DeviceTypeFilter filter = new DeviceTypeFilter();
    filter.setDeviceType("default");
    filter.setDeviceNameFilter("");
    EntityDataSortOrder sortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime"), EntityDataSortOrder.Direction.ASC);
    EntityDataPageLink pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
    List<EntityKey> entityFields = Collections.singletonList(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"));
    List<EntityKey> latestValues = Collections.singletonList(new EntityKey(EntityKeyType.ATTRIBUTE, "temperature"));
    EntityDataQuery query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, null);
    PageData<EntityData> data = doPostWithTypedResponse("/api/entitiesQuery/find", query, new TypeReference<PageData<EntityData>>() {
    });
    List<EntityData> loadedEntities = new ArrayList<>(data.getData());
    while (data.hasNext()) {
        query = query.next();
        data = doPostWithTypedResponse("/api/entitiesQuery/find", query, new TypeReference<PageData<EntityData>>() {
        });
        loadedEntities.addAll(data.getData());
    }
    Assert.assertEquals(67, loadedEntities.size());
    List<String> loadedTemperatures = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ATTRIBUTE).get("temperature").getValue()).collect(Collectors.toList());
    List<String> deviceTemperatures = temperatures.stream().map(aLong -> Long.toString(aLong)).collect(Collectors.toList());
    Assert.assertEquals(deviceTemperatures, loadedTemperatures);
    pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
    KeyFilter highTemperatureFilter = new KeyFilter();
    highTemperatureFilter.setKey(new EntityKey(EntityKeyType.ATTRIBUTE, "temperature"));
    NumericFilterPredicate predicate = new NumericFilterPredicate();
    predicate.setValue(FilterPredicateValue.fromDouble(45));
    predicate.setOperation(NumericFilterPredicate.NumericOperation.GREATER);
    highTemperatureFilter.setPredicate(predicate);
    List<KeyFilter> keyFilters = Collections.singletonList(highTemperatureFilter);
    query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, keyFilters);
    data = doPostWithTypedResponse("/api/entitiesQuery/find", query, new TypeReference<PageData<EntityData>>() {
    });
    loadedEntities = new ArrayList<>(data.getData());
    while (data.hasNext()) {
        query = query.next();
        data = doPostWithTypedResponse("/api/entitiesQuery/find", query, new TypeReference<PageData<EntityData>>() {
        });
        loadedEntities.addAll(data.getData());
    }
    Assert.assertEquals(highTemperatures.size(), loadedEntities.size());
    List<String> loadedHighTemperatures = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ATTRIBUTE).get("temperature").getValue()).collect(Collectors.toList());
    List<String> deviceHighTemperatures = highTemperatures.stream().map(aLong -> Long.toString(aLong)).collect(Collectors.toList());
    Assert.assertEquals(deviceHighTemperatures, loadedHighTemperatures);
}
Also used : Device(org.thingsboard.server.common.data.Device) Tenant(org.thingsboard.server.common.data.Tenant) EntityTypeFilter(org.thingsboard.server.common.data.query.EntityTypeFilter) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) ArrayList(java.util.ArrayList) User(org.thingsboard.server.common.data.User) EntityListFilter(org.thingsboard.server.common.data.query.EntityListFilter) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) After(org.junit.After) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) TypeReference(com.fasterxml.jackson.core.type.TypeReference) EntityKey(org.thingsboard.server.common.data.query.EntityKey) Before(org.junit.Before) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) DeviceId(org.thingsboard.server.common.data.id.DeviceId) DataConstants(org.thingsboard.server.common.data.DataConstants) EntityData(org.thingsboard.server.common.data.query.EntityData) FilterPredicateValue(org.thingsboard.server.common.data.query.FilterPredicateValue) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) Test(org.junit.Test) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) Authority(org.thingsboard.server.common.data.security.Authority) Collectors(java.util.stream.Collectors) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) List(java.util.List) PageData(org.thingsboard.server.common.data.page.PageData) Assert(org.junit.Assert) Collections(java.util.Collections) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) ArrayList(java.util.ArrayList) EntityKey(org.thingsboard.server.common.data.query.EntityKey) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) TypeReference(com.fasterxml.jackson.core.type.TypeReference) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) Device(org.thingsboard.server.common.data.Device) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) EntityData(org.thingsboard.server.common.data.query.EntityData) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) PageData(org.thingsboard.server.common.data.page.PageData) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) Test(org.junit.Test)

Example 2 with KeyFilter

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

the class EntityKeyMapping method prepareKeyMapping.

public static List<EntityKeyMapping> prepareKeyMapping(EntityDataQuery query) {
    List<EntityKey> entityFields = query.getEntityFields() != null ? query.getEntityFields() : Collections.emptyList();
    List<EntityKey> latestValues = query.getLatestValues() != null ? query.getLatestValues() : Collections.emptyList();
    Map<EntityKey, List<KeyFilter>> filters = query.getKeyFilters() != null ? query.getKeyFilters().stream().collect(Collectors.groupingBy(KeyFilter::getKey)) : Collections.emptyMap();
    EntityDataSortOrder sortOrder = query.getPageLink().getSortOrder();
    EntityKey sortOrderKey = sortOrder != null ? sortOrder.getKey() : null;
    int index = 2;
    List<EntityKeyMapping> entityFieldsMappings = entityFields.stream().map(key -> {
        EntityKeyMapping mapping = new EntityKeyMapping();
        mapping.setLatest(false);
        mapping.setSelection(true);
        mapping.setSearchable(!key.getKey().equals(ADDITIONAL_INFO));
        mapping.setEntityKey(key);
        return mapping;
    }).collect(Collectors.toList());
    List<EntityKeyMapping> latestMappings = latestValues.stream().map(key -> {
        EntityKeyMapping mapping = new EntityKeyMapping();
        mapping.setLatest(true);
        mapping.setSearchable(true);
        mapping.setSelection(true);
        mapping.setEntityKey(key);
        return mapping;
    }).collect(Collectors.toList());
    if (sortOrderKey != null) {
        Optional<EntityKeyMapping> existing;
        if (sortOrderKey.getType().equals(EntityKeyType.ENTITY_FIELD)) {
            existing = entityFieldsMappings.stream().filter(mapping -> mapping.entityKey.equals(sortOrderKey)).findFirst();
        } else {
            existing = latestMappings.stream().filter(mapping -> mapping.entityKey.equals(sortOrderKey)).findFirst();
        }
        if (existing.isPresent()) {
            existing.get().setSortOrder(true);
        } else {
            EntityKeyMapping sortOrderMapping = new EntityKeyMapping();
            sortOrderMapping.setLatest(!sortOrderKey.getType().equals(EntityKeyType.ENTITY_FIELD));
            sortOrderMapping.setSelection(true);
            sortOrderMapping.setEntityKey(sortOrderKey);
            sortOrderMapping.setSortOrder(true);
            sortOrderMapping.setIgnore(true);
            if (sortOrderKey.getType().equals(EntityKeyType.ENTITY_FIELD)) {
                entityFieldsMappings.add(sortOrderMapping);
            } else {
                latestMappings.add(sortOrderMapping);
            }
        }
    }
    List<EntityKeyMapping> mappings = new ArrayList<>();
    mappings.addAll(entityFieldsMappings);
    mappings.addAll(latestMappings);
    for (EntityKeyMapping mapping : mappings) {
        mapping.setIndex(index);
        mapping.setAlias(String.format("alias%s", index));
        mapping.setKeyFilters(filters.remove(mapping.entityKey));
        if (mapping.getEntityKey().getType().equals(EntityKeyType.ENTITY_FIELD)) {
            index++;
        } else {
            index += 2;
        }
    }
    if (!filters.isEmpty()) {
        for (EntityKey filterField : filters.keySet()) {
            EntityKeyMapping mapping = new EntityKeyMapping();
            mapping.setIndex(index);
            mapping.setAlias(String.format("alias%s", index));
            mapping.setKeyFilters(filters.get(filterField));
            mapping.setLatest(!filterField.getType().equals(EntityKeyType.ENTITY_FIELD));
            mapping.setSelection(false);
            mapping.setEntityKey(filterField);
            mappings.add(mapping);
            index += 1;
        }
    }
    return mappings;
}
Also used : Arrays(java.util.Arrays) FilterPredicateType(org.thingsboard.server.common.data.query.FilterPredicateType) HashMap(java.util.HashMap) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) StringUtils(org.apache.commons.lang3.StringUtils) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) StringFilterPredicate(org.thingsboard.server.common.data.query.StringFilterPredicate) KeyFilterPredicate(org.thingsboard.server.common.data.query.KeyFilterPredicate) Map(java.util.Map) EntityType(org.thingsboard.server.common.data.EntityType) EntityKey(org.thingsboard.server.common.data.query.EntityKey) ComplexFilterPredicate(org.thingsboard.server.common.data.query.ComplexFilterPredicate) DataConstants(org.thingsboard.server.common.data.DataConstants) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) Set(java.util.Set) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) Collectors(java.util.stream.Collectors) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) EntityFilterType(org.thingsboard.server.common.data.query.EntityFilterType) List(java.util.List) Stream(java.util.stream.Stream) BooleanFilterPredicate(org.thingsboard.server.common.data.query.BooleanFilterPredicate) Data(lombok.Data) Optional(java.util.Optional) Collections(java.util.Collections) EntityFilter(org.thingsboard.server.common.data.query.EntityFilter) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) EntityKey(org.thingsboard.server.common.data.query.EntityKey) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with KeyFilter

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

the class BaseEntityServiceTest method doTestHierarchicalFindEntityDataWithAttributesByQuery.

private void doTestHierarchicalFindEntityDataWithAttributesByQuery(final int maxLevel, final boolean fetchLastLevelOnly) throws ExecutionException, InterruptedException {
    List<Asset> assets = new ArrayList<>();
    List<Device> devices = new ArrayList<>();
    List<Long> temperatures = new ArrayList<>();
    List<Long> highTemperatures = new ArrayList<>();
    createTestHierarchy(tenantId, assets, devices, new ArrayList<>(), new ArrayList<>(), temperatures, highTemperatures);
    List<ListenableFuture<List<Void>>> attributeFutures = new ArrayList<>();
    for (int i = 0; i < devices.size(); i++) {
        Device device = devices.get(i);
        attributeFutures.add(saveLongAttribute(device.getId(), "temperature", temperatures.get(i), DataConstants.CLIENT_SCOPE));
    }
    Futures.successfulAsList(attributeFutures).get();
    RelationsQueryFilter filter = new RelationsQueryFilter();
    filter.setRootEntity(tenantId);
    filter.setDirection(EntitySearchDirection.FROM);
    filter.setFilters(Collections.singletonList(new RelationEntityTypeFilter("Contains", Collections.singletonList(EntityType.DEVICE))));
    filter.setMaxLevel(maxLevel);
    filter.setFetchLastLevelOnly(fetchLastLevelOnly);
    EntityDataSortOrder sortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime"), EntityDataSortOrder.Direction.ASC);
    EntityDataPageLink pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
    List<EntityKey> entityFields = Collections.singletonList(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"));
    List<EntityKey> latestValues = Collections.singletonList(new EntityKey(EntityKeyType.ATTRIBUTE, "temperature"));
    EntityDataQuery query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, null);
    PageData<EntityData> data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    List<EntityData> loadedEntities = new ArrayList<>(data.getData());
    while (data.hasNext()) {
        query = query.next();
        data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
        loadedEntities.addAll(data.getData());
    }
    Assert.assertEquals(25, loadedEntities.size());
    List<String> loadedTemperatures = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ATTRIBUTE).get("temperature").getValue()).collect(Collectors.toList());
    List<String> deviceTemperatures = temperatures.stream().map(aLong -> Long.toString(aLong)).collect(Collectors.toList());
    Assert.assertEquals(deviceTemperatures, loadedTemperatures);
    pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
    KeyFilter highTemperatureFilter = new KeyFilter();
    highTemperatureFilter.setKey(new EntityKey(EntityKeyType.ATTRIBUTE, "temperature"));
    NumericFilterPredicate predicate = new NumericFilterPredicate();
    predicate.setValue(FilterPredicateValue.fromDouble(45));
    predicate.setOperation(NumericFilterPredicate.NumericOperation.GREATER);
    highTemperatureFilter.setPredicate(predicate);
    List<KeyFilter> keyFilters = Collections.singletonList(highTemperatureFilter);
    query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, keyFilters);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = new ArrayList<>(data.getData());
    while (data.hasNext()) {
        query = query.next();
        data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
        loadedEntities.addAll(data.getData());
    }
    Assert.assertEquals(highTemperatures.size(), loadedEntities.size());
    List<String> loadedHighTemperatures = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ATTRIBUTE).get("temperature").getValue()).collect(Collectors.toList());
    List<String> deviceHighTemperatures = highTemperatures.stream().map(aLong -> Long.toString(aLong)).collect(Collectors.toList());
    Assert.assertEquals(deviceHighTemperatures, loadedHighTemperatures);
    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) ArrayList(java.util.ArrayList) RelationsQueryFilter(org.thingsboard.server.common.data.query.RelationsQueryFilter) EntityKey(org.thingsboard.server.common.data.query.EntityKey) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) Asset(org.thingsboard.server.common.data.asset.Asset) RelationEntityTypeFilter(org.thingsboard.server.common.data.relation.RelationEntityTypeFilter) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) Device(org.thingsboard.server.common.data.Device) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) EntityData(org.thingsboard.server.common.data.query.EntityData) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) ListenableFuture(com.google.common.util.concurrent.ListenableFuture)

Example 4 with KeyFilter

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

the class BaseEntityServiceTest method createStringKeyFilters.

private List<KeyFilter> createStringKeyFilters(String key, EntityKeyType keyType, StringFilterPredicate.StringOperation operation, String value) {
    KeyFilter filter = new KeyFilter();
    filter.setKey(new EntityKey(keyType, key));
    StringFilterPredicate predicate = new StringFilterPredicate();
    predicate.setValue(FilterPredicateValue.fromString(value));
    predicate.setOperation(operation);
    predicate.setIgnoreCase(true);
    filter.setPredicate(predicate);
    return Collections.singletonList(filter);
}
Also used : EntityKey(org.thingsboard.server.common.data.query.EntityKey) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) StringFilterPredicate(org.thingsboard.server.common.data.query.StringFilterPredicate)

Example 5 with KeyFilter

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

the class BaseEntityServiceTest method testHierarchicalFindAssetsWithAttributesByQuery.

@Test
public void testHierarchicalFindAssetsWithAttributesByQuery() throws ExecutionException, InterruptedException {
    List<Asset> assets = new ArrayList<>();
    List<Device> devices = new ArrayList<>();
    List<Long> consumptions = new ArrayList<>();
    List<Long> highConsumptions = new ArrayList<>();
    createTestHierarchy(tenantId, assets, devices, consumptions, highConsumptions, new ArrayList<>(), new ArrayList<>());
    List<ListenableFuture<List<Void>>> attributeFutures = new ArrayList<>();
    for (int i = 0; i < assets.size(); i++) {
        Asset asset = assets.get(i);
        attributeFutures.add(saveLongAttribute(asset.getId(), "consumption", consumptions.get(i), DataConstants.SERVER_SCOPE));
    }
    Futures.successfulAsList(attributeFutures).get();
    AssetSearchQueryFilter filter = new AssetSearchQueryFilter();
    filter.setRootEntity(tenantId);
    filter.setDirection(EntitySearchDirection.FROM);
    filter.setRelationType("Manages");
    EntityDataSortOrder sortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime"), EntityDataSortOrder.Direction.ASC);
    EntityDataPageLink pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
    List<EntityKey> entityFields = Collections.singletonList(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"));
    List<EntityKey> latestValues = Collections.singletonList(new EntityKey(EntityKeyType.ATTRIBUTE, "consumption"));
    EntityDataQuery query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, null);
    PageData<EntityData> data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    List<EntityData> loadedEntities = new ArrayList<>(data.getData());
    while (data.hasNext()) {
        query = query.next();
        data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
        loadedEntities.addAll(data.getData());
    }
    Assert.assertEquals(5, loadedEntities.size());
    List<String> loadedTemperatures = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ATTRIBUTE).get("consumption").getValue()).collect(Collectors.toList());
    List<String> deviceTemperatures = consumptions.stream().map(aLong -> Long.toString(aLong)).collect(Collectors.toList());
    Assert.assertEquals(deviceTemperatures, loadedTemperatures);
    pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
    KeyFilter highTemperatureFilter = new KeyFilter();
    highTemperatureFilter.setKey(new EntityKey(EntityKeyType.ATTRIBUTE, "consumption"));
    NumericFilterPredicate predicate = new NumericFilterPredicate();
    predicate.setValue(FilterPredicateValue.fromDouble(50));
    predicate.setOperation(NumericFilterPredicate.NumericOperation.GREATER);
    highTemperatureFilter.setPredicate(predicate);
    List<KeyFilter> keyFilters = Collections.singletonList(highTemperatureFilter);
    query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, keyFilters);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = new ArrayList<>(data.getData());
    while (data.hasNext()) {
        query = query.next();
        data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
        loadedEntities.addAll(data.getData());
    }
    Assert.assertEquals(highConsumptions.size(), loadedEntities.size());
    List<String> loadedHighTemperatures = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ATTRIBUTE).get("consumption").getValue()).collect(Collectors.toList());
    List<String> deviceHighTemperatures = highConsumptions.stream().map(aLong -> Long.toString(aLong)).collect(Collectors.toList());
    Assert.assertEquals(deviceHighTemperatures, loadedHighTemperatures);
    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) ArrayList(java.util.ArrayList) EntityKey(org.thingsboard.server.common.data.query.EntityKey) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) Asset(org.thingsboard.server.common.data.asset.Asset) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) Device(org.thingsboard.server.common.data.Device) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) EntityData(org.thingsboard.server.common.data.query.EntityData) AssetSearchQueryFilter(org.thingsboard.server.common.data.query.AssetSearchQueryFilter) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Test(org.junit.Test)

Aggregations

KeyFilter (org.thingsboard.server.common.data.query.KeyFilter)18 EntityKey (org.thingsboard.server.common.data.query.EntityKey)17 ArrayList (java.util.ArrayList)13 DeviceTypeFilter (org.thingsboard.server.common.data.query.DeviceTypeFilter)13 NumericFilterPredicate (org.thingsboard.server.common.data.query.NumericFilterPredicate)13 List (java.util.List)12 Test (org.junit.Test)12 Device (org.thingsboard.server.common.data.Device)12 EntityCountQuery (org.thingsboard.server.common.data.query.EntityCountQuery)12 Collections (java.util.Collections)11 Collectors (java.util.stream.Collectors)11 DataConstants (org.thingsboard.server.common.data.DataConstants)11 EntityType (org.thingsboard.server.common.data.EntityType)11 Arrays (java.util.Arrays)10 HashMap (java.util.HashMap)10 Map (java.util.Map)10 Stream (java.util.stream.Stream)10 StringUtils (org.apache.commons.lang3.StringUtils)10 After (org.junit.After)10 Assert (org.junit.Assert)10