use of io.gravitee.rest.api.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.
the class ApiMetricsResource method getApiNbSubscribers.
private Number getApiNbSubscribers(String apiId) {
// find all subscribed apis
SubscriptionQuery subscriptionQuery = new SubscriptionQuery();
subscriptionQuery.setApi(apiId);
subscriptionQuery.setStatuses(Arrays.asList(SubscriptionStatus.ACCEPTED, SubscriptionStatus.PAUSED));
// group by apis
Collection<SubscriptionEntity> searchResult = subscriptionService.search(subscriptionQuery);
if (searchResult != null) {
return searchResult.stream().collect(Collectors.groupingBy(SubscriptionEntity::getApi, Collectors.counting())).get(apiId);
}
return null;
}
use of io.gravitee.rest.api.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.
the class SubscriptionServiceImpl method findByApplicationAndPlan.
@Override
public Collection<SubscriptionEntity> findByApplicationAndPlan(String application, String plan) {
logger.debug("Find subscriptions by application {} and plan {}", application, plan);
SubscriptionQuery query = new SubscriptionQuery();
if (plan != null) {
query.setPlan(plan);
}
if (application != null && !application.trim().isEmpty()) {
query.setApplication(application);
} else if (isAuthenticated()) {
Set<ApplicationListItem> applications = applicationService.findByUser(getAuthenticatedUsername());
query.setApplications(applications.stream().map(ApplicationListItem::getId).collect(toList()));
}
return search(query);
}
use of io.gravitee.rest.api.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.
the class ScheduledSubscriptionPreExpirationNotificationService method findSubscriptionExpirationsToNotify.
@VisibleForTesting
Collection<SubscriptionEntity> findSubscriptionExpirationsToNotify(Instant now, Integer daysToExpiration) {
long expirationStartingTime = now.plus(Duration.ofDays((long) daysToExpiration)).getEpochSecond() * 1000;
SubscriptionQuery query = new SubscriptionQuery();
query.setStatuses(Arrays.asList(SubscriptionStatus.ACCEPTED, SubscriptionStatus.PAUSED));
query.setEndingAtAfter(expirationStartingTime);
query.setEndingAtBefore(expirationStartingTime + cronPeriodInMs);
return subscriptionService.search(query);
}
use of io.gravitee.rest.api.model.subscription.SubscriptionQuery 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());
}
use of io.gravitee.rest.api.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.
the class SubscriptionsResource method getSubscriptions.
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getSubscriptions(@QueryParam("apiId") String apiId, @QueryParam("applicationId") String applicationId, @QueryParam("statuses") List<SubscriptionStatus> statuses, @BeanParam PaginationParam paginationParam) {
final boolean withoutPagination = paginationParam.getSize() != null && paginationParam.getSize().equals(-1);
final SubscriptionQuery query = new SubscriptionQuery();
query.setApi(apiId);
query.setApplication(applicationId);
final Map<String, Map<String, Object>> metadata = new HashMap<>();
if (applicationId == null) {
final Set<ApplicationListItem> applications = applicationService.findByUser(getAuthenticatedUser());
if (applications == null || applications.isEmpty()) {
return createListResponse(emptyList(), paginationParam, !withoutPagination);
}
query.setApplications(applications.stream().map(ApplicationListItem::getId).collect(toSet()));
applications.forEach(application -> {
final Map<String, Object> m = new HashMap<>();
m.put("name", application.getName());
metadata.put(application.getId(), m);
});
} else if (!hasPermission(RolePermission.APPLICATION_SUBSCRIPTION, applicationId, RolePermissionAction.READ)) {
throw new ForbiddenAccessException();
}
if (statuses != null && !statuses.isEmpty()) {
query.setStatuses(statuses);
}
final Collection<SubscriptionEntity> subscriptions;
if (withoutPagination) {
subscriptions = subscriptionService.search(query);
} else {
final Page<SubscriptionEntity> pagedSubscriptions = subscriptionService.search(query, new PageableImpl(paginationParam.getPage(), paginationParam.getSize()));
if (pagedSubscriptions == null) {
subscriptions = emptyList();
} else {
subscriptions = pagedSubscriptions.getContent();
}
}
final List<Subscription> subscriptionList = subscriptions.stream().map(subscriptionMapper::convert).collect(Collectors.toList());
subscriptionList.forEach(subscription -> {
final ApiEntity api = apiService.findById(subscription.getApi());
if (api != null) {
final Map<String, Object> m = new HashMap<>();
m.put("name", api.getName());
m.put("pictureUrl", apiMapper.computeApiLinks(PortalApiLinkHelper.apisURL(uriInfo.getBaseUriBuilder(), api.getId()), api.getUpdatedAt()).getPicture());
m.put("state", api.getLifecycleState());
m.put("version", api.getVersion());
m.put("entrypoints", api.getEntrypoints());
metadata.put(api.getId(), m);
}
final PlanEntity plan = planService.findById(subscription.getPlan());
if (plan != null) {
final Map<String, Object> m = new HashMap<>();
m.put("name", plan.getName());
metadata.put(plan.getId(), m);
}
final UserEntity user = userService.findById(subscription.getSubscribedBy(), true);
if (user != null) {
final Map<String, Object> m = new HashMap<>();
m.put("name", user.getDisplayName());
metadata.put(user.getId(), m);
}
});
return createListResponse(subscriptionList, paginationParam, metadata, !withoutPagination);
}
Aggregations