Search in sources :

Example 16 with SubscriptionQuery

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());
}
Also used : SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery)

Example 17 with SubscriptionQuery

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));
}
Also used : InjectMocks(org.mockito.InjectMocks) io.gravitee.rest.api.service(io.gravitee.rest.api.service) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) Test(org.junit.Test) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Mockito(org.mockito.Mockito) ApiKeyQuery(io.gravitee.rest.api.model.key.ApiKeyQuery) EmailNotificationBuilder(io.gravitee.rest.api.service.builder.EmailNotificationBuilder) io.gravitee.rest.api.model(io.gravitee.rest.api.model) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assert.assertEquals(org.junit.Assert.assertEquals) Instant(java.time.Instant) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) Test(org.junit.Test)

Example 18 with SubscriptionQuery

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);
}
Also used : ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) TopApiEntity(io.gravitee.rest.api.model.TopApiEntity) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) FilteredEntities(io.gravitee.rest.api.model.filtering.FilteredEntities) FilterableItem(io.gravitee.rest.api.model.filtering.FilterableItem)

Example 19 with SubscriptionQuery

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);
}
Also used : SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery)

Example 20 with SubscriptionQuery

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);
}
Also used : SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery)

Aggregations

SubscriptionQuery (io.gravitee.rest.api.model.subscription.SubscriptionQuery)20 SubscriptionEntity (io.gravitee.rest.api.model.SubscriptionEntity)9 ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)8 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)7 java.util (java.util)7 Collectors (java.util.stream.Collectors)7 SubscriptionService (io.gravitee.rest.api.service.SubscriptionService)5 MediaType (io.gravitee.common.http.MediaType)4 ApplicationService (io.gravitee.rest.api.service.ApplicationService)4 Inject (javax.inject.Inject)4 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)3 io.gravitee.rest.api.model (io.gravitee.rest.api.model)3 PageableImpl (io.gravitee.rest.api.model.common.PageableImpl)3 RolePermissionAction (io.gravitee.rest.api.model.permissions.RolePermissionAction)3 io.gravitee.rest.api.service (io.gravitee.rest.api.service)3 GraviteeContext (io.gravitee.rest.api.service.common.GraviteeContext)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Page (io.gravitee.common.data.domain.Page)2 io.gravitee.repository.management.model (io.gravitee.repository.management.model)2