use of io.gravitee.rest.api.model.analytics.TopHitsAnalytics in project gravitee-management-rest-api by gravitee-io.
the class EnvironmentAnalyticsResourceTest method shouldGetEmptyTopHitsAnalyticsWhenNotAdminAndNoApp.
@Test
public void shouldGetEmptyTopHitsAnalyticsWhenNotAdminAndNoApp() {
when(applicationService.findByUser(any())).thenReturn(Collections.emptySet());
Response response = envTarget().queryParam("type", "group_by").queryParam("field", "application").queryParam("interval", 1000).queryParam("to", 1000).request().get();
assertThat(response).isNotNull();
assertThat(response.getStatus()).isEqualTo(HttpStatusCode.OK_200);
TopHitsAnalytics analytics = response.readEntity(TopHitsAnalytics.class);
assertThat(analytics.getValues()).isNull();
assertThat(analytics.getMetadata()).isNull();
}
use of io.gravitee.rest.api.model.analytics.TopHitsAnalytics in project gravitee-management-rest-api by gravitee-io.
the class EnvironmentAnalyticsResourceTest method shouldGetEmptyTopHitsAnalyticsWhenNotAdminAndNoApi.
@Test
public void shouldGetEmptyTopHitsAnalyticsWhenNotAdminAndNoApi() {
when(apiService.findByUser(any(), eq(null), eq(false))).thenReturn(Collections.emptySet());
Response response = envTarget().queryParam("type", "group_by").queryParam("field", "api").queryParam("interval", 1000).queryParam("to", 1000).request().get();
assertThat(response).isNotNull();
assertThat(response.getStatus()).isEqualTo(HttpStatusCode.OK_200);
TopHitsAnalytics analytics = response.readEntity(TopHitsAnalytics.class);
assertThat(analytics.getValues()).isNull();
assertThat(analytics.getMetadata()).isNull();
}
use of io.gravitee.rest.api.model.analytics.TopHitsAnalytics 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.TopHitsAnalytics 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;
}
use of io.gravitee.rest.api.model.analytics.TopHitsAnalytics in project gravitee-management-rest-api by gravitee-io.
the class ApiSubscribersResourceTest method shouldGetApiSubscribersNotAsPrimaryOwner.
@Test
public void shouldGetApiSubscribersNotAsPrimaryOwner() {
ApiEntity mockApi = new ApiEntity();
mockApi.setId(API);
UserEntity user = new UserEntity();
user.setId("ANOTHER_NAME");
PrimaryOwnerEntity primaryOwner = new PrimaryOwnerEntity(user);
mockApi.setPrimaryOwner(primaryOwner);
Set<ApiEntity> mockApis = new HashSet<>(Arrays.asList(mockApi));
doReturn(mockApis).when(apiService).findPublishedByUser(any(), argThat(q -> singletonList(API).equals(q.getIds())));
TopHitsAnalytics mockAnalytics = new TopHitsAnalytics();
Map<String, Long> mockedValues = new HashMap<>();
mockedValues.put("A", 10L);
mockedValues.put("B", 20L);
mockedValues.put("C", 30L);
mockAnalytics.setValues(mockedValues);
doReturn(mockAnalytics).when(analyticsService).execute(any(GroupByQuery.class));
SubscriptionEntity subA1 = new SubscriptionEntity();
subA1.setApplication("A");
subA1.setApi(API);
SubscriptionEntity subC1 = new SubscriptionEntity();
subC1.setApplication("C");
subC1.setApi(API);
doReturn(Arrays.asList(subA1, subC1)).when(subscriptionService).search(any());
ApplicationEntity appA = new ApplicationEntity();
appA.setId("A");
doReturn(appA).when(applicationService).findById("A");
doReturn(new Application().id("A")).when(applicationMapper).convert(eq(appA), any());
ApplicationEntity appC = new ApplicationEntity();
appC.setId("C");
doReturn(appC).when(applicationService).findById("C");
doReturn(new Application().id("C")).when(applicationMapper).convert(eq(appC), any());
ApplicationListItem appLIA = new ApplicationListItem();
appLIA.setId("A");
ApplicationListItem appLIC = new ApplicationListItem();
appLIC.setId("C");
doReturn(new HashSet<>(Arrays.asList(appLIA, appLIC))).when(applicationService).findByUser(USER_NAME);
final Response response = target(API).path("subscribers").request().get();
assertEquals(OK_200, response.getStatus());
ArgumentCaptor<SubscriptionQuery> queryCaptor = ArgumentCaptor.forClass(SubscriptionQuery.class);
Mockito.verify(subscriptionService).search(queryCaptor.capture());
SubscriptionQuery value = queryCaptor.getValue();
assertNotNull(value.getApplications());
assertEquals(2, value.getApplications().size());
assertTrue(value.getApplications().contains("A"));
assertTrue(value.getApplications().contains("C"));
final ApplicationsResponse applicationsResponse = response.readEntity(ApplicationsResponse.class);
assertNotNull(applicationsResponse);
assertEquals(2, applicationsResponse.getData().size());
assertEquals("C", applicationsResponse.getData().get(0).getId());
assertEquals("A", applicationsResponse.getData().get(1).getId());
}
Aggregations