Search in sources :

Example 1 with EntityCountQuery

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

the class BaseEntityServiceTest method testCountHierarchicalEntitiesByMultiRootQuery.

@Test
public void testCountHierarchicalEntitiesByMultiRootQuery() throws InterruptedException {
    List<Asset> buildings = new ArrayList<>();
    List<Asset> apartments = new ArrayList<>();
    Map<String, Map<UUID, String>> entityNameByTypeMap = new HashMap<>();
    Map<UUID, UUID> childParentRelationMap = new HashMap<>();
    createMultiRootHierarchy(buildings, apartments, entityNameByTypeMap, childParentRelationMap);
    RelationsQueryFilter filter = new RelationsQueryFilter();
    filter.setMultiRoot(true);
    filter.setMultiRootEntitiesType(EntityType.ASSET);
    filter.setMultiRootEntityIds(buildings.stream().map(IdBased::getId).map(d -> d.getId().toString()).collect(Collectors.toSet()));
    filter.setDirection(EntitySearchDirection.FROM);
    EntityCountQuery countQuery = new EntityCountQuery(filter);
    long count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(63, count);
    filter.setFilters(Collections.singletonList(new RelationEntityTypeFilter("AptToHeat", Collections.singletonList(EntityType.DEVICE))));
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(27, count);
    filter.setMultiRootEntitiesType(EntityType.ASSET);
    filter.setMultiRootEntityIds(apartments.stream().map(IdBased::getId).map(d -> d.getId().toString()).collect(Collectors.toSet()));
    filter.setDirection(EntitySearchDirection.TO);
    filter.setFilters(Lists.newArrayList(new RelationEntityTypeFilter("buildingToApt", Collections.singletonList(EntityType.ASSET)), new RelationEntityTypeFilter("AptToEnergy", Collections.singletonList(EntityType.DEVICE))));
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(9, count);
    deviceService.deleteDevicesByTenantId(tenantId);
    assetService.deleteAssetsByTenantId(tenantId);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CustomerId(org.thingsboard.server.common.data.id.CustomerId) RelationsQueryFilter(org.thingsboard.server.common.data.query.RelationsQueryFilter) Asset(org.thingsboard.server.common.data.asset.Asset) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) RelationEntityTypeFilter(org.thingsboard.server.common.data.relation.RelationEntityTypeFilter) UUID(java.util.UUID) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 2 with EntityCountQuery

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

the class BaseEntityServiceTest method testCountEntitiesByQuery.

@Test
public void testCountEntitiesByQuery() 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));
        devices.add(deviceService.saveDevice(device));
    }
    DeviceTypeFilter filter = new DeviceTypeFilter();
    filter.setDeviceType("default");
    filter.setDeviceNameFilter("");
    EntityCountQuery countQuery = new EntityCountQuery(filter);
    long count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(97, count);
    filter.setDeviceType("unknown");
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(0, count);
    filter.setDeviceType("default");
    filter.setDeviceNameFilter("Device1");
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(11, count);
    EntityListFilter entityListFilter = new EntityListFilter();
    entityListFilter.setEntityType(EntityType.DEVICE);
    entityListFilter.setEntityList(devices.stream().map(Device::getId).map(DeviceId::toString).collect(Collectors.toList()));
    countQuery = new EntityCountQuery(entityListFilter);
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(97, count);
    deviceService.deleteDevicesByTenantId(tenantId);
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(0, count);
}
Also used : Device(org.thingsboard.server.common.data.Device) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) DeviceId(org.thingsboard.server.common.data.id.DeviceId) ArrayList(java.util.ArrayList) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) EntityListFilter(org.thingsboard.server.common.data.query.EntityListFilter) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Test(org.junit.Test)

Example 3 with EntityCountQuery

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

the class BaseEntityQueryControllerTest method testCountEntitiesByQuery.

@Test
public void testCountEntitiesByQuery() throws Exception {
    List<Device> devices = new ArrayList<>();
    for (int i = 0; i < 97; i++) {
        Device device = new Device();
        device.setName("Device" + i);
        device.setType("default");
        device.setLabel("testLabel" + (int) (Math.random() * 1000));
        devices.add(doPost("/api/device", device, Device.class));
        Thread.sleep(1);
    }
    DeviceTypeFilter filter = new DeviceTypeFilter();
    filter.setDeviceType("default");
    filter.setDeviceNameFilter("");
    EntityCountQuery countQuery = new EntityCountQuery(filter);
    Long count = doPostWithResponse("/api/entitiesQuery/count", countQuery, Long.class);
    Assert.assertEquals(97, count.longValue());
    filter.setDeviceType("unknown");
    count = doPostWithResponse("/api/entitiesQuery/count", countQuery, Long.class);
    Assert.assertEquals(0, count.longValue());
    filter.setDeviceType("default");
    filter.setDeviceNameFilter("Device1");
    count = doPostWithResponse("/api/entitiesQuery/count", countQuery, Long.class);
    Assert.assertEquals(11, count.longValue());
    EntityListFilter entityListFilter = new EntityListFilter();
    entityListFilter.setEntityType(EntityType.DEVICE);
    entityListFilter.setEntityList(devices.stream().map(Device::getId).map(DeviceId::toString).collect(Collectors.toList()));
    countQuery = new EntityCountQuery(entityListFilter);
    count = doPostWithResponse("/api/entitiesQuery/count", countQuery, Long.class);
    Assert.assertEquals(97, count.longValue());
    EntityTypeFilter filter2 = new EntityTypeFilter();
    filter2.setEntityType(EntityType.DEVICE);
    EntityCountQuery countQuery2 = new EntityCountQuery(filter2);
    Long count2 = doPostWithResponse("/api/entitiesQuery/count", countQuery2, Long.class);
    Assert.assertEquals(97, count2.longValue());
}
Also used : EntityTypeFilter(org.thingsboard.server.common.data.query.EntityTypeFilter) Device(org.thingsboard.server.common.data.Device) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) DeviceId(org.thingsboard.server.common.data.id.DeviceId) ArrayList(java.util.ArrayList) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) EntityListFilter(org.thingsboard.server.common.data.query.EntityListFilter) Test(org.junit.Test)

Example 4 with EntityCountQuery

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

the class BaseWebsocketApiTest method testEntityCountWsCmd.

@Test
public void testEntityCountWsCmd() throws Exception {
    Device device = new Device();
    device.setName("Device");
    device.setType("default");
    device.setLabel("testLabel" + (int) (Math.random() * 1000));
    device = doPost("/api/device", device, Device.class);
    AttributeKvEntry dataPoint1 = new BaseAttributeKvEntry(System.currentTimeMillis(), new LongDataEntry("temperature", 42L));
    sendAttributes(device, TbAttributeSubscriptionScope.SERVER_SCOPE, Collections.singletonList(dataPoint1));
    DeviceTypeFilter dtf1 = new DeviceTypeFilter();
    dtf1.setDeviceNameFilter("D");
    dtf1.setDeviceType("default");
    EntityCountQuery edq1 = new EntityCountQuery(dtf1, Collections.emptyList());
    EntityCountCmd cmd1 = new EntityCountCmd(1, edq1);
    TelemetryPluginCmdsWrapper wrapper1 = new TelemetryPluginCmdsWrapper();
    wrapper1.setEntityCountCmds(Collections.singletonList(cmd1));
    wsClient.send(mapper.writeValueAsString(wrapper1));
    String msg1 = wsClient.waitForReply();
    EntityCountUpdate update1 = mapper.readValue(msg1, EntityCountUpdate.class);
    Assert.assertEquals(1, update1.getCmdId());
    Assert.assertEquals(1, update1.getCount());
    DeviceTypeFilter dtf2 = new DeviceTypeFilter();
    dtf2.setDeviceNameFilter("D");
    dtf2.setDeviceType("non-existing-device-type");
    EntityCountQuery edq2 = new EntityCountQuery(dtf2, Collections.emptyList());
    EntityCountCmd cmd2 = new EntityCountCmd(2, edq2);
    TelemetryPluginCmdsWrapper wrapper2 = new TelemetryPluginCmdsWrapper();
    wrapper2.setEntityCountCmds(Collections.singletonList(cmd2));
    wsClient.send(mapper.writeValueAsString(wrapper2));
    String msg2 = wsClient.waitForReply();
    EntityCountUpdate update2 = mapper.readValue(msg2, EntityCountUpdate.class);
    Assert.assertEquals(2, update2.getCmdId());
    Assert.assertEquals(0, update2.getCount());
    KeyFilter highTemperatureFilter = new KeyFilter();
    highTemperatureFilter.setKey(new EntityKey(EntityKeyType.ATTRIBUTE, "temperature"));
    NumericFilterPredicate predicate = new NumericFilterPredicate();
    predicate.setValue(FilterPredicateValue.fromDouble(40));
    predicate.setOperation(NumericFilterPredicate.NumericOperation.GREATER);
    highTemperatureFilter.setPredicate(predicate);
    highTemperatureFilter.setValueType(EntityKeyValueType.NUMERIC);
    DeviceTypeFilter dtf3 = new DeviceTypeFilter();
    dtf3.setDeviceNameFilter("D");
    dtf3.setDeviceType("default");
    EntityCountQuery edq3 = new EntityCountQuery(dtf3, Collections.singletonList(highTemperatureFilter));
    EntityCountCmd cmd3 = new EntityCountCmd(3, edq3);
    TelemetryPluginCmdsWrapper wrapper3 = new TelemetryPluginCmdsWrapper();
    wrapper3.setEntityCountCmds(Collections.singletonList(cmd3));
    wsClient.send(mapper.writeValueAsString(wrapper3));
    String msg3 = wsClient.waitForReply();
    EntityCountUpdate update3 = mapper.readValue(msg3, EntityCountUpdate.class);
    Assert.assertEquals(3, update3.getCmdId());
    Assert.assertEquals(1, update3.getCount());
    KeyFilter highTemperatureFilter2 = new KeyFilter();
    highTemperatureFilter2.setKey(new EntityKey(EntityKeyType.ATTRIBUTE, "temperature"));
    NumericFilterPredicate predicate2 = new NumericFilterPredicate();
    predicate2.setValue(FilterPredicateValue.fromDouble(50));
    predicate2.setOperation(NumericFilterPredicate.NumericOperation.GREATER);
    highTemperatureFilter2.setPredicate(predicate2);
    highTemperatureFilter2.setValueType(EntityKeyValueType.NUMERIC);
    DeviceTypeFilter dtf4 = new DeviceTypeFilter();
    dtf4.setDeviceNameFilter("D");
    dtf4.setDeviceType("default");
    EntityCountQuery edq4 = new EntityCountQuery(dtf4, Collections.singletonList(highTemperatureFilter2));
    EntityCountCmd cmd4 = new EntityCountCmd(4, edq4);
    TelemetryPluginCmdsWrapper wrapper4 = new TelemetryPluginCmdsWrapper();
    wrapper4.setEntityCountCmds(Collections.singletonList(cmd4));
    wsClient.send(mapper.writeValueAsString(wrapper4));
    String msg4 = wsClient.waitForReply();
    EntityCountUpdate update4 = mapper.readValue(msg4, EntityCountUpdate.class);
    Assert.assertEquals(4, update4.getCmdId());
    Assert.assertEquals(0, update4.getCount());
}
Also used : EntityCountCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityCountCmd) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) Device(org.thingsboard.server.common.data.Device) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) EntityKey(org.thingsboard.server.common.data.query.EntityKey) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) EntityCountUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityCountUpdate) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) TelemetryPluginCmdsWrapper(org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper) Test(org.junit.Test)

Example 5 with EntityCountQuery

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

the class DefaultEntityQueryRepository method countEntitiesByQuery.

@Override
public long countEntitiesByQuery(TenantId tenantId, CustomerId customerId, EntityCountQuery query) {
    EntityType entityType = resolveEntityType(query.getEntityFilter());
    QueryContext ctx = new QueryContext(new QuerySecurityContext(tenantId, customerId, entityType));
    if (query.getKeyFilters() == null || query.getKeyFilters().isEmpty()) {
        ctx.append("select count(e.id) from ");
        ctx.append(addEntityTableQuery(ctx, query.getEntityFilter()));
        ctx.append(" e where ");
        ctx.append(buildEntityWhere(ctx, query.getEntityFilter(), Collections.emptyList()));
        return transactionTemplate.execute(status -> {
            long startTs = System.currentTimeMillis();
            try {
                return jdbcTemplate.queryForObject(ctx.getQuery(), ctx, Long.class);
            } finally {
                queryLog.logQuery(ctx, ctx.getQuery(), System.currentTimeMillis() - startTs);
            }
        });
    } else {
        List<EntityKeyMapping> mappings = EntityKeyMapping.prepareEntityCountKeyMapping(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> 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 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 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, "");
        String countQuery = String.format("select count(id) %s", fromClauseCount);
        return transactionTemplate.execute(status -> {
            long startTs = System.currentTimeMillis();
            try {
                return jdbcTemplate.queryForObject(countQuery, ctx, Long.class);
            } finally {
                queryLog.logQuery(ctx, ctx.getQuery(), System.currentTimeMillis() - startTs);
            }
        });
    }
}
Also used : EntityType(org.thingsboard.server.common.data.EntityType) 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)

Aggregations

EntityCountQuery (org.thingsboard.server.common.data.query.EntityCountQuery)8 Test (org.junit.Test)7 CustomerId (org.thingsboard.server.common.data.id.CustomerId)6 ArrayList (java.util.ArrayList)5 Device (org.thingsboard.server.common.data.Device)4 DeviceTypeFilter (org.thingsboard.server.common.data.query.DeviceTypeFilter)4 EntityListFilter (org.thingsboard.server.common.data.query.EntityListFilter)4 RelationsQueryFilter (org.thingsboard.server.common.data.query.RelationsQueryFilter)3 RelationEntityTypeFilter (org.thingsboard.server.common.data.relation.RelationEntityTypeFilter)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 UUID (java.util.UUID)2 Asset (org.thingsboard.server.common.data.asset.Asset)2 Edge (org.thingsboard.server.common.data.edge.Edge)2 DeviceId (org.thingsboard.server.common.data.id.DeviceId)2 AssetSearchQueryFilter (org.thingsboard.server.common.data.query.AssetSearchQueryFilter)2 DeviceSearchQueryFilter (org.thingsboard.server.common.data.query.DeviceSearchQueryFilter)2 EdgeSearchQueryFilter (org.thingsboard.server.common.data.query.EdgeSearchQueryFilter)2 EdgeTypeFilter (org.thingsboard.server.common.data.query.EdgeTypeFilter)2 EntityTypeFilter (org.thingsboard.server.common.data.query.EntityTypeFilter)2