Search in sources :

Example 16 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource 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 17 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource 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 18 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project devspaces-images by redhat-developer.

the class WorkspaceResourceUsageTrackerTest method shouldReturnUsedWorkspacesForGivenAccount.

@Test
public void shouldReturnUsedWorkspacesForGivenAccount() throws Exception {
    when(accountManager.getById(any())).thenReturn(account);
    when(account.getName()).thenReturn("testAccount");
    when(workspaceManager.getByNamespace(anyString(), anyBoolean(), anyInt(), anyLong())).thenReturn(new Page<>(Arrays.asList(new WorkspaceImpl(), new WorkspaceImpl(), new WorkspaceImpl()), 0, 3, 3));
    Optional<Resource> usedWorkspacesOpt = workspaceResourceUsageTracker.getUsedResource("account123");
    assertTrue(usedWorkspacesOpt.isPresent());
    Resource usedWorkspaces = usedWorkspacesOpt.get();
    assertEquals(usedWorkspaces.getType(), WorkspaceResourceType.ID);
    assertEquals(usedWorkspaces.getAmount(), 3);
    assertEquals(usedWorkspaces.getUnit(), WorkspaceResourceType.UNIT);
    verify(accountManager).getById(eq("account123"));
    verify(workspaceManager).getByNamespace(eq("testAccount"), eq(false), anyInt(), anyLong());
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) Resource(org.eclipse.che.multiuser.resource.model.Resource) Test(org.testng.annotations.Test)

Example 19 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project devspaces-images by redhat-developer.

the class MultiUserWorkspaceActivityManager method getIdleTimeout.

@Override
protected long getIdleTimeout(String wsId) {
    List<? extends Resource> availableResources;
    try {
        WorkspaceImpl workspace = workspaceManager.getWorkspace(wsId);
        Account account = accountManager.getByName(workspace.getNamespace());
        availableResources = resourceManager.getAvailableResources(account.getId());
    } catch (NotFoundException | ServerException e) {
        LOG.error(e.getLocalizedMessage(), e);
        return defaultTimeout;
    }
    Optional<? extends Resource> timeoutOpt = availableResources.stream().filter(resource -> TimeoutResourceType.ID.equals(resource.getType())).findAny();
    if (timeoutOpt.isPresent()) {
        return timeoutOpt.get().getAmount() * 60 * 1000;
    } else {
        return defaultTimeout;
    }
}
Also used : WorkspaceManager(org.eclipse.che.api.workspace.server.WorkspaceManager) WorkspaceActivityDao(org.eclipse.che.api.workspace.activity.WorkspaceActivityDao) Logger(org.slf4j.Logger) ResourceManager(org.eclipse.che.multiuser.resource.api.usage.ResourceManager) Resource(org.eclipse.che.multiuser.resource.model.Resource) LoggerFactory(org.slf4j.LoggerFactory) Singleton(javax.inject.Singleton) NotFoundException(org.eclipse.che.api.core.NotFoundException) Inject(javax.inject.Inject) List(java.util.List) ServerException(org.eclipse.che.api.core.ServerException) Optional(java.util.Optional) Named(javax.inject.Named) AccountManager(org.eclipse.che.account.api.AccountManager) Account(org.eclipse.che.account.shared.model.Account) WorkspaceActivityManager(org.eclipse.che.api.workspace.activity.WorkspaceActivityManager) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) TimeoutResourceType(org.eclipse.che.multiuser.resource.api.type.TimeoutResourceType) EventService(org.eclipse.che.api.core.notification.EventService) Account(org.eclipse.che.account.shared.model.Account) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) ServerException(org.eclipse.che.api.core.ServerException) NotFoundException(org.eclipse.che.api.core.NotFoundException)

Example 20 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project devspaces-images by redhat-developer.

the class AbstractExhaustibleResourceTest method shouldReturnResourceWithMinusOneAmountWhenTotalResourceHasMinusOneOnResourcesDeduction.

@Test
public void shouldReturnResourceWithMinusOneAmountWhenTotalResourceHasMinusOneOnResourcesDeduction() throws Exception {
    final Resource deducted = resourceType.deduct(new ResourceImpl(TestResourceType.ID, -1, TestResourceType.UNIT), new ResourceImpl(TestResourceType.ID, 500, TestResourceType.UNIT));
    assertEquals(deducted.getType(), TestResourceType.ID);
    assertEquals(deducted.getAmount(), -1);
    assertEquals(deducted.getUnit(), TestResourceType.UNIT);
}
Also used : ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) Resource(org.eclipse.che.multiuser.resource.model.Resource) Test(org.testng.annotations.Test)

Aggregations

Resource (org.eclipse.che.multiuser.resource.model.Resource)52 ResourceImpl (org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl)24 Test (org.testng.annotations.Test)24 ArrayList (java.util.ArrayList)14 NotFoundException (org.eclipse.che.api.core.NotFoundException)12 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)12 Optional (java.util.Optional)10 Inject (javax.inject.Inject)10 Singleton (javax.inject.Singleton)10 AccountManager (org.eclipse.che.account.api.AccountManager)10 Account (org.eclipse.che.account.shared.model.Account)10 ServerException (org.eclipse.che.api.core.ServerException)10 ResourceUsageTracker (org.eclipse.che.multiuser.resource.api.ResourceUsageTracker)10 NoEnoughResourcesException (org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException)10 List (java.util.List)8 WorkspaceManager (org.eclipse.che.api.workspace.server.WorkspaceManager)8 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 HashMap (java.util.HashMap)6 Collectors (java.util.stream.Collectors)6 Provider (javax.inject.Provider)6