Search in sources :

Example 1 with ResourceManager

use of org.eclipse.che.multiuser.resource.api.usage.ResourceManager in project devspaces-images by redhat-developer.

the class OrganizationalAccountAvailableResourcesProvider method getAvailableOrganizationResources.

/**
 * Returns total resources minus resources which are already used by organization or by any of its
 * suborganizations.
 *
 * @param organization organization id to calculate its available resources
 * @return resources which are available for usage by specified organization
 * @throws NotFoundException when organization with specified id doesn't exist
 * @throws ServerException when any other exception occurs on calculation of available resources
 */
@VisibleForTesting
List<? extends Resource> getAvailableOrganizationResources(Organization organization) throws NotFoundException, ServerException {
    final ResourceManager resourceManager = resourceManagerProvider.get();
    final List<? extends Resource> total = resourceManager.getTotalResources(organization.getId());
    final List<Resource> unavailable = new ArrayList<>(resourceManager.getUsedResources(organization.getId()));
    unavailable.addAll(getUsedResourcesBySuborganizations(organization.getQualifiedName()));
    try {
        return resourceAggregator.deduct(total, unavailable);
    } catch (NoEnoughResourcesException e) {
        LOG.warn("Organization with id {} uses more resources {} than it has {}.", organization.getId(), format(unavailable), format(total));
        return resourceAggregator.excess(total, unavailable);
    }
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) Resource(org.eclipse.che.multiuser.resource.model.Resource) ArrayList(java.util.ArrayList) ResourceManager(org.eclipse.che.multiuser.resource.api.usage.ResourceManager) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with ResourceManager

use of org.eclipse.che.multiuser.resource.api.usage.ResourceManager in project devspaces-images by redhat-developer.

the class OrganizationalAccountAvailableResourcesProvider method getUsedResourcesBySuborganizations.

/**
 * Returns resources which are used by suborganizations of specified organization.
 *
 * <p>Note that the result will includes used resources of all direct and nested suborganizations.
 *
 * @param parentQualifiedName parent qualified name, e.g. 'parentName/suborgName
 * @return resources which are used by suborganizations of specified organization.
 * @throws ServerException when any other exception occurs on calculation of used resources
 */
@VisibleForTesting
List<Resource> getUsedResourcesBySuborganizations(String parentQualifiedName) throws NotFoundException, ServerException {
    ResourceManager resourceManager = resourceManagerProvider.get();
    List<Resource> usedResources = new ArrayList<>();
    for (Organization suborganization : Pages.iterate((maxItems, skipCount) -> organizationManager.getSuborganizations(parentQualifiedName, maxItems, skipCount))) {
        usedResources.addAll(resourceManager.getUsedResources(suborganization.getId()));
    }
    return usedResources;
}
Also used : Organization(org.eclipse.che.multiuser.organization.shared.model.Organization) Resource(org.eclipse.che.multiuser.resource.model.Resource) ArrayList(java.util.ArrayList) ResourceManager(org.eclipse.che.multiuser.resource.api.usage.ResourceManager) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with ResourceManager

use of org.eclipse.che.multiuser.resource.api.usage.ResourceManager in project che-server by eclipse-che.

the class OrganizationalAccountAvailableResourcesProvider method getAvailableOrganizationResources.

/**
 * Returns total resources minus resources which are already used by organization or by any of its
 * suborganizations.
 *
 * @param organization organization id to calculate its available resources
 * @return resources which are available for usage by specified organization
 * @throws NotFoundException when organization with specified id doesn't exist
 * @throws ServerException when any other exception occurs on calculation of available resources
 */
@VisibleForTesting
List<? extends Resource> getAvailableOrganizationResources(Organization organization) throws NotFoundException, ServerException {
    final ResourceManager resourceManager = resourceManagerProvider.get();
    final List<? extends Resource> total = resourceManager.getTotalResources(organization.getId());
    final List<Resource> unavailable = new ArrayList<>(resourceManager.getUsedResources(organization.getId()));
    unavailable.addAll(getUsedResourcesBySuborganizations(organization.getQualifiedName()));
    try {
        return resourceAggregator.deduct(total, unavailable);
    } catch (NoEnoughResourcesException e) {
        LOG.warn("Organization with id {} uses more resources {} than it has {}.", organization.getId(), format(unavailable), format(total));
        return resourceAggregator.excess(total, unavailable);
    }
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) Resource(org.eclipse.che.multiuser.resource.model.Resource) ArrayList(java.util.ArrayList) ResourceManager(org.eclipse.che.multiuser.resource.api.usage.ResourceManager) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with ResourceManager

use of org.eclipse.che.multiuser.resource.api.usage.ResourceManager in project che-server by eclipse-che.

the class OrganizationalAccountAvailableResourcesProvider method getUsedResourcesBySuborganizations.

/**
 * Returns resources which are used by suborganizations of specified organization.
 *
 * <p>Note that the result will includes used resources of all direct and nested suborganizations.
 *
 * @param parentQualifiedName parent qualified name, e.g. 'parentName/suborgName
 * @return resources which are used by suborganizations of specified organization.
 * @throws ServerException when any other exception occurs on calculation of used resources
 */
@VisibleForTesting
List<Resource> getUsedResourcesBySuborganizations(String parentQualifiedName) throws NotFoundException, ServerException {
    ResourceManager resourceManager = resourceManagerProvider.get();
    List<Resource> usedResources = new ArrayList<>();
    for (Organization suborganization : Pages.iterate((maxItems, skipCount) -> organizationManager.getSuborganizations(parentQualifiedName, maxItems, skipCount))) {
        usedResources.addAll(resourceManager.getUsedResources(suborganization.getId()));
    }
    return usedResources;
}
Also used : Organization(org.eclipse.che.multiuser.organization.shared.model.Organization) Resource(org.eclipse.che.multiuser.resource.model.Resource) ArrayList(java.util.ArrayList) ResourceManager(org.eclipse.che.multiuser.resource.api.usage.ResourceManager) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 ArrayList (java.util.ArrayList)4 ResourceManager (org.eclipse.che.multiuser.resource.api.usage.ResourceManager)4 Resource (org.eclipse.che.multiuser.resource.model.Resource)4 Organization (org.eclipse.che.multiuser.organization.shared.model.Organization)2 NoEnoughResourcesException (org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException)2