Search in sources :

Example 16 with Entities

use of org.hypertrace.entity.data.service.v1.Entities in project entity-service by hypertrace.

the class EntityQueryServiceTest method testExecuteWithCompositeAttributeFilters.

@Test
void testExecuteWithCompositeAttributeFilters() {
    // create and upsert some entities
    List<String> filterValue1 = List.of(generateRandomUUID(), generateRandomUUID());
    AttributeValueList.Builder attributeValueListBuilder = AttributeValueList.newBuilder();
    filterValue1.stream().map(this::generateAttrValue).forEach(attributeValueListBuilder::addValues);
    Entity entity1 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 1").putAttributes("labels", AttributeValue.newBuilder().setValueList(attributeValueListBuilder).build()).putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity1 = entityDataServiceClient.upsert(entity1);
    assertNotNull(createdEntity1);
    assertFalse(createdEntity1.getEntityId().trim().isEmpty());
    String filterValue2 = "DISCOVERED";
    Entity entity2 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 2").putAttributes(apiAttributesMap.get(API_DISCOVERY_STATE_ATTR), createAttribute(filterValue2)).putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity2 = entityDataServiceClient.upsert(entity2);
    assertNotNull(createdEntity2);
    assertFalse(createdEntity2.getEntityId().trim().isEmpty());
    Map<String, String> filterValue3 = Map.of("uuid", generateRandomUUID());
    Map<String, AttributeValue> attributeValueMap = filterValue3.entrySet().stream().collect(toUnmodifiableMap(Entry::getKey, e -> generateAttrValue(e.getValue())));
    AttributeValueMap attributeMap = AttributeValueMap.newBuilder().putAllValues(attributeValueMap).build();
    Entity entity3 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 3").putAttributes("http_url", AttributeValue.newBuilder().setValueMap(attributeMap).build()).putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity3 = entityDataServiceClient.upsert(entity3);
    assertNotNull(createdEntity3);
    assertFalse(createdEntity3.getEntityId().trim().isEmpty());
    Entity entity4 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 4").putAttributes("http_url", generateRandomUUIDAttrValue()).putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity4 = entityDataServiceClient.upsert(entity4);
    assertNotNull(createdEntity4);
    assertFalse(createdEntity4.getEntityId().trim().isEmpty());
    Entity entity5 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 5").putAttributes(apiAttributesMap.get(API_DISCOVERY_STATE_ATTR), generateRandomUUIDAttrValue()).putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity5 = entityDataServiceClient.upsert(entity5);
    assertNotNull(createdEntity5);
    assertFalse(createdEntity5.getEntityId().trim().isEmpty());
    EntityQueryRequest queryRequest = EntityQueryRequest.newBuilder().setEntityType(EntityType.SERVICE.name()).setFilter(Filter.newBuilder().setOperator(Operator.OR).addChildFilter(Filter.newBuilder().setOperator(Operator.IN).setLhs(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(API_LABELS_ATTR))).setRhs(Expression.newBuilder().setLiteral(LiteralConstant.newBuilder().setValue(org.hypertrace.entity.query.service.v1.Value.newBuilder().setValueType(STRING_ARRAY).addAllStringArray(filterValue1))))).addChildFilter(Filter.newBuilder().setOperator(Operator.EQ).setLhs(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(API_DISCOVERY_STATE_ATTR))).setRhs(Expression.newBuilder().setLiteral(LiteralConstant.newBuilder().setValue(org.hypertrace.entity.query.service.v1.Value.newBuilder().setValueType(STRING).setString(filterValue2))))).addChildFilter(Filter.newBuilder().setOperator(Operator.EQ).setLhs(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(API_HTTP_URL_ATTR))).setRhs(Expression.newBuilder().setLiteral(LiteralConstant.newBuilder().setValue(org.hypertrace.entity.query.service.v1.Value.newBuilder().setValueType(STRING_MAP).putAllStringMap(filterValue3))))).build()).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName("API.id").build()).build()).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName("API.name").build()).build()).build();
    Iterator<ResultSetChunk> resultSetChunkIterator = GrpcClientRequestContextUtil.executeWithHeadersContext(HEADERS, () -> entityQueryServiceClient.execute(queryRequest));
    List<ResultSetChunk> list = Lists.newArrayList(resultSetChunkIterator);
    assertEquals(2, list.size());
    assertEquals(2, list.get(0).getRowCount());
    assertEquals(0, list.get(0).getChunkId());
    assertFalse(list.get(0).getIsLastChunk());
    assertEquals(1, list.get(1).getRowCount());
    assertEquals(1, list.get(1).getChunkId());
    assertTrue(list.get(1).getIsLastChunk());
    assertEquals(createdEntity1.getEntityId(), list.get(0).getRow(0).getColumn(0).getString());
    assertEquals(createdEntity1.getEntityName(), list.get(0).getRow(0).getColumn(1).getString());
    assertEquals(createdEntity2.getEntityId(), list.get(0).getRow(1).getColumn(0).getString());
    assertEquals(createdEntity2.getEntityName(), list.get(0).getRow(1).getColumn(1).getString());
    assertEquals(createdEntity3.getEntityId(), list.get(1).getRow(0).getColumn(0).getString());
    assertEquals(createdEntity3.getEntityName(), list.get(1).getRow(0).getColumn(1).getString());
    assertTrue(list.get(0).getResultSetMetadata().getColumnMetadataCount() > 0);
    assertTrue(list.get(1).getResultSetMetadata().getColumnMetadataCount() > 0);
}
Also used : ASC(org.hypertrace.entity.query.service.v1.SortOrder.ASC) DatastoreProvider(org.hypertrace.core.documentstore.DatastoreProvider) Function(org.hypertrace.entity.query.service.v1.Function) BeforeEach(org.junit.jupiter.api.BeforeEach) ApiAttribute(org.hypertrace.entity.constants.v1.ApiAttribute) AttributeKind(org.hypertrace.entity.type.service.v1.AttributeKind) SERVICE_ATTRIBUTE_SERVICE_TYPE(org.hypertrace.entity.constants.v1.ServiceAttribute.SERVICE_ATTRIBUTE_SERVICE_TYPE) ManagedChannel(io.grpc.ManagedChannel) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) EntityConstants(org.hypertrace.entity.service.constants.EntityConstants) UpdateOperation(org.hypertrace.entity.query.service.v1.UpdateOperation) ProtocolStringList(com.google.protobuf.ProtocolStringList) CommonAttribute(org.hypertrace.entity.constants.v1.CommonAttribute) ResultSetMetadata(org.hypertrace.entity.query.service.v1.ResultSetMetadata) Channel(io.grpc.Channel) Disabled(org.junit.jupiter.api.Disabled) HOSTNAME(org.hypertrace.entity.v1.servicetype.ServiceType.HOSTNAME) EntityQueryServiceBlockingStub(org.hypertrace.entity.query.service.v1.EntityQueryServiceGrpc.EntityQueryServiceBlockingStub) AfterAll(org.junit.jupiter.api.AfterAll) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) BulkEntityArrayAttributeUpdateRequest(org.hypertrace.entity.query.service.v1.BulkEntityArrayAttributeUpdateRequest) K8S_SERVICE(org.hypertrace.entity.v1.servicetype.ServiceType.K8S_SERVICE) BulkEntityUpdateRequest(org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest) Set(java.util.Set) AttributeValueMap(org.hypertrace.entity.data.service.v1.AttributeValueMap) UUID(java.util.UUID) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ServiceAttribute(org.hypertrace.entity.constants.v1.ServiceAttribute) IntegrationTestServerUtil(org.hypertrace.core.serviceframework.IntegrationTestServerUtil) ColumnMetadata(org.hypertrace.entity.query.service.v1.ColumnMetadata) Operator(org.hypertrace.entity.query.service.v1.Operator) EntityServiceTestConfig(org.hypertrace.entity.service.client.config.EntityServiceTestConfig) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Entry(java.util.Map.Entry) TotalEntitiesRequest(org.hypertrace.entity.query.service.v1.TotalEntitiesRequest) EntityServiceTestConfig.getServiceConfig(org.hypertrace.entity.service.client.config.EntityServiceTestConfig.getServiceConfig) ENTITY_TYPES_COLLECTION(org.hypertrace.entity.service.constants.EntityCollectionConstants.ENTITY_TYPES_COLLECTION) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) Assertions.assertDoesNotThrow(org.junit.jupiter.api.Assertions.assertDoesNotThrow) GrpcClientRequestContextUtil(org.hypertrace.core.grpcutils.client.GrpcClientRequestContextUtil) LiteralConstant(org.hypertrace.entity.query.service.v1.LiteralConstant) SetAttribute(org.hypertrace.entity.query.service.v1.SetAttribute) STRING_MAP(org.hypertrace.entity.query.service.v1.ValueType.STRING_MAP) Row(org.hypertrace.entity.query.service.v1.Row) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) AttributeMetadata(org.hypertrace.core.attribute.service.v1.AttributeMetadata) HashMap(java.util.HashMap) Nested(org.junit.jupiter.api.Nested) ArrayList(java.util.ArrayList) Datastore(org.hypertrace.core.documentstore.Datastore) Collectors.toUnmodifiableMap(java.util.stream.Collectors.toUnmodifiableMap) Filter(org.hypertrace.entity.query.service.v1.Filter) STRING_ARRAY(org.hypertrace.entity.query.service.v1.ValueType.STRING_ARRAY) Lists(com.google.common.collect.Lists) EntityUpdateInfo(org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest.EntityUpdateInfo) AttributeValue(org.hypertrace.entity.data.service.v1.AttributeValue) ConfigFactory(com.typesafe.config.ConfigFactory) AttributeScope(org.hypertrace.core.attribute.service.v1.AttributeScope) Entity(org.hypertrace.entity.data.service.v1.Entity) EntityQueryServiceGrpc(org.hypertrace.entity.query.service.v1.EntityQueryServiceGrpc) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) AttributeServiceClient(org.hypertrace.core.attribute.service.client.AttributeServiceClient) EntityType(org.hypertrace.entity.v1.entitytype.EntityType) AttributeSource(org.hypertrace.core.attribute.service.v1.AttributeSource) Value(org.hypertrace.entity.data.service.v1.Value) TotalEntitiesResponse(org.hypertrace.entity.query.service.v1.TotalEntitiesResponse) STRING(org.hypertrace.entity.query.service.v1.ValueType.STRING) EntityDataServiceClient(org.hypertrace.entity.data.service.client.EntityDataServiceClient) EntityQueryServiceClient(org.hypertrace.entity.query.service.client.EntityQueryServiceClient) ColumnIdentifier(org.hypertrace.entity.query.service.v1.ColumnIdentifier) EntityServiceConfig(org.hypertrace.entity.service.EntityServiceConfig) Iterator(java.util.Iterator) Config(com.typesafe.config.Config) RequestContextClientCallCredsProviderFactory(org.hypertrace.core.grpcutils.client.RequestContextClientCallCredsProviderFactory) EntityServiceClientConfig(org.hypertrace.entity.service.client.config.EntityServiceClientConfig) JAEGER_SERVICE(org.hypertrace.entity.v1.servicetype.ServiceType.JAEGER_SERVICE) AttributeValueList(org.hypertrace.entity.data.service.v1.AttributeValueList) ManagedChannelBuilder(io.grpc.ManagedChannelBuilder) Expression(org.hypertrace.entity.query.service.v1.Expression) RAW_ENTITIES_COLLECTION(org.hypertrace.entity.service.constants.EntityCollectionConstants.RAW_ENTITIES_COLLECTION) ValueType(org.hypertrace.entity.query.service.v1.ValueType) Collections(java.util.Collections) AttributeCreateRequest(org.hypertrace.core.attribute.service.v1.AttributeCreateRequest) OrderByExpression(org.hypertrace.entity.query.service.v1.OrderByExpression) AttributeType(org.hypertrace.entity.type.service.v1.AttributeType) Entity(org.hypertrace.entity.data.service.v1.Entity) AttributeValue(org.hypertrace.entity.data.service.v1.AttributeValue) AttributeValueMap(org.hypertrace.entity.data.service.v1.AttributeValueMap) AttributeValueList(org.hypertrace.entity.data.service.v1.AttributeValueList) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) Test(org.junit.jupiter.api.Test)

Example 17 with Entities

use of org.hypertrace.entity.data.service.v1.Entities in project entity-service by hypertrace.

the class EntityQueryServiceTest method testCreateAndGetEntity.

@Test
public void testCreateAndGetEntity() {
    // creating an api entity with attributes
    Entity.Builder apiEntityBuilder = createApiEntity(SERVICE_ID, API_NAME, API_TYPE);
    apiEntityBuilder.putAttributes(apiAttributesMap.get(API_DISCOVERY_STATE_ATTR), createAttribute("DISCOVERED")).putAttributes(apiAttributesMap.get(API_HTTP_METHOD_ATTR), createAttribute("GET"));
    entityDataServiceClient.upsert(apiEntityBuilder.build());
    // querying the api entities
    EntityQueryRequest entityQueryRequest = EntityQueryRequest.newBuilder().setEntityType(EntityType.API.name()).addSelection(createExpression(API_DISCOVERY_STATE_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(1, values.size());
    assertEquals("DISCOVERED", values.get(0));
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity) ArrayList(java.util.ArrayList) 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)

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