Search in sources :

Example 16 with EntityDataSortOrder

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

the class BaseAlarmServiceTest method testFindCustomerAlarm.

@Test
public void testFindCustomerAlarm() 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);
    Device customerDevice = new Device();
    customerDevice.setName("TestCustomerDevice");
    customerDevice.setType("default");
    customerDevice.setTenantId(tenantId);
    customerDevice.setCustomerId(customer.getId());
    customerDevice = deviceService.saveDevice(customerDevice);
    long ts = System.currentTimeMillis();
    Alarm tenantAlarm = Alarm.builder().tenantId(tenantId).originator(tenantDevice.getId()).type(TEST_ALARM).propagate(true).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    AlarmOperationResult result = alarmService.createOrUpdateAlarm(tenantAlarm);
    tenantAlarm = result.getAlarm();
    Alarm deviceAlarm = Alarm.builder().tenantId(tenantId).originator(customerDevice.getId()).type(TEST_ALARM).propagate(true).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    result = alarmService.createOrUpdateAlarm(deviceAlarm);
    deviceAlarm = 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));
    PageData<AlarmData> tenantAlarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Arrays.asList(tenantDevice.getId(), customerDevice.getId()));
    Assert.assertEquals(2, tenantAlarms.getData().size());
    PageData<AlarmData> customerAlarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(customerDevice.getId()));
    Assert.assertEquals(1, customerAlarms.getData().size());
    Assert.assertEquals(deviceAlarm, customerAlarms.getData().get(0));
    PageData<AlarmInfo> alarms = alarmService.findAlarms(tenantId, AlarmQuery.builder().affectedEntityId(tenantDevice.getId()).status(AlarmStatus.ACTIVE_UNACK).pageLink(new TimePageLink(10, 0, "", new SortOrder("createdTime", SortOrder.Direction.DESC), 0L, System.currentTimeMillis())).build()).get();
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(tenantAlarm, alarms.getData().get(0));
}
Also used : Customer(org.thingsboard.server.common.data.Customer) Device(org.thingsboard.server.common.data.Device) SortOrder(org.thingsboard.server.common.data.page.SortOrder) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) 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) 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 17 with EntityDataSortOrder

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

the class DefaultEntityQueryRepository method findEntityDataByQuery.

@Override
public PageData<EntityData> findEntityDataByQuery(TenantId tenantId, CustomerId customerId, EntityDataQuery query) {
    return transactionTemplate.execute(status -> {
        EntityType entityType = resolveEntityType(query.getEntityFilter());
        QueryContext ctx = new QueryContext(new QuerySecurityContext(tenantId, customerId, entityType));
        EntityDataPageLink pageLink = query.getPageLink();
        List<EntityKeyMapping> mappings = EntityKeyMapping.prepareKeyMapping(query);
        List<EntityKeyMapping> selectionMapping = mappings.stream().filter(EntityKeyMapping::isSelection).collect(Collectors.toList());
        List<EntityKeyMapping> entityFieldsSelectionMapping = selectionMapping.stream().filter(mapping -> !mapping.isLatest()).collect(Collectors.toList());
        List<EntityKeyMapping> latestSelectionMapping = selectionMapping.stream().filter(EntityKeyMapping::isLatest).collect(Collectors.toList());
        List<EntityKeyMapping> filterMapping = mappings.stream().filter(EntityKeyMapping::hasFilter).collect(Collectors.toList());
        List<EntityKeyMapping> entityFieldsFiltersMapping = filterMapping.stream().filter(mapping -> !mapping.isLatest()).collect(Collectors.toList());
        List<EntityKeyMapping> allLatestMappings = mappings.stream().filter(EntityKeyMapping::isLatest).collect(Collectors.toList());
        String entityWhereClause = DefaultEntityQueryRepository.this.buildEntityWhere(ctx, query.getEntityFilter(), entityFieldsFiltersMapping);
        String latestJoinsCnt = EntityKeyMapping.buildLatestJoins(ctx, query.getEntityFilter(), entityType, allLatestMappings, true);
        String latestJoinsData = EntityKeyMapping.buildLatestJoins(ctx, query.getEntityFilter(), entityType, allLatestMappings, false);
        String textSearchQuery = DefaultEntityQueryRepository.this.buildTextSearchQuery(ctx, selectionMapping, pageLink.getTextSearch());
        String entityFieldsSelection = EntityKeyMapping.buildSelections(entityFieldsSelectionMapping, query.getEntityFilter().getType(), entityType);
        String entityTypeStr;
        if (query.getEntityFilter().getType().equals(EntityFilterType.RELATIONS_QUERY)) {
            entityTypeStr = "e.entity_type";
        } else {
            entityTypeStr = "'" + entityType.name() + "'";
        }
        if (!StringUtils.isEmpty(entityFieldsSelection)) {
            entityFieldsSelection = String.format("e.id id, %s entity_type, %s", entityTypeStr, entityFieldsSelection);
        } else {
            entityFieldsSelection = String.format("e.id id, %s entity_type", entityTypeStr);
        }
        String latestSelection = EntityKeyMapping.buildSelections(latestSelectionMapping, query.getEntityFilter().getType(), entityType);
        String topSelection = "entities.*";
        if (!StringUtils.isEmpty(latestSelection)) {
            topSelection = topSelection + ", " + latestSelection;
        }
        String fromClauseCount = String.format("from (select %s from (select %s from %s e where %s) entities %s ) result %s", "entities.*", entityFieldsSelection, addEntityTableQuery(ctx, query.getEntityFilter()), entityWhereClause, latestJoinsCnt, textSearchQuery);
        String fromClauseData = String.format("from (select %s from (select %s from %s e where %s) entities %s ) result %s", topSelection, entityFieldsSelection, addEntityTableQuery(ctx, query.getEntityFilter()), entityWhereClause, latestJoinsData, textSearchQuery);
        if (!StringUtils.isEmpty(pageLink.getTextSearch())) {
            // Unfortunately, we need to sacrifice performance in case of full text search, because it is applied to all joined records.
            fromClauseCount = fromClauseData;
        }
        String countQuery = String.format("select count(id) %s", fromClauseCount);
        long startTs = System.currentTimeMillis();
        int totalElements;
        try {
            totalElements = jdbcTemplate.queryForObject(countQuery, ctx, Integer.class);
        } finally {
            queryLog.logQuery(ctx, countQuery, System.currentTimeMillis() - startTs);
        }
        if (totalElements == 0) {
            return new PageData<>();
        }
        String dataQuery = String.format("select * %s", fromClauseData);
        EntityDataSortOrder sortOrder = pageLink.getSortOrder();
        if (sortOrder != null) {
            Optional<EntityKeyMapping> sortOrderMappingOpt = mappings.stream().filter(EntityKeyMapping::isSortOrder).findFirst();
            if (sortOrderMappingOpt.isPresent()) {
                EntityKeyMapping sortOrderMapping = sortOrderMappingOpt.get();
                String direction = sortOrder.getDirection() == EntityDataSortOrder.Direction.ASC ? "asc" : "desc";
                if (sortOrderMapping.getEntityKey().getType() == EntityKeyType.ENTITY_FIELD) {
                    dataQuery = String.format("%s order by %s %s, result.id %s", dataQuery, sortOrderMapping.getValueAlias(), direction, direction);
                } else {
                    dataQuery = String.format("%s order by %s %s, %s %s, result.id %s", dataQuery, sortOrderMapping.getSortOrderNumAlias(), direction, sortOrderMapping.getSortOrderStrAlias(), direction, direction);
                }
            }
        }
        int startIndex = pageLink.getPageSize() * pageLink.getPage();
        if (pageLink.getPageSize() > 0) {
            dataQuery = String.format("%s limit %s offset %s", dataQuery, pageLink.getPageSize(), startIndex);
        }
        startTs = System.currentTimeMillis();
        List<Map<String, Object>> rows;
        try {
            rows = jdbcTemplate.queryForList(dataQuery, ctx);
        } finally {
            queryLog.logQuery(ctx, countQuery, System.currentTimeMillis() - startTs);
        }
        return EntityDataAdapter.createEntityData(pageLink, selectionMapping, rows, totalElements);
    });
}
Also used : Arrays(java.util.Arrays) Getter(lombok.Getter) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) EntityTypeFilter(org.thingsboard.server.common.data.query.EntityTypeFilter) HashMap(java.util.HashMap) AssetTypeFilter(org.thingsboard.server.common.data.query.AssetTypeFilter) DeviceSearchQueryFilter(org.thingsboard.server.common.data.query.DeviceSearchQueryFilter) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) StringUtils(org.apache.commons.lang3.StringUtils) TenantId(org.thingsboard.server.common.data.id.TenantId) Value(org.springframework.beans.factory.annotation.Value) EntityListFilter(org.thingsboard.server.common.data.query.EntityListFilter) RelationsQueryFilter(org.thingsboard.server.common.data.query.RelationsQueryFilter) Map(java.util.Map) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) Repository(org.springframework.stereotype.Repository) EdgeTypeFilter(org.thingsboard.server.common.data.query.EdgeTypeFilter) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) EntityData(org.thingsboard.server.common.data.query.EntityData) EntityViewTypeFilter(org.thingsboard.server.common.data.query.EntityViewTypeFilter) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) ApiUsageStateFilter(org.thingsboard.server.common.data.query.ApiUsageStateFilter) EdgeSearchQueryFilter(org.thingsboard.server.common.data.query.EdgeSearchQueryFilter) UUID(java.util.UUID) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) Collectors(java.util.stream.Collectors) EntitySearchQueryFilter(org.thingsboard.server.common.data.query.EntitySearchQueryFilter) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) EntityFilterType(org.thingsboard.server.common.data.query.EntityFilterType) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) AssetSearchQueryFilter(org.thingsboard.server.common.data.query.AssetSearchQueryFilter) EntityNameFilter(org.thingsboard.server.common.data.query.EntityNameFilter) SingleEntityFilter(org.thingsboard.server.common.data.query.SingleEntityFilter) RelationEntityTypeFilter(org.thingsboard.server.common.data.relation.RelationEntityTypeFilter) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) PageData(org.thingsboard.server.common.data.page.PageData) Optional(java.util.Optional) EntityViewSearchQueryFilter(org.thingsboard.server.common.data.query.EntityViewSearchQueryFilter) Collections(java.util.Collections) EntityFilter(org.thingsboard.server.common.data.query.EntityFilter) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) EntityType(org.thingsboard.server.common.data.EntityType) PageData(org.thingsboard.server.common.data.page.PageData) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with EntityDataSortOrder

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

the class DefaultAlarmQueryRepository method findAlarmDataByQueryForEntities.

@Override
public PageData<AlarmData> findAlarmDataByQueryForEntities(TenantId tenantId, AlarmDataQuery query, Collection<EntityId> orderedEntityIds) {
    return transactionTemplate.execute(status -> {
        AlarmDataPageLink pageLink = query.getPageLink();
        QueryContext ctx = new QueryContext(new QuerySecurityContext(tenantId, null, EntityType.ALARM));
        ctx.addUuidListParameter("entity_ids", orderedEntityIds.stream().map(EntityId::getId).collect(Collectors.toList()));
        StringBuilder selectPart = new StringBuilder(FIELDS_SELECTION);
        StringBuilder fromPart = new StringBuilder(" from alarm a ");
        StringBuilder wherePart = new StringBuilder(" where ");
        StringBuilder sortPart = new StringBuilder(" order by ");
        StringBuilder joinPart = new StringBuilder();
        boolean addAnd = false;
        if (pageLink.isSearchPropagatedAlarms()) {
            selectPart.append(" ea.entity_id as entity_id ");
            fromPart.append(JOIN_ENTITY_ALARMS);
            wherePart.append(buildPermissionsQuery(tenantId, ctx));
            addAnd = true;
        } else {
            selectPart.append(" a.originator_id as entity_id ");
        }
        EntityDataSortOrder sortOrder = pageLink.getSortOrder();
        if (sortOrder != null && sortOrder.getKey().getType().equals(EntityKeyType.ALARM_FIELD)) {
            String sortOrderKey = sortOrder.getKey().getKey();
            sortPart.append(alarmFieldColumnMap.getOrDefault(sortOrderKey, sortOrderKey)).append(" ").append(sortOrder.getDirection().name());
            if (pageLink.isSearchPropagatedAlarms()) {
                wherePart.append(" and ea.entity_id in (:entity_ids)");
            } else {
                addAndIfNeeded(wherePart, addAnd);
                addAnd = true;
                wherePart.append(" a.originator_id in (:entity_ids)");
            }
        } else {
            joinPart.append(" inner join (select * from (VALUES");
            int entityIdIdx = 0;
            int lastEntityIdIdx = orderedEntityIds.size() - 1;
            for (EntityId entityId : orderedEntityIds) {
                joinPart.append("(uuid('").append(entityId.getId().toString()).append("'), ").append(entityIdIdx).append(")");
                if (entityIdIdx != lastEntityIdIdx) {
                    joinPart.append(",");
                } else {
                    joinPart.append(")");
                }
                entityIdIdx++;
            }
            joinPart.append(" as e(id, priority)) e ");
            if (pageLink.isSearchPropagatedAlarms()) {
                joinPart.append("on ea.entity_id = e.id");
            } else {
                joinPart.append("on a.originator_id = e.id");
            }
            sortPart.append("e.priority");
        }
        long startTs;
        long endTs;
        if (pageLink.getTimeWindow() > 0) {
            endTs = System.currentTimeMillis();
            startTs = endTs - pageLink.getTimeWindow();
        } else {
            startTs = pageLink.getStartTs();
            endTs = pageLink.getEndTs();
        }
        if (startTs > 0) {
            addAndIfNeeded(wherePart, addAnd);
            addAnd = true;
            ctx.addLongParameter("startTime", startTs);
            wherePart.append("a.created_time >= :startTime");
            if (pageLink.isSearchPropagatedAlarms()) {
                wherePart.append(" and ea.created_time >= :startTime");
            }
        }
        if (endTs > 0) {
            addAndIfNeeded(wherePart, addAnd);
            addAnd = true;
            ctx.addLongParameter("endTime", endTs);
            wherePart.append("a.created_time <= :endTime");
            if (pageLink.isSearchPropagatedAlarms()) {
                wherePart.append(" and ea.created_time <= :endTime");
            }
        }
        if (pageLink.getTypeList() != null && !pageLink.getTypeList().isEmpty()) {
            addAndIfNeeded(wherePart, addAnd);
            addAnd = true;
            ctx.addStringListParameter("alarmTypes", pageLink.getTypeList());
            wherePart.append("a.type in (:alarmTypes)");
            if (pageLink.isSearchPropagatedAlarms()) {
                wherePart.append(" and ea.alarm_type in (:alarmTypes)");
            }
        }
        if (pageLink.getSeverityList() != null && !pageLink.getSeverityList().isEmpty()) {
            addAndIfNeeded(wherePart, addAnd);
            addAnd = true;
            ctx.addStringListParameter("alarmSeverities", pageLink.getSeverityList().stream().map(AlarmSeverity::name).collect(Collectors.toList()));
            wherePart.append("a.severity in (:alarmSeverities)");
        }
        if (pageLink.getStatusList() != null && !pageLink.getStatusList().isEmpty()) {
            Set<AlarmStatus> statusSet = toStatusSet(pageLink.getStatusList());
            if (!statusSet.isEmpty()) {
                addAndIfNeeded(wherePart, addAnd);
                addAnd = true;
                ctx.addStringListParameter("alarmStatuses", statusSet.stream().map(AlarmStatus::name).collect(Collectors.toList()));
                wherePart.append(" a.status in (:alarmStatuses)");
            }
        }
        String textSearchQuery = buildTextSearchQuery(ctx, query.getAlarmFields(), pageLink.getTextSearch());
        String mainQuery;
        if (!textSearchQuery.isEmpty()) {
            mainQuery = selectPart.toString() + fromPart.toString() + wherePart.toString();
            mainQuery = String.format("select * from (%s) a %s WHERE %s", mainQuery, joinPart, textSearchQuery);
        } else {
            mainQuery = selectPart.toString() + fromPart.toString() + joinPart.toString() + wherePart.toString();
        }
        String countQuery = String.format("select count(*) from (%s) result", mainQuery);
        long queryTs = System.currentTimeMillis();
        int totalElements;
        try {
            totalElements = jdbcTemplate.queryForObject(countQuery, ctx, Integer.class);
        } finally {
            queryLog.logQuery(ctx, countQuery, System.currentTimeMillis() - queryTs);
        }
        if (totalElements == 0) {
            return AlarmDataAdapter.createAlarmData(pageLink, Collections.emptyList(), totalElements, orderedEntityIds);
        }
        String dataQuery = mainQuery + sortPart;
        int startIndex = pageLink.getPageSize() * pageLink.getPage();
        if (pageLink.getPageSize() > 0) {
            dataQuery = String.format("%s limit %s offset %s", dataQuery, pageLink.getPageSize(), startIndex);
        }
        queryTs = System.currentTimeMillis();
        List<Map<String, Object>> rows;
        try {
            rows = jdbcTemplate.queryForList(dataQuery, ctx);
        } finally {
            queryLog.logQuery(ctx, dataQuery, System.currentTimeMillis() - queryTs);
        }
        return AlarmDataAdapter.createAlarmData(pageLink, rows, totalElements, orderedEntityIds);
    });
}
Also used : AlarmSeverity(org.thingsboard.server.common.data.alarm.AlarmSeverity) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) AlarmStatus(org.thingsboard.server.common.data.alarm.AlarmStatus) AlarmDataPageLink(org.thingsboard.server.common.data.query.AlarmDataPageLink) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with EntityDataSortOrder

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

the class BaseEntityServiceTest method testFindEntityDataByQueryWithTimeseries.

@Test
public void testFindEntityDataByQueryWithTimeseries() throws ExecutionException, InterruptedException {
    List<Device> devices = new ArrayList<>();
    List<Double> temperatures = new ArrayList<>();
    List<Double> highTemperatures = new ArrayList<>();
    for (int i = 0; i < 67; 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);
        double temperature = (double) (Math.random() * 100.0);
        temperatures.add(temperature);
        if (temperature > 45.0) {
            highTemperatures.add(temperature);
        }
    }
    List<ListenableFuture<Integer>> timeseriesFutures = new ArrayList<>();
    for (int i = 0; i < devices.size(); i++) {
        Device device = devices.get(i);
        timeseriesFutures.add(saveLongTimeseries(device.getId(), "temperature", temperatures.get(i)));
    }
    Futures.successfulAsList(timeseriesFutures).get();
    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.TIME_SERIES, "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(67, loadedEntities.size());
    List<String> loadedTemperatures = new ArrayList<>();
    for (Device device : devices) {
        loadedTemperatures.add(loadedEntities.stream().filter(entityData -> entityData.getEntityId().equals(device.getId())).findFirst().orElse(null).getLatest().get(EntityKeyType.TIME_SERIES).get("temperature").getValue());
    }
    List<String> deviceTemperatures = temperatures.stream().map(aDouble -> Double.toString(aDouble)).collect(Collectors.toList());
    Assert.assertEquals(deviceTemperatures, loadedTemperatures);
    pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
    KeyFilter highTemperatureFilter = new KeyFilter();
    highTemperatureFilter.setKey(new EntityKey(EntityKeyType.TIME_SERIES, "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.TIME_SERIES).get("temperature").getValue()).collect(Collectors.toList());
    List<String> deviceHighTemperatures = highTemperatures.stream().map(aDouble -> Double.toString(aDouble)).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) 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) 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) Test(org.junit.Test)

Example 20 with EntityDataSortOrder

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

the class BaseEntityServiceTest method testSimpleFindEntityDataByQuery.

@Test
public void testSimpleFindEntityDataByQuery() throws InterruptedException {
    List<Device> devices = new ArrayList<>();
    for (int i = 0; i < 97; i++) {
        Device device = new Device();
        device.setTenantId(tenantId);
        device.setName("Device" + i);
        device.setType("default");
        device.setLabel("testLabel" + (int) (Math.random() * 1000));
        // TO make sure devices have different created time
        Thread.sleep(1);
        devices.add(deviceService.saveDevice(device));
    }
    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"));
    EntityDataQuery query = new EntityDataQuery(filter, pageLink, entityFields, null, null);
    PageData<EntityData> data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    Assert.assertEquals(97, data.getTotalElements());
    Assert.assertEquals(10, data.getTotalPages());
    Assert.assertTrue(data.hasNext());
    Assert.assertEquals(10, data.getData().size());
    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(97, loadedEntities.size());
    List<EntityId> loadedIds = loadedEntities.stream().map(EntityData::getEntityId).collect(Collectors.toList());
    List<EntityId> deviceIds = devices.stream().map(Device::getId).collect(Collectors.toList());
    deviceIds.sort(Comparator.comparing(EntityId::getId));
    loadedIds.sort(Comparator.comparing(EntityId::getId));
    Assert.assertEquals(deviceIds, loadedIds);
    List<String> loadedNames = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
    List<String> deviceNames = devices.stream().map(Device::getName).collect(Collectors.toList());
    Collections.sort(loadedNames);
    Collections.sort(deviceNames);
    Assert.assertEquals(deviceNames, loadedNames);
    sortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"), EntityDataSortOrder.Direction.DESC);
    pageLink = new EntityDataPageLink(10, 0, "device1", sortOrder);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, null);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    Assert.assertEquals(11, data.getTotalElements());
    Assert.assertEquals("Device19", data.getData().get(0).getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue());
    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) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityKey(org.thingsboard.server.common.data.query.EntityKey) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) 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