use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.
the class ResourceAggregator method deduct.
/**
* Returns list which is result of deduction {@code resourceToDeduct} from {@code
* sourceResources}.
*
* @param sourceResources the source resources
* @param resourcesToDeduct the resources which should be deducted from {@code sourceResources}
* @throws NoEnoughResourcesException when {@code sourceResources} list doesn't contain enough
* resources
* @throws IllegalArgumentException when {@code sourceResources} or {@code resourcesToDeduct}
* contain resource with not supported type
*/
public List<? extends Resource> deduct(List<? extends Resource> sourceResources, List<? extends Resource> resourcesToDeduct) throws NoEnoughResourcesException {
checkSupporting(sourceResources);
checkSupporting(resourcesToDeduct);
final Map<String, Resource> result = sourceResources.stream().collect(Collectors.toMap(Resource::getType, Function.identity()));
final List<Resource> missingResources = new ArrayList<>();
for (Resource toDeduct : resourcesToDeduct) {
final Resource sourceResource = result.get(toDeduct.getType());
if (sourceResource != null) {
try {
result.put(toDeduct.getType(), deduct(sourceResource, toDeduct));
} catch (NoEnoughResourcesException e) {
result.remove(toDeduct.getType());
missingResources.addAll(e.getMissingResources());
}
} else {
missingResources.add(toDeduct);
}
}
if (!missingResources.isEmpty()) {
throw new NoEnoughResourcesException(sourceResources, resourcesToDeduct, missingResources);
}
return new ArrayList<>(result.values());
}
use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.
the class ResourceAggregator method aggregateByType.
/**
* Aggregates resources of the same type.
*
* @param resources resources list which can contain more that one instance for some type
* @return map where key is resources type and value is aggregated resource
* @throws IllegalArgumentException when resources list contains resource with not supported type
*/
public Map<String, Resource> aggregateByType(List<? extends Resource> resources) {
checkSupporting(resources);
Map<String, Resource> type2Resource = new HashMap<>();
for (Resource resource : resources) {
final Resource resource1 = type2Resource.get(resource.getType());
if (resource1 != null) {
type2Resource.put(resource.getType(), aggregate(resource1, resource));
} else {
type2Resource.put(resource.getType(), resource);
}
}
return type2Resource;
}
use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.
the class RamResourceUsageTrackerTest method shouldReturnUsedRamOfRunningWorkspaceForGivenAccount.
@Test
public void shouldReturnUsedRamOfRunningWorkspaceForGivenAccount() throws Exception {
mockWorkspaces(createWorkspace(WorkspaceStatus.RUNNING, 1000, 500, 500));
when(envRamCalculator.calculate(any(Runtime.class))).thenReturn(2000L);
final Optional<Resource> usedRamOpt = ramUsageTracker.getUsedResource(ACCOUNT_ID);
assertTrue(usedRamOpt.isPresent());
final Resource usedRam = usedRamOpt.get();
assertEquals(usedRam.getType(), RamResourceType.ID);
assertEquals(usedRam.getAmount(), 2000L);
assertEquals(usedRam.getUnit(), RamResourceType.UNIT);
verify(accountManager).getById(ACCOUNT_ID);
verify(workspaceManager).getByNamespace(anyString(), anyBoolean(), anyInt(), anyLong());
}
use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.
the class RamResourceUsageTrackerTest method returnUsedRamOfStartingWorkspaceForGivenAccount.
@Test
public void returnUsedRamOfStartingWorkspaceForGivenAccount() throws Exception {
mockWorkspaces(createWorkspace(WorkspaceStatus.STARTING, 1000, 500, 500));
when(envRamCalculator.calculate(any(Environment.class))).thenReturn(2000L);
final Optional<Resource> usedRamOpt = ramUsageTracker.getUsedResource(ACCOUNT_ID);
assertTrue(usedRamOpt.isPresent());
final Resource usedRam = usedRamOpt.get();
assertEquals(usedRam.getType(), RamResourceType.ID);
assertEquals(usedRam.getAmount(), 2000L);
assertEquals(usedRam.getUnit(), RamResourceType.UNIT);
verify(accountManager).getById(ACCOUNT_ID);
verify(workspaceManager).getByNamespace(anyString(), anyBoolean(), anyInt(), anyLong());
}
use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.
the class FreeResourcesProvider method getResources.
@Override
public List<ProvidedResources> getResources(String accountId) throws ServerException, NotFoundException {
Map<String, ResourceImpl> freeResources = new HashMap<>();
String limitId = null;
try {
FreeResourcesLimit resourcesLimit = freeResourcesLimitManager.get(accountId);
for (Resource resource : resourcesLimit.getResources()) {
freeResources.put(resource.getType(), new ResourceImpl(resource));
}
limitId = resourcesLimit.getAccountId();
} catch (NotFoundException ignored) {
// there is no resources limit for given account
}
// add default resources which are not specified by limit
for (ResourceImpl resource : getDefaultResources(accountId)) {
freeResources.putIfAbsent(resource.getType(), resource);
}
if (!freeResources.isEmpty()) {
return singletonList(new ProvidedResourcesImpl(FREE_RESOURCES_PROVIDER, limitId, accountId, -1L, -1L, freeResources.values()));
} else {
return emptyList();
}
}
Aggregations