use of io.gravitee.rest.api.model.analytics.query.GroupByQuery in project gravitee-management-rest-api by gravitee-io.
the class EnvironmentAnalyticsResource method executeGroupBy.
private Analytics executeGroupBy(AnalyticsParam analyticsParam, String extraFilter) {
GroupByQuery query = new GroupByQuery();
query.setFrom(analyticsParam.getFrom());
query.setTo(analyticsParam.getTo());
query.setInterval(analyticsParam.getInterval());
query.setQuery(analyticsParam.getQuery());
query.setField(analyticsParam.getField());
if (analyticsParam.getOrder() != null) {
final GroupByQuery.Order order = new GroupByQuery.Order();
order.setField(analyticsParam.getOrder().getField());
order.setType(analyticsParam.getOrder().getType());
order.setOrder(analyticsParam.getOrder().isOrder());
query.setOrder(order);
}
List<Range> ranges = analyticsParam.getRanges();
if (ranges != null) {
Map<Double, Double> rangeMap = ranges.stream().collect(Collectors.toMap(Range::getFrom, Range::getTo));
query.setGroups(rangeMap);
}
addExtraFilter(query, extraFilter);
switch(analyticsParam.getField()) {
case STATE_FIELD:
{
return getTopHitsAnalytics(api -> api.getState().name());
}
case LIFECYCLE_STATE_FIELD:
{
return getTopHitsAnalytics(api -> api.getLifecycleState().name());
}
default:
return analyticsService.execute(query);
}
}
use of io.gravitee.rest.api.model.analytics.query.GroupByQuery in project gravitee-management-rest-api by gravitee-io.
the class ApplicationAnalyticsResourceTest method shouldGetGroupByAnalytics.
@Test
public void shouldGetGroupByAnalytics() {
final Response response = target(APPLICATION).path("analytics").queryParam("from", 0).queryParam("to", 100).queryParam("interval", 10_000).queryParam("query", APPLICATION).queryParam("field", APPLICATION).queryParam("type", "GROUP_BY").queryParam("ranges", "10:20").queryParam("order", "orderType:orderField").request().get();
assertEquals(HttpStatusCode.OK_200, response.getStatus());
ArgumentCaptor<GroupByQuery> queryCaptor = ArgumentCaptor.forClass(GroupByQuery.class);
Mockito.verify(analyticsService).execute(queryCaptor.capture());
final GroupByQuery query = queryCaptor.getValue();
assertEquals(0, query.getFrom());
assertEquals(100, query.getTo());
assertEquals(10_000, query.getInterval());
assertEquals(APPLICATION, query.getQuery());
assertEquals(APPLICATION, query.getField());
assertEquals(ANALYTICS_ROOT_FIELD, query.getRootField());
assertEquals(APPLICATION, query.getRootIdentifier());
Order order = query.getOrder();
assertNotNull(order);
assertEquals("orderType", order.getType());
assertEquals("orderField", order.getField());
assertTrue(order.isOrder());
Map<Double, Double> groups = query.getGroups();
assertNotNull(groups);
assertEquals(1, groups.size());
Double upperRange = groups.get(Double.valueOf(10));
assertNotNull(upperRange);
assertEquals(0, upperRange.compareTo(Double.valueOf(20)));
}
use of io.gravitee.rest.api.model.analytics.query.GroupByQuery in project gravitee-management-rest-api by gravitee-io.
the class ApplicationSubscribersResource method getNbHitsByApplication.
private Map<String, Long> getNbHitsByApplication(String applicationId) {
GroupByQuery query = new GroupByQuery();
Instant now = Instant.now();
query.setField("api");
query.setFrom(now.minus(7, ChronoUnit.DAYS).toEpochMilli());
query.setTo(now.toEpochMilli());
query.setInterval(43200000);
query.setRootField("application");
query.setRootIdentifier(applicationId);
TopHitsAnalytics analytics = analyticsService.execute(query);
if (analytics != null) {
return analytics.getValues();
}
return null;
}
use of io.gravitee.rest.api.model.analytics.query.GroupByQuery in project gravitee-management-rest-api by gravitee-io.
the class ApiSubscribersResource method getNbHitsByApplication.
private Map<String, Long> getNbHitsByApplication(String apiId) {
GroupByQuery query = new GroupByQuery();
Instant now = Instant.now();
query.setField("application");
query.setFrom(now.minus(7, ChronoUnit.DAYS).toEpochMilli());
query.setTo(now.toEpochMilli());
query.setInterval(43200000);
query.setRootField("api");
query.setRootIdentifier(apiId);
try {
final TopHitsAnalytics analytics = analyticsService.execute(query);
if (analytics != null) {
return analytics.getValues();
}
} catch (final Exception e) {
// do nothing as the analytics errors should not break the portal
}
return null;
}
Aggregations