Search in sources :

Example 1 with TopHitsAnalytics

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();
}
Also used : Response(javax.ws.rs.core.Response) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) Test(org.junit.Test)

Example 2 with TopHitsAnalytics

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();
}
Also used : Response(javax.ws.rs.core.Response) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) Test(org.junit.Test)

Example 3 with TopHitsAnalytics

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;
}
Also used : GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) Instant(java.time.Instant) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics)

Example 4 with TopHitsAnalytics

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;
}
Also used : GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) Instant(java.time.Instant) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException)

Example 5 with TopHitsAnalytics

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());
}
Also used : java.util(java.util) ArgumentMatchers(org.mockito.ArgumentMatchers) OK_200(io.gravitee.common.http.HttpStatusCode.OK_200) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) Error(io.gravitee.rest.api.portal.rest.model.Error) Collections.singletonList(java.util.Collections.singletonList) Application(io.gravitee.rest.api.portal.rest.model.Application) GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) ArgumentCaptor(org.mockito.ArgumentCaptor) ApplicationsResponse(io.gravitee.rest.api.portal.rest.model.ApplicationsResponse) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) Mockito.doReturn(org.mockito.Mockito.doReturn) Before(org.junit.Before) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Collections.emptySet(java.util.Collections.emptySet) NOT_FOUND_404(io.gravitee.common.http.HttpStatusCode.NOT_FOUND_404) IOException(java.io.IOException) Test(org.junit.Test) ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Mockito(org.mockito.Mockito) Response(javax.ws.rs.core.Response) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) Assert(org.junit.Assert) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) UserEntity(io.gravitee.rest.api.model.UserEntity) ApplicationsResponse(io.gravitee.rest.api.portal.rest.model.ApplicationsResponse) ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Response(javax.ws.rs.core.Response) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) Application(io.gravitee.rest.api.portal.rest.model.Application) ApplicationsResponse(io.gravitee.rest.api.portal.rest.model.ApplicationsResponse) Test(org.junit.Test)

Aggregations

TopHitsAnalytics (io.gravitee.rest.api.model.analytics.TopHitsAnalytics)9 GroupByQuery (io.gravitee.rest.api.model.analytics.query.GroupByQuery)5 Response (javax.ws.rs.core.Response)5 Test (org.junit.Test)5 ApplicationEntity (io.gravitee.rest.api.model.ApplicationEntity)3 SubscriptionEntity (io.gravitee.rest.api.model.SubscriptionEntity)3 Application (io.gravitee.rest.api.portal.rest.model.Application)3 ApplicationsResponse (io.gravitee.rest.api.portal.rest.model.ApplicationsResponse)3 ErrorResponse (io.gravitee.rest.api.portal.rest.model.ErrorResponse)3 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)2 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)2 Instant (java.time.Instant)2 NOT_FOUND_404 (io.gravitee.common.http.HttpStatusCode.NOT_FOUND_404)1 OK_200 (io.gravitee.common.http.HttpStatusCode.OK_200)1 PrimaryOwnerEntity (io.gravitee.rest.api.model.PrimaryOwnerEntity)1 UserEntity (io.gravitee.rest.api.model.UserEntity)1 Analytics (io.gravitee.rest.api.model.analytics.Analytics)1 HistogramAnalytics (io.gravitee.rest.api.model.analytics.HistogramAnalytics)1 StatsAnalytics (io.gravitee.rest.api.model.analytics.query.StatsAnalytics)1 ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)1