Search in sources :

Example 1 with ApiMetrics

use of io.gravitee.rest.api.portal.rest.model.ApiMetrics 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 2 with ApiMetrics

use of io.gravitee.rest.api.portal.rest.model.ApiMetrics in project gravitee-management-rest-api by gravitee-io.

the class ApiMetricsResourceTest method shouldGetEmptyApiMetrics.

@Test
public void shouldGetEmptyApiMetrics() {
    // Case 1
    doReturn(null).when(analyticsService).execute(any(StatsQuery.class));
    doReturn(null).when(subscriptionService).search(any());
    doReturn(null).when(healthCheckService).getAvailability(any(), any());
    Response response = target(API).path("metrics").request().get();
    assertEquals(OK_200, response.getStatus());
    ApiMetrics apiMetrics = response.readEntity(ApiMetrics.class);
    assertNotNull(apiMetrics);
    assertNull(apiMetrics.getHits());
    assertNull(apiMetrics.getHealth());
    assertNull(apiMetrics.getSubscribers());
    // Case 2
    doReturn(null).when(analyticsService).execute(any(StatsQuery.class));
    doReturn(Collections.emptyList()).when(subscriptionService).search(any());
    doReturn(new io.gravitee.rest.api.model.healthcheck.ApiMetrics<Number>()).when(healthCheckService).getAvailability(any(), any());
    response = target(API).path("metrics").request().get();
    assertEquals(OK_200, response.getStatus());
    apiMetrics = response.readEntity(ApiMetrics.class);
    assertNotNull(apiMetrics);
    assertNull(apiMetrics.getHits());
    assertNull(apiMetrics.getHealth());
    assertNull(apiMetrics.getSubscribers());
    // Case 3
    doReturn(null).when(analyticsService).execute(any(StatsQuery.class));
    doReturn(null).when(subscriptionService).search(any());
    io.gravitee.rest.api.model.healthcheck.ApiMetrics<Number> mockedMetrics = new io.gravitee.rest.api.model.healthcheck.ApiMetrics<>();
    mockedMetrics.setGlobal(Collections.singletonMap("1w", Double.NaN));
    doReturn(mockedMetrics).when(healthCheckService).getAvailability(any(), any());
    response = target(API).path("metrics").request().get();
    assertEquals(OK_200, response.getStatus());
    apiMetrics = response.readEntity(ApiMetrics.class);
    assertNotNull(apiMetrics);
    assertNull(apiMetrics.getHits());
    assertNull(apiMetrics.getHealth());
    assertNull(apiMetrics.getSubscribers());
}
Also used : ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Response(javax.ws.rs.core.Response) StatsQuery(io.gravitee.rest.api.model.analytics.query.StatsQuery) ApiMetrics(io.gravitee.rest.api.portal.rest.model.ApiMetrics) Test(org.junit.Test)

Example 3 with ApiMetrics

use of io.gravitee.rest.api.portal.rest.model.ApiMetrics in project gravitee-management-rest-api by gravitee-io.

the class ApiMetricsResource method getApiMetricsByApiId.

@GET
@Produces({ MediaType.APPLICATION_JSON })
@RequirePortalAuth
public Response getApiMetricsByApiId(@Context Request request, @PathParam("apiId") String apiId) {
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    Collection<ApiEntity> userApis = apiService.findPublishedByUser(getAuthenticatedUserOrNull(), apiQuery);
    if (userApis.stream().anyMatch(a -> a.getId().equals(apiId))) {
        Number healthRatio = getHealthRatio(apiId);
        Number nbHits = getNbHits(apiId);
        Number subscribers = getApiNbSubscribers(apiId);
        ApiMetrics metrics = new ApiMetrics();
        metrics.setHealth(healthRatio);
        metrics.setHits(nbHits);
        metrics.setSubscribers(subscribers);
        return Response.ok(metrics).build();
    }
    throw new ApiNotFoundException(apiId);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) ApiMetrics(io.gravitee.rest.api.portal.rest.model.ApiMetrics) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)

Aggregations

ApiMetrics (io.gravitee.rest.api.portal.rest.model.ApiMetrics)3 StatsQuery (io.gravitee.rest.api.model.analytics.query.StatsQuery)2 ErrorResponse (io.gravitee.rest.api.portal.rest.model.ErrorResponse)2 Response (javax.ws.rs.core.Response)2 Test (org.junit.Test)2 SubscriptionEntity (io.gravitee.rest.api.model.SubscriptionEntity)1 StatsAnalytics (io.gravitee.rest.api.model.analytics.query.StatsAnalytics)1 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)1 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)1 RequirePortalAuth (io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)1 ApiNotFoundException (io.gravitee.rest.api.service.exceptions.ApiNotFoundException)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1