Search in sources :

Example 1 with SetAttribute

use of org.hypertrace.entity.query.service.v1.SetAttribute in project entity-service by hypertrace.

the class EntityQueryServiceTest method testBulkUpdate.

@Test
public void testBulkUpdate() throws InterruptedException {
    Entity.Builder apiEntityBuilder1 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.API.name()).setEntityName("api1").putIdentifyingAttributes(EntityConstants.getValue(ServiceAttribute.SERVICE_ATTRIBUTE_ID), createAttribute(SERVICE_ID)).putIdentifyingAttributes(EntityConstants.getValue(ApiAttribute.API_ATTRIBUTE_NAME), createAttribute("api1")).putIdentifyingAttributes(EntityConstants.getValue(ApiAttribute.API_ATTRIBUTE_API_TYPE), createAttribute(API_TYPE));
    apiEntityBuilder1.putAttributes(apiAttributesMap.get(API_DISCOVERY_STATE_ATTR), createAttribute("DISCOVERED")).putAttributes(apiAttributesMap.get(API_HTTP_METHOD_ATTR), createAttribute("GET"));
    Entity entity1 = entityDataServiceClient.upsert(apiEntityBuilder1.build());
    Entity.Builder apiEntityBuilder2 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.API.name()).setEntityName("api2").putIdentifyingAttributes(EntityConstants.getValue(ServiceAttribute.SERVICE_ATTRIBUTE_ID), createAttribute(SERVICE_ID)).putIdentifyingAttributes(EntityConstants.getValue(ApiAttribute.API_ATTRIBUTE_NAME), createAttribute("api2")).putIdentifyingAttributes(EntityConstants.getValue(ApiAttribute.API_ATTRIBUTE_API_TYPE), createAttribute(API_TYPE));
    apiEntityBuilder2.putAttributes(apiAttributesMap.get(API_DISCOVERY_STATE_ATTR), createAttribute("UNDER_DISCOVERY")).putAttributes(apiAttributesMap.get(API_HTTP_METHOD_ATTR), createAttribute("GET"));
    Entity entity2 = entityDataServiceClient.upsert(apiEntityBuilder2.build());
    // create BulkUpdate request
    UpdateOperation update1 = UpdateOperation.newBuilder().setSetAttribute(SetAttribute.newBuilder().setAttribute(ColumnIdentifier.newBuilder().setColumnName(API_DISCOVERY_STATE_ATTR)).setValue(LiteralConstant.newBuilder().setValue(org.hypertrace.entity.query.service.v1.Value.newBuilder().setString("DISCOVERED")))).build();
    UpdateOperation update2 = UpdateOperation.newBuilder().setSetAttribute(SetAttribute.newBuilder().setAttribute(ColumnIdentifier.newBuilder().setColumnName(API_HTTP_METHOD_ATTR)).setValue(LiteralConstant.newBuilder().setValue(org.hypertrace.entity.query.service.v1.Value.newBuilder().setString("POST")))).build();
    EntityUpdateInfo updateInfo1 = EntityUpdateInfo.newBuilder().addUpdateOperation(update2).build();
    EntityUpdateInfo updateInfo2 = EntityUpdateInfo.newBuilder().addUpdateOperation(update1).addUpdateOperation(update2).build();
    BulkEntityUpdateRequest bulkUpdateRequest = BulkEntityUpdateRequest.newBuilder().setEntityType(EntityType.API.name()).putEntities(entity1.getEntityId(), updateInfo1).putEntities(entity2.getEntityId(), updateInfo2).build();
    Iterator<ResultSetChunk> iterator = GrpcClientRequestContextUtil.executeWithHeadersContext(HEADERS, () -> entityQueryServiceClient.bulkUpdate(bulkUpdateRequest));
    while (iterator.hasNext()) {
        continue;
    }
    // Add a small delay for the update to reflect
    Thread.sleep(500);
    EntityQueryRequest entityQueryRequest = EntityQueryRequest.newBuilder().setEntityType(EntityType.API.name()).addSelection(createExpression(API_DISCOVERY_STATE_ATTR)).addSelection(createExpression(API_HTTP_METHOD_ATTR)).build();
    Iterator<ResultSetChunk> resultSetChunkIterator = GrpcClientRequestContextUtil.executeWithHeadersContext(HEADERS, () -> entityQueryServiceClient.execute(entityQueryRequest));
    List<String> values = new ArrayList<>();
    while (resultSetChunkIterator.hasNext()) {
        ResultSetChunk chunk = resultSetChunkIterator.next();
        for (Row row : chunk.getRowList()) {
            for (int i = 0; i < row.getColumnCount(); i++) {
                String value = row.getColumnList().get(i).getString();
                values.add(value);
            }
        }
    }
    assertEquals(4, values.size());
    assertEquals("DISCOVERED", values.get(0));
    assertEquals("POST", values.get(1));
    assertEquals("DISCOVERED", values.get(2));
    assertEquals("POST", values.get(3));
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity) ArrayList(java.util.ArrayList) EntityUpdateInfo(org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest.EntityUpdateInfo) BulkEntityUpdateRequest(org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest) UpdateOperation(org.hypertrace.entity.query.service.v1.UpdateOperation) Row(org.hypertrace.entity.query.service.v1.Row) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) Test(org.junit.jupiter.api.Test)

Example 2 with SetAttribute

use of org.hypertrace.entity.query.service.v1.SetAttribute in project entity-service by hypertrace.

the class EntityQueryServiceTest method testBulkUpdateWithLabels.

@Test
public void testBulkUpdateWithLabels() {
    Entity.Builder apiEntityBuilder2 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.API.name()).setEntityName("api2").putIdentifyingAttributes(EntityConstants.getValue(ServiceAttribute.SERVICE_ATTRIBUTE_ID), createAttribute(SERVICE_ID)).putIdentifyingAttributes(EntityConstants.getValue(ApiAttribute.API_ATTRIBUTE_NAME), createAttribute("api2")).putIdentifyingAttributes(EntityConstants.getValue(ApiAttribute.API_ATTRIBUTE_API_TYPE), createAttribute(API_TYPE));
    apiEntityBuilder2.putAttributes(apiAttributesMap.get(API_LABELS_ATTR), createStringArrayAttribute(List.of("Label1")));
    Entity entity2 = entityDataServiceClient.upsert(apiEntityBuilder2.build());
    UpdateOperation update = UpdateOperation.newBuilder().setSetAttribute(SetAttribute.newBuilder().setAttribute(ColumnIdentifier.newBuilder().setColumnName(API_LABELS_ATTR)).setValue(LiteralConstant.newBuilder().setValue(org.hypertrace.entity.query.service.v1.Value.newBuilder().addAllStringArray(Collections.emptyList()).setValueType(STRING_ARRAY)))).build();
    EntityUpdateInfo updateInfo = EntityUpdateInfo.newBuilder().addUpdateOperation(update).build();
    BulkEntityUpdateRequest bulkUpdateRequest = BulkEntityUpdateRequest.newBuilder().setEntityType(EntityType.API.name()).putEntities(entity2.getEntityId(), updateInfo).build();
    assertDoesNotThrow(() -> GrpcClientRequestContextUtil.executeWithHeadersContext(HEADERS, () -> entityQueryServiceClient.bulkUpdate(bulkUpdateRequest)));
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity) BulkEntityUpdateRequest(org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest) UpdateOperation(org.hypertrace.entity.query.service.v1.UpdateOperation) EntityUpdateInfo(org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest.EntityUpdateInfo) Test(org.junit.jupiter.api.Test)

Example 3 with SetAttribute

use of org.hypertrace.entity.query.service.v1.SetAttribute in project entity-service by hypertrace.

the class EntityQueryServiceImplTest method testUpdate_success.

@Test
public void testUpdate_success() throws Exception {
    Collection mockEntitiesCollection = mockEntitiesCollection();
    Builder newStatus = LiteralConstant.newBuilder().setValue(Value.newBuilder().setValueType(ValueType.STRING).setString("NEW_STATUS"));
    EntityUpdateRequest updateRequest = EntityUpdateRequest.newBuilder().setEntityType(TEST_ENTITY_TYPE).addEntityIds("entity-id-1").setOperation(UpdateOperation.newBuilder().setSetAttribute(SetAttribute.newBuilder().setAttribute(ColumnIdentifier.newBuilder().setColumnName(ATTRIBUTE_ID2)).setValue(newStatus))).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(ATTRIBUTE_ID1))).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(ATTRIBUTE_ID2))).build();
    StreamObserver<ResultSetChunk> mockResponseObserver = mock(StreamObserver.class);
    Context.current().withValue(RequestContext.CURRENT, mockRequestContextWithTenantId()).call(() -> {
        EntityQueryServiceImpl eqs = new EntityQueryServiceImpl(mockEntitiesCollection, mockMappingForAttributes1And2(), 1, false);
        eqs.update(updateRequest, mockResponseObserver);
        return null;
    });
    verify(mockEntitiesCollection, times(1)).bulkUpdateSubDocs(eq(Map.of(new SingleValueKey("tenant1", "entity-id-1"), Map.of("attributes.status", new JSONDocument(DocStoreJsonFormat.printer().print(newStatus))))));
}
Also used : SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Builder(org.hypertrace.entity.query.service.v1.LiteralConstant.Builder) Collection(org.hypertrace.core.documentstore.Collection) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) BulkEntityUpdateRequest(org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest) EntityUpdateRequest(org.hypertrace.entity.query.service.v1.EntityUpdateRequest) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) Test(org.junit.jupiter.api.Test)

Example 4 with SetAttribute

use of org.hypertrace.entity.query.service.v1.SetAttribute in project entity-service by hypertrace.

the class EntityQueryServiceImplTest method testBulkUpdateEntityArrayAttribute.

@Test
void testBulkUpdateEntityArrayAttribute() throws Exception {
    List<String> entityIds = IntStream.rangeClosed(1, 5).mapToObj(i -> UUID.randomUUID().toString()).collect(Collectors.toList());
    BulkEntityArrayAttributeUpdateRequest request = BulkEntityArrayAttributeUpdateRequest.newBuilder().addAllEntityIds(entityIds).setAttribute(ColumnIdentifier.newBuilder().setColumnName(ATTRIBUTE_ID3).build()).setEntityType(TEST_ENTITY_TYPE).setOperation(BulkEntityArrayAttributeUpdateRequest.Operation.OPERATION_ADD).addAllValues(List.of(LiteralConstant.newBuilder().setValue(Value.newBuilder().setString("Label1")).build(), LiteralConstant.newBuilder().setValue(Value.newBuilder().setString("Label2")).build())).build();
    when(mockAttributeMapping.getDocStorePathByAttributeId(requestContext, ATTRIBUTE_ID3)).thenReturn(Optional.of(EDS_COLUMN_NAME3));
    StreamObserver<BulkEntityArrayAttributeUpdateResponse> mockResponseObserver = mock(StreamObserver.class);
    Context.current().withValue(RequestContext.CURRENT, mockRequestContextWithTenantId()).call(() -> {
        EntityQueryServiceImpl eqs = new EntityQueryServiceImpl(entitiesCollection, mockAttributeMapping, 1, false);
        eqs.bulkUpdateEntityArrayAttribute(request, mockResponseObserver);
        return null;
    });
    ArgumentCaptor<BulkArrayValueUpdateRequest> argumentCaptor = ArgumentCaptor.forClass(BulkArrayValueUpdateRequest.class);
    verify(entitiesCollection, times(1)).bulkOperationOnArrayValue(argumentCaptor.capture());
    BulkArrayValueUpdateRequest bulkArrayValueUpdateRequest = argumentCaptor.getValue();
    assertEquals(entityIds.stream().map(entityId -> new SingleValueKey("tenant1", entityId)).collect(Collectors.toCollection(LinkedHashSet::new)), bulkArrayValueUpdateRequest.getKeys());
    assertEquals(BulkArrayValueUpdateRequest.Operation.ADD, bulkArrayValueUpdateRequest.getOperation());
    assertEquals(EDS_COLUMN_NAME3 + ".valueList.values", bulkArrayValueUpdateRequest.getSubDocPath());
    List<Document> subDocuments = bulkArrayValueUpdateRequest.getSubDocuments();
    assertEquals(2, subDocuments.size());
    assertEquals("{\"value\":{\"string\":\"Label1\"}}", subDocuments.get(0).toString());
    assertEquals("{\"value\":{\"string\":\"Label2\"}}", subDocuments.get(1).toString());
}
Also used : Context(io.grpc.Context) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Builder(org.hypertrace.entity.query.service.v1.LiteralConstant.Builder) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) UpdateOperation(org.hypertrace.entity.query.service.v1.UpdateOperation) ResultSetMetadata(org.hypertrace.entity.query.service.v1.ResultSetMetadata) Query(org.hypertrace.core.documentstore.Query) Disabled(org.junit.jupiter.api.Disabled) ArgumentMatcher(org.mockito.ArgumentMatcher) StreamObserver(io.grpc.stub.StreamObserver) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) BulkEntityArrayAttributeUpdateResponse(org.hypertrace.entity.query.service.v1.BulkEntityArrayAttributeUpdateResponse) BulkEntityArrayAttributeUpdateRequest(org.hypertrace.entity.query.service.v1.BulkEntityArrayAttributeUpdateRequest) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Value(org.hypertrace.entity.query.service.v1.Value) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) BulkEntityUpdateRequest(org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ColumnMetadata(org.hypertrace.entity.query.service.v1.ColumnMetadata) Test(org.junit.jupiter.api.Test) List(java.util.List) JsonFormat(com.google.protobuf.util.JsonFormat) TotalEntitiesRequest(org.hypertrace.entity.query.service.v1.TotalEntitiesRequest) Optional(java.util.Optional) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) LiteralConstant(org.hypertrace.entity.query.service.v1.LiteralConstant) Mockito.mock(org.mockito.Mockito.mock) SetAttribute(org.hypertrace.entity.query.service.v1.SetAttribute) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) Row(org.hypertrace.entity.query.service.v1.Row) Document(org.hypertrace.core.documentstore.Document) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) DocStoreJsonFormat(org.hypertrace.entity.service.util.DocStoreJsonFormat) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) Filter(org.hypertrace.core.documentstore.Filter) Mock(org.mockito.Mock) EntityUpdateRequest(org.hypertrace.entity.query.service.v1.EntityUpdateRequest) LENIENT(org.mockito.quality.Strictness.LENIENT) Nested(org.junit.jupiter.api.Nested) Collection(org.hypertrace.core.documentstore.Collection) ArgumentCaptor(org.mockito.ArgumentCaptor) EntityUpdateInfo(org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest.EntityUpdateInfo) AttributeValue(org.hypertrace.entity.data.service.v1.AttributeValue) Entity(org.hypertrace.entity.data.service.v1.Entity) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) LinkedHashSet(java.util.LinkedHashSet) TotalEntitiesResponse(org.hypertrace.entity.query.service.v1.TotalEntitiesResponse) BulkArrayValueUpdateRequest(org.hypertrace.core.documentstore.BulkArrayValueUpdateRequest) ColumnIdentifier(org.hypertrace.entity.query.service.v1.ColumnIdentifier) BulkUpdateResult(org.hypertrace.core.documentstore.BulkUpdateResult) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) DisplayName(org.junit.jupiter.api.DisplayName) Mockito(org.mockito.Mockito) TestUtils.convertToCloseableIterator(org.hypertrace.entity.TestUtils.convertToCloseableIterator) Expression(org.hypertrace.entity.query.service.v1.Expression) ValueType(org.hypertrace.entity.query.service.v1.ValueType) OrderByExpression(org.hypertrace.entity.query.service.v1.OrderByExpression) LinkedHashSet(java.util.LinkedHashSet) BulkEntityArrayAttributeUpdateRequest(org.hypertrace.entity.query.service.v1.BulkEntityArrayAttributeUpdateRequest) BulkEntityArrayAttributeUpdateResponse(org.hypertrace.entity.query.service.v1.BulkEntityArrayAttributeUpdateResponse) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) BulkArrayValueUpdateRequest(org.hypertrace.core.documentstore.BulkArrayValueUpdateRequest) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Test(org.junit.jupiter.api.Test)

Example 5 with SetAttribute

use of org.hypertrace.entity.query.service.v1.SetAttribute in project entity-service by hypertrace.

the class EntityQueryServiceImpl method doUpdate.

private void doUpdate(RequestContext requestContext, EntityUpdateRequest request) throws Exception {
    if (request.getOperation().hasSetAttribute()) {
        SetAttribute setAttribute = request.getOperation().getSetAttribute();
        String attributeId = setAttribute.getAttribute().getColumnName();
        String subDocPath = entityAttributeMapping.getDocStorePathByAttributeId(requestContext, attributeId).orElseThrow(() -> new IllegalArgumentException("Unknown attribute FQN " + attributeId));
        JSONDocument jsonDocument = convertToJsonDocument(setAttribute.getValue());
        Map<Key, Map<String, Document>> entitiesUpdateMap = new HashMap<>();
        for (String entityId : request.getEntityIdsList()) {
            SingleValueKey key = new SingleValueKey(requestContext.getTenantId().orElseThrow(), entityId);
            if (entitiesUpdateMap.containsKey(key)) {
                entitiesUpdateMap.get(key).put(subDocPath, jsonDocument);
            } else {
                Map<String, Document> subDocument = new HashMap<>();
                subDocument.put(subDocPath, jsonDocument);
                entitiesUpdateMap.put(key, subDocument);
            }
        }
        try {
            entitiesCollection.bulkUpdateSubDocs(entitiesUpdateMap);
        } catch (Exception e) {
            LOG.error("Failed to update entities {}, subDocPath {}, with new doc {}.", entitiesUpdateMap, subDocPath, jsonDocument, e);
            throw e;
        }
    }
}
Also used : SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) HashMap(java.util.HashMap) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Map(java.util.Map) HashMap(java.util.HashMap) SetAttribute(org.hypertrace.entity.query.service.v1.SetAttribute) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Key(org.hypertrace.core.documentstore.Key) ServiceException(com.google.protobuf.ServiceException) ConversionException(org.hypertrace.entity.query.service.converter.ConversionException)

Aggregations

ResultSetChunk (org.hypertrace.entity.query.service.v1.ResultSetChunk)6 Test (org.junit.jupiter.api.Test)6 UpdateOperation (org.hypertrace.entity.query.service.v1.UpdateOperation)5 JSONDocument (org.hypertrace.core.documentstore.JSONDocument)4 Entity (org.hypertrace.entity.data.service.v1.Entity)4 BulkEntityUpdateRequest (org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest)4 EntityUpdateRequest (org.hypertrace.entity.query.service.v1.EntityUpdateRequest)4 Row (org.hypertrace.entity.query.service.v1.Row)4 SetAttribute (org.hypertrace.entity.query.service.v1.SetAttribute)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Document (org.hypertrace.core.documentstore.Document)3 SingleValueKey (org.hypertrace.core.documentstore.SingleValueKey)3 EntityUpdateInfo (org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest.EntityUpdateInfo)3 EntityQueryRequest (org.hypertrace.entity.query.service.v1.EntityQueryRequest)3 ServiceException (com.google.protobuf.ServiceException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Collection (org.hypertrace.core.documentstore.Collection)2