use of org.thingsboard.server.common.data.id.IdBased in project thingsboard by thingsboard.
the class BaseEntityServiceTest method testMultiRootHierarchicalFindEntityDataWithAttributesByQuery.
@Test
public void testMultiRootHierarchicalFindEntityDataWithAttributesByQuery() throws ExecutionException, 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);
EntityDataSortOrder sortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime"), EntityDataSortOrder.Direction.ASC);
EntityDataPageLink pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
List<EntityKey> entityFields = Lists.newArrayList(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"), new EntityKey(EntityKeyType.ENTITY_FIELD, "parentId"), new EntityKey(EntityKeyType.ENTITY_FIELD, "type"));
List<EntityKey> latestValues = Collections.singletonList(new EntityKey(EntityKeyType.ATTRIBUTE, "status"));
KeyFilter onlineStatusFilter = new KeyFilter();
onlineStatusFilter.setKey(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"));
StringFilterPredicate predicate = new StringFilterPredicate();
predicate.setOperation(StringOperation.ENDS_WITH);
predicate.setValue(FilterPredicateValue.fromString("_1"));
onlineStatusFilter.setPredicate(predicate);
List<KeyFilter> keyFilters = Collections.singletonList(onlineStatusFilter);
EntityDataQuery query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, keyFilters);
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());
}
long expectedEntitiesCnt = entityNameByTypeMap.entrySet().stream().filter(e -> !e.getKey().equals("building")).flatMap(e -> e.getValue().entrySet().stream()).map(Map.Entry::getValue).filter(e -> StringUtils.endsWith(e, "_1")).count();
Assert.assertEquals(expectedEntitiesCnt, loadedEntities.size());
Map<UUID, UUID> actualRelations = new HashMap<>();
loadedEntities.forEach(ed -> {
UUID parentId = UUID.fromString(ed.getLatest().get(EntityKeyType.ENTITY_FIELD).get("parentId").getValue());
UUID entityId = ed.getEntityId().getId();
Assert.assertEquals(childParentRelationMap.get(entityId), parentId);
actualRelations.put(entityId, parentId);
String entityType = ed.getLatest().get(EntityKeyType.ENTITY_FIELD).get("type").getValue();
String actualEntityName = ed.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue();
String expectedEntityName = entityNameByTypeMap.get(entityType).get(entityId);
Assert.assertEquals(expectedEntityName, actualEntityName);
});
deviceService.deleteDevicesByTenantId(tenantId);
assetService.deleteAssetsByTenantId(tenantId);
}
use of org.thingsboard.server.common.data.id.IdBased in project thingsboard by thingsboard.
the class BaseEventService method removeEvents.
@Override
public void removeEvents(TenantId tenantId, EntityId entityId, EventFilter eventFilter, Long startTime, Long endTime) {
TimePageLink eventsPageLink = new TimePageLink(1000, 0, null, null, startTime, endTime);
PageData<Event> eventsPageData;
do {
if (eventFilter == null) {
eventsPageData = findEvents(tenantId, entityId, eventsPageLink);
} else {
eventsPageData = findEventsByFilter(tenantId, entityId, eventFilter, eventsPageLink);
}
eventDao.removeAllByIds(eventsPageData.getData().stream().map(IdBased::getUuidId).collect(Collectors.toList()));
} while (eventsPageData.hasNext());
}
Aggregations