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());
}
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());
}
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);
}
Aggregations