Search in sources :

Example 6 with StatsAnalytics

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

the class EnvironmentAnalyticsResourceTest method shouldGetEmptyStatsAnalyticsWhenNotAdminAndNoApi.

@Test
public void shouldGetEmptyStatsAnalyticsWhenNotAdminAndNoApi() {
    when(apiService.findByUser(any(), eq(null), eq(false))).thenReturn(Collections.emptySet());
    Response response = envTarget().queryParam("type", "stats").queryParam("field", "api").queryParam("interval", 1000).queryParam("to", 1000).request().get();
    assertThat(response).isNotNull();
    assertThat(response.getStatus()).isEqualTo(HttpStatusCode.OK_200);
    StatsAnalytics analytics = response.readEntity(StatsAnalytics.class);
    assertThat(analytics.getAvg()).isNull();
    assertThat(analytics.getCount()).isNull();
}
Also used : Response(javax.ws.rs.core.Response) StatsAnalytics(io.gravitee.rest.api.model.analytics.query.StatsAnalytics) Test(org.junit.Test)

Example 7 with StatsAnalytics

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

the class ApiMetricsResourceTest method shouldGetApiMetrics.

@Test
public void shouldGetApiMetrics() {
    StatsAnalytics mockAnalytics = new StatsAnalytics();
    mockAnalytics.setCount(API_NB_HITS);
    doReturn(mockAnalytics).when(analyticsService).execute(any(StatsQuery.class));
    SubscriptionEntity subA1 = new SubscriptionEntity();
    subA1.setApplication("A");
    subA1.setApi(API);
    SubscriptionEntity subA2 = new SubscriptionEntity();
    subA2.setApplication("A");
    subA2.setApi("2");
    SubscriptionEntity subB1 = new SubscriptionEntity();
    subB1.setApplication("B");
    subB1.setApi(API);
    SubscriptionEntity subC4 = new SubscriptionEntity();
    subC4.setApplication("C");
    subC4.setApi("4");
    SubscriptionEntity subC8 = new SubscriptionEntity();
    subC8.setApplication("C");
    subC8.setApi("8");
    doReturn(Arrays.asList(subC8, subA2, subB1, subC4, subA1)).when(subscriptionService).search(any());
    io.gravitee.rest.api.model.healthcheck.ApiMetrics<Number> mockMetrics = new io.gravitee.rest.api.model.healthcheck.ApiMetrics<>();
    Map<String, Double> globalMetrics = new HashMap<>();
    globalMetrics.put("1w", API_HEALTHCHECK_RATIO);
    mockMetrics.setGlobal(globalMetrics);
    doReturn(mockMetrics).when(healthCheckService).getAvailability(any(), any());
    final Response response = target(API).path("metrics").request().get();
    assertEquals(OK_200, response.getStatus());
    final ApiMetrics apiMetrics = response.readEntity(ApiMetrics.class);
    assertNotNull(apiMetrics);
    assertEquals(Double.valueOf(API_NB_HITS.doubleValue()), apiMetrics.getHits());
    assertEquals(EXPECTED_API_HEALTHCHECK_RATIO, apiMetrics.getHealth());
    assertEquals(2, apiMetrics.getSubscribers());
}
Also used : StatsQuery(io.gravitee.rest.api.model.analytics.query.StatsQuery) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) ApiMetrics(io.gravitee.rest.api.portal.rest.model.ApiMetrics) ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Response(javax.ws.rs.core.Response) StatsAnalytics(io.gravitee.rest.api.model.analytics.query.StatsAnalytics) Test(org.junit.Test)

Example 8 with StatsAnalytics

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

the class ApiMetricsResource method getNbHits.

private Number getNbHits(String apiId) {
    StatsQuery query = new StatsQuery();
    Instant now = Instant.now();
    query.setFrom(now.minus(7, ChronoUnit.DAYS).toEpochMilli());
    query.setTo(now.toEpochMilli());
    query.setInterval(43200000);
    query.setRootField("api");
    query.setRootIdentifier(apiId);
    query.setField("response-time");
    try {
        final StatsAnalytics analytics = analyticsService.execute(query);
        if (analytics != null) {
            return analytics.getCount();
        }
    } catch (final Exception e) {
    // do nothing as the analytics errors should not break the portal
    }
    return null;
}
Also used : Instant(java.time.Instant) StatsQuery(io.gravitee.rest.api.model.analytics.query.StatsQuery) StatsAnalytics(io.gravitee.rest.api.model.analytics.query.StatsAnalytics) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException)

Example 9 with StatsAnalytics

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

the class EnvironmentAnalyticsResource method buildCountStat.

private StatsAnalytics buildCountStat(float countValue) {
    StatsAnalytics stats = new StatsAnalytics();
    stats.setCount(countValue);
    return stats;
}
Also used : StatsAnalytics(io.gravitee.rest.api.model.analytics.query.StatsAnalytics)

Example 10 with StatsAnalytics

use of io.gravitee.rest.api.model.analytics.query.StatsAnalytics 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)

Aggregations

StatsAnalytics (io.gravitee.rest.api.model.analytics.query.StatsAnalytics)10 Response (javax.ws.rs.core.Response)7 Test (org.junit.Test)7 StatsQuery (io.gravitee.rest.api.model.analytics.query.StatsQuery)2 SubscriptionEntity (io.gravitee.rest.api.model.SubscriptionEntity)1 Analytics (io.gravitee.rest.api.model.analytics.Analytics)1 HistogramAnalytics (io.gravitee.rest.api.model.analytics.HistogramAnalytics)1 TopHitsAnalytics (io.gravitee.rest.api.model.analytics.TopHitsAnalytics)1 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)1 ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)1 ApiMetrics (io.gravitee.rest.api.portal.rest.model.ApiMetrics)1 ErrorResponse (io.gravitee.rest.api.portal.rest.model.ErrorResponse)1 ApiNotFoundException (io.gravitee.rest.api.service.exceptions.ApiNotFoundException)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 Instant (java.time.Instant)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1