Search in sources :

Example 6 with TopHitsAnalytics

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

the class EnvironmentAnalyticsResource method getTopHitsAnalytics.

@NotNull
private TopHitsAnalytics getTopHitsAnalytics(Function<ApiEntity, String> groupingByFunction) {
    Set<ApiEntity> apis = isAdmin() ? new HashSet<>(apiService.search(new ApiQuery())) : apiService.findByUser(getAuthenticatedUser(), new ApiQuery(), false);
    Map<String, Long> collect = apis.stream().collect(Collectors.groupingBy(groupingByFunction, Collectors.counting()));
    TopHitsAnalytics topHitsAnalytics = new TopHitsAnalytics();
    topHitsAnalytics.setValues(collect);
    return topHitsAnalytics;
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with TopHitsAnalytics

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

the class EnvironmentAnalyticsResource method getPlatformAnalytics.

@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get environment analytics")
@ApiResponses({ @ApiResponse(code = 200, message = "Environment analytics"), @ApiResponse(code = 500, message = "Internal server error") })
public Response getPlatformAnalytics(@BeanParam AnalyticsParam analyticsParam) {
    analyticsParam.validate();
    Analytics analytics = null;
    // add filter by Apis or Applications
    String extraFilter = null;
    if (!isAdmin()) {
        String fieldName;
        List<String> ids;
        if (APPLICATION_FIELD.equals(analyticsParam.getField())) {
            fieldName = APPLICATION_FIELD;
            ids = applicationService.findByUser(getAuthenticatedUser()).stream().map(ApplicationListItem::getId).filter(appId -> permissionService.hasPermission(APPLICATION_ANALYTICS, appId, READ)).collect(Collectors.toList());
        } else {
            fieldName = API_FIELD;
            ids = apiService.findByUser(getAuthenticatedUser(), null, false).stream().map(ApiEntity::getId).filter(apiId -> permissionService.hasPermission(API_ANALYTICS, apiId, READ)).collect(Collectors.toList());
        }
        extraFilter = getExtraFilter(fieldName, ids);
    }
    if (analyticsParam.getQuery() != null) {
        analyticsParam.setQuery(analyticsParam.getQuery().replaceAll("\\?", "1"));
    }
    switch(analyticsParam.getTypeParam().getValue()) {
        case DATE_HISTO:
            analytics = !isAdmin() && extraFilter == null ? new HistogramAnalytics() : executeDateHisto(analyticsParam, extraFilter);
            break;
        case GROUP_BY:
            analytics = !isAdmin() && extraFilter == null ? new TopHitsAnalytics() : executeGroupBy(analyticsParam, extraFilter);
            break;
        case COUNT:
            analytics = !isAdmin() && extraFilter == null ? new StatsAnalytics() : executeCount(analyticsParam, extraFilter);
            break;
        case STATS:
            analytics = !isAdmin() && extraFilter == null ? new StatsAnalytics() : executeStats(analyticsParam, extraFilter);
            break;
    }
    return Response.ok(analytics).build();
}
Also used : TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) StatsAnalytics(io.gravitee.rest.api.model.analytics.query.StatsAnalytics) HistogramAnalytics(io.gravitee.rest.api.model.analytics.HistogramAnalytics) Analytics(io.gravitee.rest.api.model.analytics.Analytics) StatsAnalytics(io.gravitee.rest.api.model.analytics.query.StatsAnalytics) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) HistogramAnalytics(io.gravitee.rest.api.model.analytics.HistogramAnalytics) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 8 with TopHitsAnalytics

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

the class ApiSubscribersResourceTest method shouldGetApiSubscribersAsPrimaryOwnerAlphaSorted.

@Test
public void shouldGetApiSubscribersAsPrimaryOwnerAlphaSorted() {
    TopHitsAnalytics mockAnalytics = new TopHitsAnalytics();
    Map<String, Long> mockedValues = new HashMap<>();
    mockedValues.put("A", 0L);
    mockedValues.put("C", 0L);
    mockedValues.put("B", 0L);
    mockAnalytics.setValues(mockedValues);
    doReturn(mockAnalytics).when(analyticsService).execute(any(GroupByQuery.class));
    SubscriptionEntity subA1 = new SubscriptionEntity();
    subA1.setApplication("A");
    subA1.setApi(API);
    SubscriptionEntity subB1 = new SubscriptionEntity();
    subB1.setApplication("B");
    subB1.setApi(API);
    SubscriptionEntity subC1 = new SubscriptionEntity();
    subC1.setApplication("C");
    subC1.setApi(API);
    doReturn(Arrays.asList(subB1, subC1, subA1)).when(subscriptionService).search(any());
    ApplicationEntity appA = new ApplicationEntity();
    appA.setId("A");
    appA.setName("A");
    doReturn(appA).when(applicationService).findById("A");
    doReturn(new Application().id("A").name("A")).when(applicationMapper).convert(eq(appA), any());
    ApplicationEntity appB = new ApplicationEntity();
    appB.setId("B");
    appB.setName("B");
    doReturn(appB).when(applicationService).findById("B");
    doReturn(new Application().id("B").name("B")).when(applicationMapper).convert(eq(appB), any());
    ApplicationEntity appC = new ApplicationEntity();
    appC.setId("C");
    appC.setName("C");
    doReturn(appC).when(applicationService).findById("C");
    doReturn(new Application().id("C").name("C")).when(applicationMapper).convert(eq(appC), any());
    final Response response = target(API).path("subscribers").request().get();
    assertEquals(OK_200, response.getStatus());
    final ApplicationsResponse applicationsResponse = response.readEntity(ApplicationsResponse.class);
    assertNotNull(applicationsResponse);
    assertEquals(3, applicationsResponse.getData().size());
    assertEquals("A", applicationsResponse.getData().get(0).getId());
    assertEquals("B", applicationsResponse.getData().get(1).getId());
    assertEquals("C", applicationsResponse.getData().get(2).getId());
}
Also used : ApplicationsResponse(io.gravitee.rest.api.portal.rest.model.ApplicationsResponse) ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Response(javax.ws.rs.core.Response) GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) Application(io.gravitee.rest.api.portal.rest.model.Application) ApplicationsResponse(io.gravitee.rest.api.portal.rest.model.ApplicationsResponse) Test(org.junit.Test)

Example 9 with TopHitsAnalytics

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

the class ApiSubscribersResourceTest method shouldGetApiSubscribersAsPrimaryOwner.

@Test
public void shouldGetApiSubscribersAsPrimaryOwner() {
    TopHitsAnalytics mockAnalytics = new TopHitsAnalytics();
    Map<String, Long> mockedValues = new HashMap<>();
    mockedValues.put("A", 10L);
    mockedValues.put("B", 20L);
    mockedValues.put("C", 30L);
    mockAnalytics.setValues(mockedValues);
    doReturn(mockAnalytics).when(analyticsService).execute(any(GroupByQuery.class));
    SubscriptionEntity subA1 = new SubscriptionEntity();
    subA1.setApplication("A");
    subA1.setApi(API);
    SubscriptionEntity subB1 = new SubscriptionEntity();
    subB1.setApplication("B");
    subB1.setApi(API);
    SubscriptionEntity subC1 = new SubscriptionEntity();
    subC1.setApplication("C");
    subC1.setApi(API);
    doReturn(Arrays.asList(subB1, subC1, subA1)).when(subscriptionService).search(any());
    ApplicationEntity appA = new ApplicationEntity();
    appA.setId("A");
    doReturn(appA).when(applicationService).findById("A");
    doReturn(new Application().id("A")).when(applicationMapper).convert(eq(appA), any());
    ApplicationEntity appB = new ApplicationEntity();
    appB.setId("B");
    doReturn(appB).when(applicationService).findById("B");
    doReturn(new Application().id("B")).when(applicationMapper).convert(eq(appB), any());
    ApplicationEntity appC = new ApplicationEntity();
    appC.setId("C");
    doReturn(appC).when(applicationService).findById("C");
    doReturn(new Application().id("C")).when(applicationMapper).convert(eq(appC), any());
    final Response response = target(API).path("subscribers").request().get();
    assertEquals(OK_200, response.getStatus());
    final ApplicationsResponse applicationsResponse = response.readEntity(ApplicationsResponse.class);
    assertNotNull(applicationsResponse);
    assertEquals(3, applicationsResponse.getData().size());
    assertEquals("C", applicationsResponse.getData().get(0).getId());
    assertEquals("B", applicationsResponse.getData().get(1).getId());
    assertEquals("A", applicationsResponse.getData().get(2).getId());
}
Also used : ApplicationsResponse(io.gravitee.rest.api.portal.rest.model.ApplicationsResponse) ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Response(javax.ws.rs.core.Response) GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) Application(io.gravitee.rest.api.portal.rest.model.Application) ApplicationsResponse(io.gravitee.rest.api.portal.rest.model.ApplicationsResponse) Test(org.junit.Test)

Aggregations

TopHitsAnalytics (io.gravitee.rest.api.model.analytics.TopHitsAnalytics)9 GroupByQuery (io.gravitee.rest.api.model.analytics.query.GroupByQuery)5 Response (javax.ws.rs.core.Response)5 Test (org.junit.Test)5 ApplicationEntity (io.gravitee.rest.api.model.ApplicationEntity)3 SubscriptionEntity (io.gravitee.rest.api.model.SubscriptionEntity)3 Application (io.gravitee.rest.api.portal.rest.model.Application)3 ApplicationsResponse (io.gravitee.rest.api.portal.rest.model.ApplicationsResponse)3 ErrorResponse (io.gravitee.rest.api.portal.rest.model.ErrorResponse)3 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)2 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)2 Instant (java.time.Instant)2 NOT_FOUND_404 (io.gravitee.common.http.HttpStatusCode.NOT_FOUND_404)1 OK_200 (io.gravitee.common.http.HttpStatusCode.OK_200)1 PrimaryOwnerEntity (io.gravitee.rest.api.model.PrimaryOwnerEntity)1 UserEntity (io.gravitee.rest.api.model.UserEntity)1 Analytics (io.gravitee.rest.api.model.analytics.Analytics)1 HistogramAnalytics (io.gravitee.rest.api.model.analytics.HistogramAnalytics)1 StatsAnalytics (io.gravitee.rest.api.model.analytics.query.StatsAnalytics)1 ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)1