Search in sources :

Example 1 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.

the class ResourceManager method getUsedResources.

/**
 * Returns list of resources which are used by given account.
 *
 * @param accountId id of account
 * @return list of resources which are used by given account
 * @throws NotFoundException when account with specified id was not found
 * @throws ServerException when some exception occurred while resources fetching
 */
public List<? extends Resource> getUsedResources(String accountId) throws NotFoundException, ServerException {
    List<Resource> usedResources = new ArrayList<>();
    for (ResourceUsageTracker usageTracker : usageTrackers) {
        Optional<Resource> usedResource = usageTracker.getUsedResource(accountId);
        usedResource.ifPresent(usedResources::add);
    }
    return usedResources;
}
Also used : Resource(org.eclipse.che.multiuser.resource.model.Resource) ArrayList(java.util.ArrayList) ResourceUsageTracker(org.eclipse.che.multiuser.resource.api.ResourceUsageTracker)

Example 2 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.

the class ResourceManager method getResourceDetails.

/**
 * Returns detailed information about resources which given account can use.
 *
 * @param accountId account id
 * @return detailed information about resources which can be used by given account
 * @throws NotFoundException when account with specified id was not found
 * @throws ServerException when some exception occurs
 */
public ResourcesDetails getResourceDetails(String accountId) throws NotFoundException, ServerException {
    final List<ProvidedResources> resources = new ArrayList<>();
    for (ResourcesProvider resourcesProvider : resourcesProviders) {
        resources.addAll(resourcesProvider.getResources(accountId));
    }
    final List<Resource> allResources = resources.stream().flatMap(providedResources -> providedResources.getResources().stream()).collect(Collectors.toList());
    return new ResourcesDetailsImpl(accountId, resources, new ArrayList<>(resourceAggregator.aggregateByType(allResources).values()));
}
Also used : ResourceUsageTracker(org.eclipse.che.multiuser.resource.api.ResourceUsageTracker) Resource(org.eclipse.che.multiuser.resource.model.Resource) Set(java.util.Set) Singleton(javax.inject.Singleton) Collectors(java.util.stream.Collectors) AvailableResourcesProvider(org.eclipse.che.multiuser.resource.api.AvailableResourcesProvider) NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ProvidedResources(org.eclipse.che.multiuser.resource.model.ProvidedResources) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) ResourcesDetailsImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourcesDetailsImpl) List(java.util.List) ServerException(org.eclipse.che.api.core.ServerException) ResourcesProvider(org.eclipse.che.multiuser.resource.api.ResourcesProvider) Map(java.util.Map) Optional(java.util.Optional) ResourcesDetails(org.eclipse.che.multiuser.resource.model.ResourcesDetails) AccountManager(org.eclipse.che.account.api.AccountManager) Account(org.eclipse.che.account.shared.model.Account) ResourceAggregator(org.eclipse.che.multiuser.resource.api.ResourceAggregator) AvailableResourcesProvider(org.eclipse.che.multiuser.resource.api.AvailableResourcesProvider) ResourcesProvider(org.eclipse.che.multiuser.resource.api.ResourcesProvider) ProvidedResources(org.eclipse.che.multiuser.resource.model.ProvidedResources) ArrayList(java.util.ArrayList) Resource(org.eclipse.che.multiuser.resource.model.Resource) ResourcesDetailsImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourcesDetailsImpl)

Example 3 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.

the class RuntimeResourceUsageTracker method getUsedResource.

@Override
public Optional<Resource> getUsedResource(String accountId) throws NotFoundException, ServerException {
    final Account account = accountManager.getById(accountId);
    final long currentlyUsedRuntimes = Pages.stream((maxItems, skipCount) -> workspaceManagerProvider.get().getByNamespace(account.getName(), false, maxItems, skipCount)).filter(ws -> STOPPED != ws.getStatus()).count();
    if (currentlyUsedRuntimes > 0) {
        return Optional.of(new ResourceImpl(RuntimeResourceType.ID, currentlyUsedRuntimes, RuntimeResourceType.UNIT));
    } else {
        return Optional.empty();
    }
}
Also used : WorkspaceManager(org.eclipse.che.api.workspace.server.WorkspaceManager) ResourceUsageTracker(org.eclipse.che.multiuser.resource.api.ResourceUsageTracker) RuntimeResourceType(org.eclipse.che.multiuser.resource.api.type.RuntimeResourceType) Provider(javax.inject.Provider) Resource(org.eclipse.che.multiuser.resource.model.Resource) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) Singleton(javax.inject.Singleton) NotFoundException(org.eclipse.che.api.core.NotFoundException) Inject(javax.inject.Inject) ServerException(org.eclipse.che.api.core.ServerException) Optional(java.util.Optional) Pages(org.eclipse.che.api.core.Pages) AccountManager(org.eclipse.che.account.api.AccountManager) Account(org.eclipse.che.account.shared.model.Account) STOPPED(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STOPPED) Account(org.eclipse.che.account.shared.model.Account) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl)

Example 4 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.

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)

Example 5 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.

the class AbstractExhaustibleResourceTest method shouldFindDifferenceResourcesAmountsOnResourcesDeduction.

@Test
public void shouldFindDifferenceResourcesAmountsOnResourcesDeduction() throws Exception {
    final Resource deducted = resourceType.deduct(new ResourceImpl(TestResourceType.ID, 1000, TestResourceType.UNIT), new ResourceImpl(TestResourceType.ID, 500, TestResourceType.UNIT));
    assertEquals(deducted.getType(), TestResourceType.ID);
    assertEquals(deducted.getAmount(), 500);
    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