Search in sources :

Example 1 with RatingSummaryEntity

use of io.gravitee.rest.api.model.RatingSummaryEntity in project gravitee-management-rest-api by gravitee-io.

the class ApisResource method convert.

private ApiListItem convert(ApiEntity api, boolean isRatingServiceEnabled) {
    final ApiListItem apiItem = new ApiListItem();
    apiItem.setId(api.getId());
    apiItem.setName(api.getName());
    apiItem.setVersion(api.getVersion());
    apiItem.setDescription(api.getDescription());
    final UriBuilder ub = uriInfo.getBaseUriBuilder();
    final UriBuilder uriBuilder = ub.path("organizations").path(GraviteeContext.getCurrentOrganization()).path("environments").path(GraviteeContext.getCurrentEnvironment()).path("apis").path(api.getId()).path("picture");
    // force browser to get if updated
    uriBuilder.queryParam("hash", api.getUpdatedAt().getTime());
    apiItem.setPictureUrl(uriBuilder.build().toString());
    apiItem.setCategories(api.getCategories());
    apiItem.setCreatedAt(api.getCreatedAt());
    apiItem.setUpdatedAt(api.getUpdatedAt());
    apiItem.setLabels(api.getLabels());
    apiItem.setPrimaryOwner(api.getPrimaryOwner());
    apiItem.setHasHealthCheckEnabled(apiService.hasHealthCheckEnabled(api, false));
    if (api.getVisibility() != null) {
        apiItem.setVisibility(io.gravitee.rest.api.model.Visibility.valueOf(api.getVisibility().toString()));
    }
    if (api.getState() != null) {
        apiItem.setState(Lifecycle.State.valueOf(api.getState().toString()));
    }
    if (api.getProxy() != null) {
        apiItem.setVirtualHosts(api.getProxy().getVirtualHosts());
    }
    if (isRatingServiceEnabled) {
        final RatingSummaryEntity ratingSummary = ratingService.findSummaryByApi(api.getId());
        apiItem.setRate(ratingSummary.getAverageRate());
        apiItem.setNumberOfRatings(ratingSummary.getNumberOfRatings());
    }
    apiItem.setTags(api.getTags());
    if (api.getLifecycleState() != null) {
        apiItem.setLifecycleState(ApiLifecycleState.valueOf(api.getLifecycleState().toString()));
    }
    if (api.getWorkflowState() != null) {
        apiItem.setWorkflowState(WorkflowState.valueOf(api.getWorkflowState().toString()));
    }
    // Issue https://github.com/gravitee-io/issues/issues/3356
    if (api.getProxy().getVirtualHosts() != null && !api.getProxy().getVirtualHosts().isEmpty()) {
        apiItem.setContextPath(api.getProxy().getVirtualHosts().get(0).getPath());
    }
    return apiItem;
}
Also used : RatingSummaryEntity(io.gravitee.rest.api.model.RatingSummaryEntity) UriBuilder(javax.ws.rs.core.UriBuilder)

Example 2 with RatingSummaryEntity

use of io.gravitee.rest.api.model.RatingSummaryEntity in project gravitee-management-rest-api by gravitee-io.

the class FilteringServiceImpl method getRatedApis.

private FilteredEntities<ApiEntity> getRatedApis(Collection<ApiEntity> apis, boolean excluded) {
    // keep apis with ratings
    Map<ApiEntity, RatingSummaryEntity> ratings = new HashMap<>();
    // APIPortal: should create a specific service to retrieve all the information
    // in one call to the repository
    apis.forEach(api -> {
        RatingSummaryEntity apiRatingSummary = ratingService.findSummaryByApi(api.getId());
        if (apiRatingSummary != null && apiRatingSummary.getNumberOfRatings() > 0) {
            ratings.put(api, apiRatingSummary);
        }
    });
    if (excluded) {
        return new FilteredEntities<>(apis.stream().filter(api -> !ratings.containsKey(api)).collect(Collectors.toList()), null);
    } else {
        // sort apis by ratings, nb of ratings, and name
        return new FilteredEntities<>(ratings.entrySet().stream().sorted((e1, e2) -> {
            RatingSummaryEntity o1 = e1.getValue();
            RatingSummaryEntity o2 = e2.getValue();
            int averageRateComparaison = Double.compare(o2.getAverageRate(), o1.getAverageRate());
            if (averageRateComparaison != 0) {
                return averageRateComparaison;
            }
            int nbRatingsComparaison = Integer.compare(o2.getNumberOfRatings(), o1.getNumberOfRatings());
            if (nbRatingsComparaison != 0) {
                return nbRatingsComparaison;
            }
            return String.CASE_INSENSITIVE_ORDER.compare(e1.getKey().getName(), e2.getKey().getName());
        }).map(Map.Entry::getKey).collect(Collectors.toList()), null);
    }
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) TopApiService(io.gravitee.rest.api.service.TopApiService) SubscriptionStatus(io.gravitee.rest.api.model.SubscriptionStatus) RatingService(io.gravitee.rest.api.service.RatingService) Autowired(org.springframework.beans.factory.annotation.Autowired) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) FilterableItem(io.gravitee.rest.api.model.filtering.FilterableItem) SubscriptionService(io.gravitee.rest.api.service.SubscriptionService) Component(org.springframework.stereotype.Component) RatingSummaryEntity(io.gravitee.rest.api.model.RatingSummaryEntity) ApplicationService(io.gravitee.rest.api.service.ApplicationService) AbstractService(io.gravitee.rest.api.service.impl.AbstractService) TopApiEntity(io.gravitee.rest.api.model.TopApiEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) FilteringService(io.gravitee.rest.api.service.filtering.FilteringService) FilteredEntities(io.gravitee.rest.api.model.filtering.FilteredEntities) RatingSummaryEntity(io.gravitee.rest.api.model.RatingSummaryEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) TopApiEntity(io.gravitee.rest.api.model.TopApiEntity) FilteredEntities(io.gravitee.rest.api.model.filtering.FilteredEntities)

Example 3 with RatingSummaryEntity

use of io.gravitee.rest.api.model.RatingSummaryEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiMapperTest method testConvert.

@Test
public void testConvert() {
    Instant now = Instant.now();
    Date nowDate = Date.from(now);
    // init
    apiEntity = new ApiEntity();
    apiEntity.setId(API_ID);
    apiEntity.setDescription(API_DESCRIPTION);
    apiEntity.setName(API_NAME);
    apiEntity.setLabels(new ArrayList<>(Arrays.asList(API_LABEL)));
    doThrow(CategoryNotFoundException.class).when(categoryService).findNotHiddenById(API_CATEGORY_HIDDEN);
    apiEntity.setCategories(new HashSet<>(Arrays.asList(API_CATEGORY, API_CATEGORY_HIDDEN)));
    apiEntity.setEntrypoints(Arrays.asList(new ApiEntrypointEntity(API_ENTRYPOINT_1), new ApiEntrypointEntity(API + "/foo")));
    Map<String, Object> metadata = new HashMap<>();
    metadata.put("meta", API);
    apiEntity.setMetadata(metadata);
    apiEntity.setVersion(API_VERSION);
    UserEntity ownerEntity = new UserEntity();
    ownerEntity.setId(API_OWNER_ID);
    ownerEntity.setEmail(API_OWNER_EMAIL);
    ownerEntity.setFirstname(API_OWNER_FIRSTNAME);
    ownerEntity.setLastname(API_OWNER_LASTNAME);
    apiEntity.setPrimaryOwner(new PrimaryOwnerEntity(ownerEntity));
    RatingSummaryEntity ratingSummaryEntity = new RatingSummaryEntity();
    ratingSummaryEntity.setAverageRate(Double.valueOf(4.2));
    ratingSummaryEntity.setNumberOfRatings(10);
    doReturn(true).when(ratingService).isEnabled();
    doReturn(ratingSummaryEntity).when(ratingService).findSummaryByApi(API_ID);
    doReturn(true).when(parameterService).findAsBoolean(Key.PORTAL_APIS_CATEGORY_ENABLED, ParameterReferenceType.ENVIRONMENT);
    Proxy proxy = new Proxy();
    proxy.setVirtualHosts(Collections.singletonList(new VirtualHost("/foo")));
    apiEntity.setProxy(proxy);
    apiEntity.setLifecycleState(ApiLifecycleState.PUBLISHED);
    apiEntity.setUpdatedAt(nowDate);
    // Test
    Api responseApi = apiMapper.convert(apiEntity);
    assertNotNull(responseApi);
    assertNull(responseApi.getPages());
    assertNull(responseApi.getPlans());
    assertEquals(API_DESCRIPTION, responseApi.getDescription());
    assertEquals(API_ID, responseApi.getId());
    assertEquals(API_NAME, responseApi.getName());
    assertEquals(API_VERSION, responseApi.getVersion());
    assertFalse(responseApi.getDraft());
    List<String> entrypoints = responseApi.getEntrypoints();
    assertNotNull(entrypoints);
    assertEquals(2, entrypoints.size());
    assertEquals(API_ENTRYPOINT_1, entrypoints.get(0));
    assertEquals(API + "/foo", entrypoints.get(1));
    List<String> labels = responseApi.getLabels();
    assertNotNull(labels);
    assertTrue(labels.contains(API_LABEL));
    User owner = responseApi.getOwner();
    assertNotNull(owner);
    assertEquals(API_OWNER_ID, owner.getId());
    assertEquals(API_OWNER_EMAIL, owner.getEmail());
    assertEquals(API_OWNER_FIRSTNAME + ' ' + API_OWNER_LASTNAME, owner.getDisplayName());
    assertEquals(now.toEpochMilli(), responseApi.getUpdatedAt().toInstant().toEpochMilli());
    List<String> categories = responseApi.getCategories();
    assertNotNull(categories);
    assertTrue(categories.contains(API_CATEGORY));
    RatingSummary ratingSummary = responseApi.getRatingSummary();
    assertNotNull(ratingSummary);
    assertEquals(Double.valueOf(4.2), ratingSummary.getAverage());
    assertEquals(BigDecimal.valueOf(10), ratingSummary.getCount());
}
Also used : RatingSummary(io.gravitee.rest.api.portal.rest.model.RatingSummary) ApiEntrypointEntity(io.gravitee.rest.api.model.api.ApiEntrypointEntity) User(io.gravitee.rest.api.portal.rest.model.User) Instant(java.time.Instant) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) RatingSummaryEntity(io.gravitee.rest.api.model.RatingSummaryEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Proxy(io.gravitee.definition.model.Proxy) VirtualHost(io.gravitee.definition.model.VirtualHost) Api(io.gravitee.rest.api.portal.rest.model.Api) Test(org.junit.Test)

Example 4 with RatingSummaryEntity

use of io.gravitee.rest.api.model.RatingSummaryEntity in project gravitee-management-rest-api by gravitee-io.

the class FilteringServiceTest method shouldGetStarredApi.

@Test
public void shouldGetStarredApi() {
    doReturn(true).when(ratingService).isEnabled();
    RatingSummaryEntity ratingSummary1 = new RatingSummaryEntity();
    ratingSummary1.setApi("1");
    ratingSummary1.setAverageRate(4.5);
    ratingSummary1.setNumberOfRatings(3);
    doReturn(ratingSummary1).when(ratingService).findSummaryByApi("1");
    RatingSummaryEntity ratingSummary3 = new RatingSummaryEntity();
    ratingSummary3.setApi("3");
    ratingSummary3.setAverageRate(5.0);
    ratingSummary3.setNumberOfRatings(10);
    doReturn(ratingSummary3).when(ratingService).findSummaryByApi("3");
    RatingSummaryEntity ratingSummary4 = new RatingSummaryEntity();
    ratingSummary4.setApi("4");
    ratingSummary4.setAverageRate(5.0);
    ratingSummary4.setNumberOfRatings(1);
    doReturn(ratingSummary4).when(ratingService).findSummaryByApi("4");
    RatingSummaryEntity ratingSummary5 = new RatingSummaryEntity();
    ratingSummary5.setApi("5");
    ratingSummary5.setAverageRate(4.5);
    ratingSummary5.setNumberOfRatings(3);
    doReturn(ratingSummary5).when(ratingService).findSummaryByApi("5");
    FilteredEntities<ApiEntity> apiEntityFilteredEntities = filteringService.filterApis(mockApis, FilteringService.FilterType.STARRED, null);
    List<ApiEntity> filteredItems = apiEntityFilteredEntities.getFilteredItems();
    assertEquals(4, filteredItems.size());
    assertEquals("3", filteredItems.get(0).getId());
    assertEquals("4", filteredItems.get(1).getId());
    assertEquals("1", filteredItems.get(2).getId());
    assertEquals("5", filteredItems.get(3).getId());
}
Also used : RatingSummaryEntity(io.gravitee.rest.api.model.RatingSummaryEntity) TopApiEntity(io.gravitee.rest.api.model.TopApiEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Test(org.junit.Test)

Example 5 with RatingSummaryEntity

use of io.gravitee.rest.api.model.RatingSummaryEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiMapper method convert.

public Api convert(ApiEntity api) {
    final Api apiItem = new Api();
    apiItem.setDescription(api.getDescription());
    List<ApiEntrypointEntity> apiEntrypoints = api.getEntrypoints();
    if (apiEntrypoints != null) {
        List<String> entrypoints = apiEntrypoints.stream().map(ApiEntrypointEntity::getTarget).collect(Collectors.toList());
        apiItem.setEntrypoints(entrypoints);
    }
    apiItem.setDraft(api.getLifecycleState() == ApiLifecycleState.UNPUBLISHED || api.getLifecycleState() == ApiLifecycleState.CREATED);
    apiItem.setRunning(api.getState() == Lifecycle.State.STARTED);
    apiItem.setPublic(api.getVisibility() == Visibility.PUBLIC);
    apiItem.setId(api.getId());
    List<String> apiLabels = api.getLabels();
    if (apiLabels != null) {
        apiItem.setLabels(new ArrayList<>(apiLabels));
    } else {
        apiItem.setLabels(new ArrayList<>());
    }
    apiItem.setName(api.getName());
    PrimaryOwnerEntity primaryOwner = api.getPrimaryOwner();
    if (primaryOwner != null) {
        User owner = new User();
        owner.setId(primaryOwner.getId());
        owner.setDisplayName(primaryOwner.getDisplayName());
        owner.setEmail(primaryOwner.getEmail());
        apiItem.setOwner(owner);
    }
    apiItem.setPages(null);
    apiItem.setPlans(null);
    if (ratingService.isEnabled()) {
        final RatingSummaryEntity ratingSummaryEntity = ratingService.findSummaryByApi(api.getId());
        RatingSummary ratingSummary = new RatingSummary().average(ratingSummaryEntity.getAverageRate()).count(BigDecimal.valueOf(ratingSummaryEntity.getNumberOfRatings()));
        apiItem.setRatingSummary(ratingSummary);
    }
    if (api.getUpdatedAt() != null) {
        apiItem.setUpdatedAt(api.getUpdatedAt().toInstant().atOffset(ZoneOffset.UTC));
    }
    apiItem.setVersion(api.getVersion());
    boolean isCategoryModeEnabled = this.parameterService.findAsBoolean(Key.PORTAL_APIS_CATEGORY_ENABLED, ParameterReferenceType.ENVIRONMENT);
    if (isCategoryModeEnabled && api.getCategories() != null) {
        apiItem.setCategories(api.getCategories().stream().filter(categoryId -> {
            try {
                categoryService.findNotHiddenById(categoryId);
                return true;
            } catch (CategoryNotFoundException v) {
                return false;
            }
        }).collect(Collectors.toList()));
    } else {
        apiItem.setCategories(new ArrayList<>());
    }
    return apiItem;
}
Also used : RatingSummary(io.gravitee.rest.api.portal.rest.model.RatingSummary) ApiEntrypointEntity(io.gravitee.rest.api.model.api.ApiEntrypointEntity) User(io.gravitee.rest.api.portal.rest.model.User) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) RatingSummaryEntity(io.gravitee.rest.api.model.RatingSummaryEntity) CategoryNotFoundException(io.gravitee.rest.api.service.exceptions.CategoryNotFoundException) Api(io.gravitee.rest.api.portal.rest.model.Api)

Aggregations

RatingSummaryEntity (io.gravitee.rest.api.model.RatingSummaryEntity)6 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)3 PrimaryOwnerEntity (io.gravitee.rest.api.model.PrimaryOwnerEntity)2 TopApiEntity (io.gravitee.rest.api.model.TopApiEntity)2 ApiEntrypointEntity (io.gravitee.rest.api.model.api.ApiEntrypointEntity)2 Api (io.gravitee.rest.api.portal.rest.model.Api)2 RatingSummary (io.gravitee.rest.api.portal.rest.model.RatingSummary)2 User (io.gravitee.rest.api.portal.rest.model.User)2 UriBuilder (javax.ws.rs.core.UriBuilder)2 Test (org.junit.Test)2 Proxy (io.gravitee.definition.model.Proxy)1 VirtualHost (io.gravitee.definition.model.VirtualHost)1 SubscriptionEntity (io.gravitee.rest.api.model.SubscriptionEntity)1 SubscriptionStatus (io.gravitee.rest.api.model.SubscriptionStatus)1 UserEntity (io.gravitee.rest.api.model.UserEntity)1 ApiListItem (io.gravitee.rest.api.model.api.ApiListItem)1 ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)1 FilterableItem (io.gravitee.rest.api.model.filtering.FilterableItem)1 FilteredEntities (io.gravitee.rest.api.model.filtering.FilteredEntities)1 SubscriptionQuery (io.gravitee.rest.api.model.subscription.SubscriptionQuery)1