use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.
the class RamResourceUsageTrackerTest method shouldNotSumRamOfStoppedWorkspaceWhenGettingUsedRamForGivenAccount.
@Test
public void shouldNotSumRamOfStoppedWorkspaceWhenGettingUsedRamForGivenAccount() throws Exception {
final WorkspaceImpl stoppedWs = createWorkspace(WorkspaceStatus.STOPPED, 3500);
final WorkspaceImpl runningWs = createWorkspace(WorkspaceStatus.RUNNING, 2500);
mockWorkspaces(stoppedWs, runningWs);
when(envRamCalculator.calculate(runningWs.getRuntime())).thenReturn(2500L);
final Optional<Resource> usedRamOpt = ramUsageTracker.getUsedResource(ACCOUNT_ID);
assertTrue(usedRamOpt.isPresent());
final Resource usedRam = usedRamOpt.get();
assertEquals(usedRam.getType(), RamResourceType.ID);
assertEquals(usedRam.getAmount(), 2500L);
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 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());
}
use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.
the class ResourceManagerTest method testGetsAccountTotalResources.
@Test
public void testGetsAccountTotalResources() throws Exception {
final List<Resource> res = ImmutableList.of(new ResourceImpl("RAM", 2048, "mb"), new ResourceImpl("timeout", 15, "minutes"));
doReturn(res).when(providedResources).getResources();
List<? extends Resource> actual = resourceManager.getTotalResources(ACCOUNT_ID);
assertEquals(actual.size(), res.size());
assertTrue(actual.containsAll(res));
}
use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.
the class ResourceAggregatorTest method shouldTestResourcesAggregationByTypes.
@Test
public void shouldTestResourcesAggregationByTypes() throws Exception {
// given
final ResourceImpl aResource = new ResourceImpl(A_RESOURCE_TYPE, 123, "unit");
final ResourceImpl bResource = new ResourceImpl(B_RESOURCE_TYPE, 123, "unit");
final ResourceImpl anotherBResource = new ResourceImpl(B_RESOURCE_TYPE, 321, "unit");
final ResourceImpl aggregatedBResources = new ResourceImpl(B_RESOURCE_TYPE, 444, "unit");
when(bResourceType.aggregate(any(), any())).thenReturn(aggregatedBResources);
// when
final Map<String, Resource> aggregatedResources = resourceAggregator.aggregateByType(asList(aResource, bResource, anotherBResource));
// then
verify(bResourceType).aggregate(eq(bResource), eq(anotherBResource));
verify(aResourceType, never()).aggregate(any(), any());
assertEquals(aggregatedResources.size(), 2);
assertTrue(aggregatedResources.containsKey(A_RESOURCE_TYPE));
assertTrue(aggregatedResources.containsValue(aResource));
assertTrue(aggregatedResources.containsKey(B_RESOURCE_TYPE));
assertTrue(aggregatedResources.containsValue(aggregatedBResources));
}
use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.
the class ResourceAggregator method excess.
/**
* Returns list which contains resources from specified {@code sourceResources} which have
* excessive amount in compare to specified {@code resourcesToCompare}.
*
* <p>Example :
*
* <pre>
* | \ | Source | To compare| Result |
* |:------------|:----------|:----------|:---------|
* | Resource1 | 5 | 3 | 2 |
* | ----------- | --------- | --------- | -------- |
* | Resource2 | - | 9 | - |
* | ----------- | --------- | --------- | -------- |
* | Resource3 | 1 | - | 1 |
* | ----------- | --------- | --------- | -------- |
* </pre>
*
* @param sourceResources the source resources
* @param resourcesToCompare the resources which should be compared to {@code sourceResources}
* @throws IllegalArgumentException when {@code sourceResources} or {@code resourcesToCompare}
* contain resource with not supported type
*/
public List<? extends Resource> excess(List<? extends Resource> sourceResources, List<? extends Resource> resourcesToCompare) {
checkSupporting(sourceResources);
checkSupporting(resourcesToCompare);
final Map<String, Resource> result = sourceResources.stream().collect(Collectors.toMap(Resource::getType, Function.identity()));
for (Resource toCompare : resourcesToCompare) {
String resourceType = toCompare.getType();
final Resource sourceResource = result.get(resourceType);
if (sourceResource != null) {
if (sourceResource.getAmount() == toCompare.getAmount()) {
// source resource doesn't have excessive amount
result.remove(resourceType);
continue;
}
try {
Resource excess = deduct(sourceResource, toCompare);
if (excess.getAmount() == 0) {
// source resource doesn't have excessive amount
result.remove(resourceType);
} else {
result.put(resourceType, excess);
}
} catch (NoEnoughResourcesException e) {
// source resource doesn't have excessive amount
result.remove(resourceType);
}
}
}
return new ArrayList<>(result.values());
}
Aggregations