Search in sources :

Example 11 with Builder

use of org.hypertrace.gateway.service.v1.entity.Entity.Builder in project gateway-service by hypertrace.

the class QueryServiceEntityFetcherTests method test_getEntitiesBySpace.

@Test
public void test_getEntitiesBySpace() {
    long startTime = 1L;
    long endTime = 10L;
    int limit = 10;
    int offset = 0;
    String tenantId = "TENANT_ID";
    String space = "test-space";
    Map<String, String> requestHeaders = Map.of("x-tenant-id", tenantId);
    AttributeScope entityType = AttributeScope.API;
    EntitiesRequest entitiesRequest = EntitiesRequest.newBuilder().setEntityType(entityType.name()).setStartTimeMillis(startTime).setEndTimeMillis(endTime).addSelection(buildExpression(API_NAME_ATTR)).setSpaceId(space).setLimit(limit).setOffset(offset).build();
    EntitiesRequestContext entitiesRequestContext = new EntitiesRequestContext(tenantId, startTime, endTime, entityType.name(), "API.startTime", requestHeaders);
    QueryRequest expectedQueryRequest = QueryRequest.newBuilder().addSelection(createAttributeExpression(API_ID_ATTR)).addSelection(createAttributeExpression(API_NAME_ATTR)).addSelection(QueryRequestUtil.createCountByColumnSelection("API.id")).setFilter(createQsRequestFilter(API_START_TIME_ATTR, API_ID_ATTR, startTime, endTime, createStringFilter(SPACE_IDS_ATTR, Operator.EQ, "test-space"))).addGroupBy(createAttributeExpression(API_ID_ATTR)).addGroupBy(createAttributeExpression(API_NAME_ATTR)).setOffset(offset).setLimit(QueryServiceClient.DEFAULT_QUERY_SERVICE_GROUP_BY_LIMIT).build();
    List<ResultSetChunk> resultSetChunks = List.of(getResultSetChunk(List.of(API_ID_ATTR, API_NAME_ATTR), new String[][] { { "api-id-0", "api-0" } }));
    Map<EntityKey, Builder> expectedEntityKeyBuilderResponseMap = Map.of(EntityKey.of("api-id-0"), Entity.newBuilder().setEntityType(AttributeScope.API.name()).setId("api-id-0").putAttribute(API_NAME_ATTR, getStringValue("api-0")).putAttribute(API_ID_ATTR, getStringValue("api-id-0")));
    EntityFetcherResponse expectedEntityFetcherResponse = new EntityFetcherResponse(expectedEntityKeyBuilderResponseMap);
    when(queryServiceClient.executeQuery(eq(expectedQueryRequest), eq(requestHeaders), eq(500))).thenReturn(resultSetChunks.iterator());
    compareEntityFetcherResponses(expectedEntityFetcherResponse, queryServiceEntityFetcher.getEntities(entitiesRequestContext, entitiesRequest));
}
Also used : QueryRequest(org.hypertrace.core.query.service.api.QueryRequest) Builder(org.hypertrace.gateway.service.v1.entity.Entity.Builder) EntityKey(org.hypertrace.gateway.service.entity.EntityKey) EntitiesRequest(org.hypertrace.gateway.service.v1.entity.EntitiesRequest) AttributeScope(org.hypertrace.core.attribute.service.v1.AttributeScope) EntitiesRequestContext(org.hypertrace.gateway.service.entity.EntitiesRequestContext) QueryServiceRequestAndResponseUtils.getResultSetChunk(org.hypertrace.gateway.service.common.QueryServiceRequestAndResponseUtils.getResultSetChunk) ResultSetChunk(org.hypertrace.core.query.service.api.ResultSetChunk) Test(org.junit.jupiter.api.Test)

Example 12 with Builder

use of org.hypertrace.gateway.service.v1.entity.Entity.Builder in project gateway-service by hypertrace.

the class ExecutionVisitor method visit.

@Override
public EntityResponse visit(SortAndPaginateNode sortAndPaginateNode) {
    EntityResponse childNodeResponse = sortAndPaginateNode.getChildNode().acceptVisitor(this);
    // Create a list from elements of HashMap
    List<Map.Entry<EntityKey, Builder>> list = new LinkedList<>(childNodeResponse.getEntityFetcherResponse().getEntityKeyBuilderMap().entrySet());
    // Sort the list
    List<Map.Entry<EntityKey, Entity.Builder>> sortedList = DataCollectionUtil.limitAndSort(list.stream(), sortAndPaginateNode.getLimit(), sortAndPaginateNode.getOffset(), sortAndPaginateNode.getOrderByExpressionList().size(), new EntityKeyEntityBuilderEntryComparator(sortAndPaginateNode.getOrderByExpressionList()));
    // put data from sorted list to a linked hashmap
    Map<EntityKey, Builder> linkedHashMap = new LinkedHashMap<>();
    sortedList.forEach(entry -> linkedHashMap.put(entry.getKey(), entry.getValue()));
    return new EntityResponse(new EntityFetcherResponse(linkedHashMap), childNodeResponse.getTotal());
}
Also used : Entity(org.hypertrace.gateway.service.v1.entity.Entity) EntityKey(org.hypertrace.gateway.service.entity.EntityKey) EntityFetcherResponse(org.hypertrace.gateway.service.common.datafetcher.EntityFetcherResponse) EntityResponse(org.hypertrace.gateway.service.common.datafetcher.EntityResponse) EntityKeyEntityBuilderEntryComparator(org.hypertrace.gateway.service.entity.EntityKeyEntityBuilderEntryComparator) Builder(org.hypertrace.gateway.service.v1.entity.Entity.Builder) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap)

Example 13 with Builder

use of org.hypertrace.gateway.service.v1.entity.Entity.Builder in project gateway-service by hypertrace.

the class ExecutionVisitor method visit.

@Override
public EntityResponse visit(PaginateOnlyNode paginateOnlyNode) {
    EntityResponse childNodeResponse = paginateOnlyNode.getChildNode().acceptVisitor(this);
    // Create a list from elements of HashMap
    List<Map.Entry<EntityKey, Builder>> list = new LinkedList<>(childNodeResponse.getEntityFetcherResponse().getEntityKeyBuilderMap().entrySet());
    // Sort the list
    List<Map.Entry<EntityKey, Entity.Builder>> sortedList = DataCollectionUtil.paginateAndLimit(list.stream(), paginateOnlyNode.getLimit(), paginateOnlyNode.getOffset());
    // put data from sorted list to a linked hashmap
    Map<EntityKey, Builder> linkedHashMap = new LinkedHashMap<>();
    sortedList.forEach(entry -> linkedHashMap.put(entry.getKey(), entry.getValue()));
    return new EntityResponse(new EntityFetcherResponse(linkedHashMap), childNodeResponse.getTotal());
}
Also used : Entity(org.hypertrace.gateway.service.v1.entity.Entity) EntityKey(org.hypertrace.gateway.service.entity.EntityKey) EntityFetcherResponse(org.hypertrace.gateway.service.common.datafetcher.EntityFetcherResponse) EntityResponse(org.hypertrace.gateway.service.common.datafetcher.EntityResponse) Builder(org.hypertrace.gateway.service.v1.entity.Entity.Builder) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap)

Example 14 with Builder

use of org.hypertrace.gateway.service.v1.entity.Entity.Builder in project gateway-service by hypertrace.

the class RequestHandler method handleQueryServiceResponse.

private ExploreResponse.Builder handleQueryServiceResponse(ExploreRequestContext context, Iterator<ResultSetChunk> resultSetChunkIterator, ExploreRequestContext requestContext, AttributeMetadataProvider attributeMetadataProvider) {
    ExploreResponse.Builder builder = ExploreResponse.newBuilder();
    while (resultSetChunkIterator.hasNext()) {
        ResultSetChunk chunk = resultSetChunkIterator.next();
        getLogger().debug("Received chunk: {}", chunk);
        if (chunk.getRowCount() < 1) {
            break;
        }
        if (!chunk.hasResultSetMetadata()) {
            getLogger().warn("Chunk doesn't have result metadata so couldn't process the response.");
            break;
        }
        chunk.getRowList().forEach(row -> handleQueryServiceResponseSingleRow(row, chunk.getResultSetMetadata(), builder, requestContext, attributeMetadataProvider));
    }
    // If there's a Group By in the request, we need to do the sorting and pagination ourselves.
    if (requestContext.hasGroupBy()) {
        sortAndPaginatePostProcess(builder, requestContext.getOrderByExpressions(), requestContext.getRowLimitBeforeRest(), requestContext.getOffset());
    }
    if (requestContext.hasGroupBy() && requestContext.getIncludeRestGroup()) {
        theRestGroupRequestHandler.getRowsForTheRestGroup(context, requestContext.getExploreRequest(), builder);
    }
    return builder;
}
Also used : ExploreResponse(org.hypertrace.gateway.service.v1.explore.ExploreResponse) ResultSetChunk(org.hypertrace.core.query.service.api.ResultSetChunk)

Example 15 with Builder

use of org.hypertrace.gateway.service.v1.entity.Entity.Builder in project gateway-service by hypertrace.

the class EntityRequestHandler method handleRequest.

@Override
public ExploreResponse.Builder handleRequest(ExploreRequestContext requestContext, ExploreRequest exploreRequest) {
    // ourselves.
    if (!exploreRequest.getGroupByList().isEmpty()) {
        requestContext.setHasGroupBy(true);
    }
    Set<String> entityIds = getEntityIds(requestContext, exploreRequest);
    Iterator<ResultSetChunk> resultSetChunkIterator = entityServiceEntityFetcher.getResults(requestContext, exploreRequest, entityIds);
    ExploreResponse.Builder builder = ExploreResponse.newBuilder();
    while (resultSetChunkIterator.hasNext()) {
        org.hypertrace.entity.query.service.v1.ResultSetChunk chunk = resultSetChunkIterator.next();
        getLogger().debug("Received chunk: {}", chunk);
        if (chunk.getRowCount() < 1) {
            break;
        }
        if (!chunk.hasResultSetMetadata()) {
            getLogger().warn("Chunk doesn't have result metadata so couldn't process the response.");
            break;
        }
        chunk.getRowList().forEach(row -> handleRow(row, chunk.getResultSetMetadata(), builder, requestContext, attributeMetadataProvider));
    }
    // If there's a Group By in the request, we need to do the sorting and pagination ourselves.
    if (requestContext.hasGroupBy()) {
        sortAndPaginatePostProcess(builder, requestContext.getOrderByExpressions(), requestContext.getRowLimitBeforeRest(), requestContext.getOffset());
    }
    if (requestContext.hasGroupBy() && requestContext.getIncludeRestGroup()) {
        getTheRestGroupRequestHandler().getRowsForTheRestGroup(requestContext, exploreRequest, builder);
    }
    return builder;
}
Also used : ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) ExploreResponse(org.hypertrace.gateway.service.v1.explore.ExploreResponse) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk)

Aggregations

EntityKey (org.hypertrace.gateway.service.entity.EntityKey)15 EntitiesRequest (org.hypertrace.gateway.service.v1.entity.EntitiesRequest)15 EntitiesRequestContext (org.hypertrace.gateway.service.entity.EntitiesRequestContext)14 Builder (org.hypertrace.gateway.service.v1.entity.Entity.Builder)14 OrderByExpression (org.hypertrace.gateway.service.v1.common.OrderByExpression)12 Test (org.junit.jupiter.api.Test)11 AttributeScope (org.hypertrace.core.attribute.service.v1.AttributeScope)10 Expression (org.hypertrace.gateway.service.v1.common.Expression)10 LinkedHashMap (java.util.LinkedHashMap)9 EntitiesRequestAndResponseUtils.buildOrderByExpression (org.hypertrace.gateway.service.common.EntitiesRequestAndResponseUtils.buildOrderByExpression)9 EntityFetcherResponse (org.hypertrace.gateway.service.common.datafetcher.EntityFetcherResponse)9 QueryRequest (org.hypertrace.core.query.service.api.QueryRequest)8 ResultSetChunk (org.hypertrace.core.query.service.api.ResultSetChunk)8 EntityResponse (org.hypertrace.gateway.service.common.datafetcher.EntityResponse)8 TimeAggregation (org.hypertrace.gateway.service.v1.common.TimeAggregation)8 AttributeMetadata (org.hypertrace.core.attribute.service.v1.AttributeMetadata)7 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Map (java.util.Map)5 Collectors (java.util.stream.Collectors)5