use of io.gravitee.rest.api.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.
the class ScheduledSubscriptionsService method run.
@Override
public void run() {
logger.debug("Refresh subscriptions #{} started at {}", counter.incrementAndGet(), Instant.now().toString());
final Set<String> apiIds = apiService.findAllLight().stream().map(ApiEntity::getId).collect(toSet());
final SubscriptionQuery query = new SubscriptionQuery();
query.setApis(apiIds);
query.setStatuses(Collections.singleton(SubscriptionStatus.ACCEPTED));
query.setEndingAtBefore(new Date().getTime());
subscriptionService.search(query).forEach(subscription -> subscriptionService.close(subscription.getId()));
logger.debug("Refresh subscriptions #{} ended at {}", counter.get(), Instant.now().toString());
}
use of io.gravitee.rest.api.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.
the class ScheduledSubscriptionPreExpirationNotificationServiceTest method shouldFindSubscriptionExpirationsToNotify.
@Test
public void shouldFindSubscriptionExpirationsToNotify() {
Instant now = Instant.ofEpochMilli(1469022010000L);
Integer daysBeforeNotification = 10;
SubscriptionEntity subscription = mock(SubscriptionEntity.class);
when(subscriptionService.search(any(SubscriptionQuery.class))).thenReturn(Collections.singletonList(subscription));
Collection<SubscriptionEntity> subscriptionsToNotify = service.findSubscriptionExpirationsToNotify(now, daysBeforeNotification);
assertEquals(Collections.singletonList(subscription), subscriptionsToNotify);
verify(subscriptionService, times(1)).search(argThat(subscriptionQuery -> Arrays.asList(SubscriptionStatus.ACCEPTED, SubscriptionStatus.PAUSED).equals(subscriptionQuery.getStatuses()) && // 1469886010000 -> now + 10 days
subscriptionQuery.getEndingAtAfter() == 1469886010000L && // 1469889610000 -> now + 10 days + 1h (cron period)
subscriptionQuery.getEndingAtBefore() == 1469889610000L));
}
use of io.gravitee.rest.api.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.
the class FilteringServiceImpl method getEntitiesOrderByNumberOfSubscriptions.
/**
* Filters and sorts input entities by number of subscriptions.
*
* @param items Can be {@link ApiEntity} or {@link ApplicationListItem}
* @param excluded If set to true, only entities without subscriptions are returned. Else, only entities with subscriptions are returned.
* @return a {@link FilteredEntities} object with the filtered and sorted list of items and a metadata map.
*/
@Override
public <T extends FilterableItem> FilteredEntities<T> getEntitiesOrderByNumberOfSubscriptions(Collection<T> items, Boolean excluded, boolean isAsc) {
if (items == null || items.isEmpty()) {
return new FilteredEntities<>(Collections.emptyList(), new HashMap<>());
}
Function<SubscriptionEntity, String> getItemFunction;
if (items.toArray()[0] instanceof ApiEntity) {
getItemFunction = (SubscriptionEntity sub) -> sub.getApi();
} else if (items.toArray()[0] instanceof ApplicationListItem) {
getItemFunction = (SubscriptionEntity sub) -> sub.getApplication();
} else {
throw new IllegalStateException("Only ApiEntity and ApplicationListItem are allowed");
}
// find all subscribed items
SubscriptionQuery subscriptionQuery = new SubscriptionQuery();
subscriptionQuery.setApis(items.stream().map(FilterableItem::getId).collect(Collectors.toList()));
subscriptionQuery.setStatuses(Arrays.asList(SubscriptionStatus.ACCEPTED, SubscriptionStatus.PAUSED));
// group by items
Map<String, Long> subscribedItemsWithCount = subscriptionService.search(subscriptionQuery).stream().collect(Collectors.groupingBy(getItemFunction, Collectors.counting()));
// link an item with its nb of subscriptions
Map<FilterableItem, Long> itemsWithCount = new HashMap<>();
Map<String, Map<String, Object>> itemsMetadata = new HashMap<>();
Map<String, Object> subscriptionsMetadata = new HashMap<>();
itemsMetadata.put("subscriptions", subscriptionsMetadata);
items.forEach(item -> {
Long itemSubscriptionsCount = subscribedItemsWithCount.get(item.getId());
if ((excluded == null) || (!excluded && itemSubscriptionsCount != null) || (excluded && itemSubscriptionsCount == null)) {
// creation of a map which will be sorted to retrieve items in the right order
itemsWithCount.put(item, itemSubscriptionsCount == null ? 0L : itemSubscriptionsCount);
// creation of a metadata map
subscriptionsMetadata.put(item.getId(), itemSubscriptionsCount == null ? 0L : itemSubscriptionsCount);
}
});
// order the list
Comparator<Map.Entry<FilterableItem, Long>> comparingByValue = Map.Entry.comparingByValue();
if (!isAsc) {
comparingByValue = comparingByValue.reversed();
}
return new FilteredEntities(itemsWithCount.entrySet().stream().sorted(Map.Entry.<FilterableItem, Long>comparingByValue().reversed().thenComparing(Map.Entry.comparingByKey(Comparator.comparing(FilterableItem::getName, String.CASE_INSENSITIVE_ORDER)))).map(Map.Entry::getKey).collect(Collectors.toList()), itemsMetadata);
}
use of io.gravitee.rest.api.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.
the class SubscriptionServiceImpl method findByPlan.
@Override
public Collection<SubscriptionEntity> findByPlan(String plan) {
logger.debug("Find subscriptions by plan {}", plan);
SubscriptionQuery query = new SubscriptionQuery();
query.setPlan(plan);
return search(query);
}
use of io.gravitee.rest.api.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.
the class SubscriptionServiceImpl method findByApi.
@Override
public Collection<SubscriptionEntity> findByApi(String api) {
logger.debug("Find subscriptions by api {}", api);
SubscriptionQuery query = new SubscriptionQuery();
query.setApi(api);
return search(query);
}
Aggregations