Search in sources :

Example 1 with GroupByQuery

use of io.gravitee.rest.api.model.analytics.query.GroupByQuery in project gravitee-management-rest-api by gravitee-io.

the class EnvironmentAnalyticsResource method executeGroupBy.

private Analytics executeGroupBy(AnalyticsParam analyticsParam, String extraFilter) {
    GroupByQuery query = new GroupByQuery();
    query.setFrom(analyticsParam.getFrom());
    query.setTo(analyticsParam.getTo());
    query.setInterval(analyticsParam.getInterval());
    query.setQuery(analyticsParam.getQuery());
    query.setField(analyticsParam.getField());
    if (analyticsParam.getOrder() != null) {
        final GroupByQuery.Order order = new GroupByQuery.Order();
        order.setField(analyticsParam.getOrder().getField());
        order.setType(analyticsParam.getOrder().getType());
        order.setOrder(analyticsParam.getOrder().isOrder());
        query.setOrder(order);
    }
    List<Range> ranges = analyticsParam.getRanges();
    if (ranges != null) {
        Map<Double, Double> rangeMap = ranges.stream().collect(Collectors.toMap(Range::getFrom, Range::getTo));
        query.setGroups(rangeMap);
    }
    addExtraFilter(query, extraFilter);
    switch(analyticsParam.getField()) {
        case STATE_FIELD:
            {
                return getTopHitsAnalytics(api -> api.getState().name());
            }
        case LIFECYCLE_STATE_FIELD:
            {
                return getTopHitsAnalytics(api -> api.getLifecycleState().name());
            }
        default:
            return analyticsService.execute(query);
    }
}
Also used : APPLICATION_ANALYTICS(io.gravitee.rest.api.model.permissions.RolePermission.APPLICATION_ANALYTICS) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) API_ANALYTICS(io.gravitee.rest.api.model.permissions.RolePermission.API_ANALYTICS) ApiResponses(io.swagger.annotations.ApiResponses) Function(java.util.function.Function) ApiService(io.gravitee.rest.api.service.ApiService) DateHistogramQuery(io.gravitee.rest.api.model.analytics.query.DateHistogramQuery) GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) HashSet(java.util.HashSet) Inject(javax.inject.Inject) ApiOperation(io.swagger.annotations.ApiOperation) Aggregation(io.gravitee.rest.api.management.rest.resource.param.Aggregation) AggregationType(io.gravitee.rest.api.model.analytics.query.AggregationType) Map(java.util.Map) PermissionService(io.gravitee.rest.api.service.PermissionService) Analytics(io.gravitee.rest.api.model.analytics.Analytics) Api(io.swagger.annotations.Api) AbstractQuery(io.gravitee.rest.api.model.analytics.query.AbstractQuery) StatsAnalytics(io.gravitee.rest.api.model.analytics.query.StatsAnalytics) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) READ(io.gravitee.rest.api.model.permissions.RolePermissionAction.READ) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Set(java.util.Set) CountQuery(io.gravitee.rest.api.model.analytics.query.CountQuery) Collectors(java.util.stream.Collectors) BeanParam(javax.ws.rs.BeanParam) List(java.util.List) MediaType(io.gravitee.common.http.MediaType) Response(javax.ws.rs.core.Response) StatsQuery(io.gravitee.rest.api.model.analytics.query.StatsQuery) ApiResponse(io.swagger.annotations.ApiResponse) AnalyticsParam(io.gravitee.rest.api.management.rest.resource.param.AnalyticsParam) ApplicationService(io.gravitee.rest.api.service.ApplicationService) Range(io.gravitee.rest.api.management.rest.resource.param.Range) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) AnalyticsService(io.gravitee.rest.api.service.AnalyticsService) HistogramAnalytics(io.gravitee.rest.api.model.analytics.HistogramAnalytics) NotNull(org.jetbrains.annotations.NotNull) GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) Range(io.gravitee.rest.api.management.rest.resource.param.Range)

Example 2 with GroupByQuery

use of io.gravitee.rest.api.model.analytics.query.GroupByQuery in project gravitee-management-rest-api by gravitee-io.

the class ApplicationAnalyticsResourceTest method shouldGetGroupByAnalytics.

@Test
public void shouldGetGroupByAnalytics() {
    final Response response = target(APPLICATION).path("analytics").queryParam("from", 0).queryParam("to", 100).queryParam("interval", 10_000).queryParam("query", APPLICATION).queryParam("field", APPLICATION).queryParam("type", "GROUP_BY").queryParam("ranges", "10:20").queryParam("order", "orderType:orderField").request().get();
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    ArgumentCaptor<GroupByQuery> queryCaptor = ArgumentCaptor.forClass(GroupByQuery.class);
    Mockito.verify(analyticsService).execute(queryCaptor.capture());
    final GroupByQuery query = queryCaptor.getValue();
    assertEquals(0, query.getFrom());
    assertEquals(100, query.getTo());
    assertEquals(10_000, query.getInterval());
    assertEquals(APPLICATION, query.getQuery());
    assertEquals(APPLICATION, query.getField());
    assertEquals(ANALYTICS_ROOT_FIELD, query.getRootField());
    assertEquals(APPLICATION, query.getRootIdentifier());
    Order order = query.getOrder();
    assertNotNull(order);
    assertEquals("orderType", order.getType());
    assertEquals("orderField", order.getField());
    assertTrue(order.isOrder());
    Map<Double, Double> groups = query.getGroups();
    assertNotNull(groups);
    assertEquals(1, groups.size());
    Double upperRange = groups.get(Double.valueOf(10));
    assertNotNull(upperRange);
    assertEquals(0, upperRange.compareTo(Double.valueOf(20)));
}
Also used : Response(javax.ws.rs.core.Response) Order(io.gravitee.rest.api.model.analytics.query.GroupByQuery.Order) GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) Test(org.junit.Test)

Example 3 with GroupByQuery

use of io.gravitee.rest.api.model.analytics.query.GroupByQuery in project gravitee-management-rest-api by gravitee-io.

the class ApplicationSubscribersResource method getNbHitsByApplication.

private Map<String, Long> getNbHitsByApplication(String applicationId) {
    GroupByQuery query = new GroupByQuery();
    Instant now = Instant.now();
    query.setField("api");
    query.setFrom(now.minus(7, ChronoUnit.DAYS).toEpochMilli());
    query.setTo(now.toEpochMilli());
    query.setInterval(43200000);
    query.setRootField("application");
    query.setRootIdentifier(applicationId);
    TopHitsAnalytics analytics = analyticsService.execute(query);
    if (analytics != null) {
        return analytics.getValues();
    }
    return null;
}
Also used : GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) Instant(java.time.Instant) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics)

Example 4 with GroupByQuery

use of io.gravitee.rest.api.model.analytics.query.GroupByQuery in project gravitee-management-rest-api by gravitee-io.

the class ApiSubscribersResource method getNbHitsByApplication.

private Map<String, Long> getNbHitsByApplication(String apiId) {
    GroupByQuery query = new GroupByQuery();
    Instant now = Instant.now();
    query.setField("application");
    query.setFrom(now.minus(7, ChronoUnit.DAYS).toEpochMilli());
    query.setTo(now.toEpochMilli());
    query.setInterval(43200000);
    query.setRootField("api");
    query.setRootIdentifier(apiId);
    try {
        final TopHitsAnalytics analytics = analyticsService.execute(query);
        if (analytics != null) {
            return analytics.getValues();
        }
    } catch (final Exception e) {
    // do nothing as the analytics errors should not break the portal
    }
    return null;
}
Also used : GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) Instant(java.time.Instant) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException)

Aggregations

GroupByQuery (io.gravitee.rest.api.model.analytics.query.GroupByQuery)4 TopHitsAnalytics (io.gravitee.rest.api.model.analytics.TopHitsAnalytics)3 Instant (java.time.Instant)2 Response (javax.ws.rs.core.Response)2 MediaType (io.gravitee.common.http.MediaType)1 Aggregation (io.gravitee.rest.api.management.rest.resource.param.Aggregation)1 AnalyticsParam (io.gravitee.rest.api.management.rest.resource.param.AnalyticsParam)1 Range (io.gravitee.rest.api.management.rest.resource.param.Range)1 Analytics (io.gravitee.rest.api.model.analytics.Analytics)1 HistogramAnalytics (io.gravitee.rest.api.model.analytics.HistogramAnalytics)1 AbstractQuery (io.gravitee.rest.api.model.analytics.query.AbstractQuery)1 AggregationType (io.gravitee.rest.api.model.analytics.query.AggregationType)1 CountQuery (io.gravitee.rest.api.model.analytics.query.CountQuery)1 DateHistogramQuery (io.gravitee.rest.api.model.analytics.query.DateHistogramQuery)1 Order (io.gravitee.rest.api.model.analytics.query.GroupByQuery.Order)1 StatsAnalytics (io.gravitee.rest.api.model.analytics.query.StatsAnalytics)1 StatsQuery (io.gravitee.rest.api.model.analytics.query.StatsQuery)1 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)1 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)1 ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)1