Search in sources :

Example 6 with Entities

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);
}
Also used : ByIdRequest(org.hypertrace.entity.data.service.v1.ByIdRequest) ServiceException(com.google.protobuf.ServiceException) LoggerFactory(org.slf4j.LoggerFactory) Channel(io.grpc.Channel) EntityTypeClient(org.hypertrace.entity.type.service.rxclient.EntityTypeClient) StreamObserver(io.grpc.stub.StreamObserver) Map(java.util.Map) Objects.isNull(java.util.Objects.isNull) ENRICHED_ENTITIES_COLLECTION(org.hypertrace.entity.service.constants.EntityCollectionConstants.ENRICHED_ENTITIES_COLLECTION) DocStoreConverter(org.hypertrace.entity.service.util.DocStoreConverter) InvalidRequestException(org.hypertrace.entity.service.exception.InvalidRequestException) StringUtils(org.hypertrace.entity.service.util.StringUtils) Streams(com.google.common.collect.Streams) Empty(org.hypertrace.entity.data.service.v1.Empty) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) EntityRelationship(org.hypertrace.entity.data.service.v1.EntityRelationship) EntityRelationships(org.hypertrace.entity.data.service.v1.EntityRelationships) MergeAndUpsertEntityRequest(org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityRequest) Optional(java.util.Optional) EntityChangeEventGenerator(org.hypertrace.entity.service.change.event.api.EntityChangeEventGenerator) EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Builder(org.hypertrace.entity.data.service.v1.Entity.Builder) Document(org.hypertrace.core.documentstore.Document) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) ByTypeAndIdentifyingAttributes(org.hypertrace.entity.data.service.v1.ByTypeAndIdentifyingAttributes) DocStoreJsonFormat(org.hypertrace.entity.service.util.DocStoreJsonFormat) Descriptors(com.google.protobuf.Descriptors) Filter(org.hypertrace.core.documentstore.Filter) HashMap(java.util.HashMap) Function(java.util.function.Function) Collection(org.hypertrace.core.documentstore.Collection) Entities(org.hypertrace.entity.data.service.v1.Entities) ArrayList(java.util.ArrayList) Datastore(org.hypertrace.core.documentstore.Datastore) Key(org.hypertrace.core.documentstore.Key) EntityServiceConstants(org.hypertrace.entity.service.constants.EntityServiceConstants) Entity(org.hypertrace.entity.data.service.v1.Entity) EntityDataServiceImplBase(org.hypertrace.entity.data.service.v1.EntityDataServiceGrpc.EntityDataServiceImplBase) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) IOException(java.io.IOException) ENTITY_RELATIONSHIPS_COLLECTION(org.hypertrace.entity.service.constants.EntityCollectionConstants.ENTITY_RELATIONSHIPS_COLLECTION) EnrichedEntities(org.hypertrace.entity.data.service.v1.EnrichedEntities) Query(org.hypertrace.entity.data.service.v1.Query) RAW_ENTITIES_COLLECTION(org.hypertrace.entity.service.constants.EntityCollectionConstants.RAW_ENTITIES_COLLECTION) Message(com.google.protobuf.Message) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) MergeAndUpsertEntityResponse(org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityResponse) Collections(java.util.Collections) RelationshipsQuery(org.hypertrace.entity.data.service.v1.RelationshipsQuery) ServiceException(com.google.protobuf.ServiceException) EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Key(org.hypertrace.core.documentstore.Key)

Example 7 with Entities

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());
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity) InOrder(org.mockito.InOrder) Test(org.junit.jupiter.api.Test)

Example 8 with Entities

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());
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity) InOrder(org.mockito.InOrder) Test(org.junit.jupiter.api.Test)

Example 9 with Entities

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();
}
Also used : ServiceException(com.google.protobuf.ServiceException) Query(org.hypertrace.entity.data.service.v1.Query) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest)

Example 10 with Entities

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);
    }
}
Also used : EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Entity(org.hypertrace.entity.data.service.v1.Entity) ServiceException(com.google.protobuf.ServiceException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Key(org.hypertrace.core.documentstore.Key)

Aggregations

Entity (org.hypertrace.entity.data.service.v1.Entity)16 Test (org.junit.jupiter.api.Test)12 EnrichedEntity (org.hypertrace.entity.data.service.v1.EnrichedEntity)9 HashMap (java.util.HashMap)7 ArrayList (java.util.ArrayList)6 Query (org.hypertrace.entity.data.service.v1.Query)6 ServiceException (com.google.protobuf.ServiceException)5 Map (java.util.Map)5 Channel (io.grpc.Channel)4 IOException (java.io.IOException)4 Collections (java.util.Collections)4 Iterator (java.util.Iterator)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 Stream (java.util.stream.Stream)4 Datastore (org.hypertrace.core.documentstore.Datastore)4 Document (org.hypertrace.core.documentstore.Document)4 JSONDocument (org.hypertrace.core.documentstore.JSONDocument)4 Key (org.hypertrace.core.documentstore.Key)4 RequestContext (org.hypertrace.core.grpcutils.context.RequestContext)4