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