use of org.hypertrace.entity.data.service.v1.Entities in project entity-service by hypertrace.
the class EntityDataServiceImpl method upsertEnrichedEntities.
@Override
public void upsertEnrichedEntities(EnrichedEntities request, StreamObserver<Empty> responseObserver) {
String tenantId = RequestContext.CURRENT.get().getTenantId().orElse(null);
if (isNull(tenantId)) {
responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
return;
}
for (EnrichedEntity entity : request.getEntitiesList()) {
if (StringUtils.isEmpty(entity.getEntityType())) {
LOG.info("{}. Invalid upsertEnrichedEntities request:{}", entity, ErrorMessages.ENTITY_TYPE_EMPTY);
responseObserver.onError(new RuntimeException(ErrorMessages.ENTITY_TYPE_EMPTY));
return;
}
if (StringUtils.isEmpty(entity.getEntityId())) {
LOG.info("{}. Invalid upsertEnrichedEntities request:{}", entity, ErrorMessages.ENTITY_ID_EMPTY);
responseObserver.onError(new RuntimeException(ErrorMessages.ENTITY_ID_EMPTY));
return;
}
}
Map<Key, EnrichedEntity> entities = request.getEntitiesList().stream().collect(Collectors.toUnmodifiableMap(entity -> this.entityNormalizer.getEntityDocKey(tenantId, entity.getEntityType(), entity.getEntityId()), Function.identity()));
upsertEntities(entities, enrichedEntitiesCollection, responseObserver);
}
use of org.hypertrace.entity.data.service.v1.Entities in project entity-service by hypertrace.
the class EntityChangeEventGeneratorImplTest method sendCreateNotification.
@Test
void sendCreateNotification() {
List<Entity> entities = createEntities(2);
changeEventGenerator.sendCreateNotification(requestContext, entities);
InOrder inOrderVerifier = inOrder(eventProducer);
inOrderVerifier.verify(eventProducer).send(KeyUtil.getKey(entities.get(0)), EntityChangeEventValue.newBuilder().setCreateEvent(EntityCreateEvent.newBuilder().setCreatedEntity(entities.get(0)).build()).setEventTimeMillis(CURRENT_TIME_MILLIS).build());
inOrderVerifier.verify(eventProducer).send(KeyUtil.getKey(entities.get(1)), EntityChangeEventValue.newBuilder().setCreateEvent(EntityCreateEvent.newBuilder().setCreatedEntity(entities.get(1)).build()).setEventTimeMillis(CURRENT_TIME_MILLIS).build());
}
use of org.hypertrace.entity.data.service.v1.Entities in project entity-service by hypertrace.
the class EntityChangeEventGeneratorImplTest method sendDeleteNotification.
@Test
void sendDeleteNotification() {
List<Entity> entities = createEntities(2);
changeEventGenerator.sendDeleteNotification(requestContext, entities);
InOrder inOrderVerifier = inOrder(eventProducer);
inOrderVerifier.verify(eventProducer).send(KeyUtil.getKey(entities.get(0)), EntityChangeEventValue.newBuilder().setDeleteEvent(EntityDeleteEvent.newBuilder().setDeletedEntity(entities.get(0)).build()).setEventTimeMillis(CURRENT_TIME_MILLIS).build());
inOrderVerifier.verify(eventProducer).send(KeyUtil.getKey(entities.get(1)), EntityChangeEventValue.newBuilder().setDeleteEvent(EntityDeleteEvent.newBuilder().setDeletedEntity(entities.get(1)).build()).setEventTimeMillis(CURRENT_TIME_MILLIS).build());
}
use of org.hypertrace.entity.data.service.v1.Entities in project entity-service by hypertrace.
the class EntityQueryServiceImpl method total.
@Override
public void total(TotalEntitiesRequest request, StreamObserver<TotalEntitiesResponse> responseObserver) {
RequestContext requestContext = RequestContext.CURRENT.get();
Optional<String> tenantId = requestContext.getTenantId();
if (tenantId.isEmpty()) {
responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
return;
}
// converting total entities request to entity query request
EntityQueryRequest entityQueryRequest = EntityQueryRequest.newBuilder().setEntityType(request.getEntityType()).setFilter(request.getFilter()).build();
// converting entity query request to entity data service query
Query query = entityQueryConverter.convertToEDSQuery(requestContext, entityQueryRequest);
// TODO: Replace to use the new org.hypertrace.core.documentstore.query.Query DTO
long total = entitiesCollection.total(DocStoreConverter.transform(tenantId.get(), query, emptyList()));
responseObserver.onNext(TotalEntitiesResponse.newBuilder().setTotal(total).build());
responseObserver.onCompleted();
}
use of org.hypertrace.entity.data.service.v1.Entities in project entity-service by hypertrace.
the class EntityDataServiceImpl method getAndUpsertEntities.
@Override
public void getAndUpsertEntities(Entities request, StreamObserver<Entity> responseObserver) {
String tenantId = RequestContext.CURRENT.get().getTenantId().orElse(null);
if (tenantId == null) {
responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
return;
}
try {
Map<Key, Document> documentMap = new HashMap<>();
List<Entity> updatedEntities = new ArrayList<>();
for (Entity entity : request.getEntityList()) {
Entity normalizedEntity = this.entityNormalizer.normalize(tenantId, entity);
updatedEntities.add(normalizedEntity);
Document doc = convertEntityToDocument(normalizedEntity);
Key key = this.entityNormalizer.getEntityDocKey(tenantId, normalizedEntity);
documentMap.put(key, doc);
}
List<Entity> existingEntities = Streams.stream(entitiesCollection.bulkUpsertAndReturnOlderDocuments(documentMap)).flatMap(document -> PARSER.<Entity>parseOrLog(document, Entity.newBuilder()).stream()).map(Entity::toBuilder).map(builder -> builder.setTenantId(tenantId)).map(Entity.Builder::build).collect(Collectors.toList());
existingEntities.forEach(responseObserver::onNext);
responseObserver.onCompleted();
entityChangeEventGenerator.sendChangeNotification(RequestContext.CURRENT.get(), existingEntities, updatedEntities);
} catch (IOException e) {
LOG.error("Failed to bulk upsert entities", e);
responseObserver.onError(e);
}
}
Aggregations