use of org.eclipse.che.multiuser.resource.model.Resource in project devspaces-images by redhat-developer.
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 devspaces-images by redhat-developer.
the class RuntimeResourceUsageTrackerTest method shouldReturnUsedRuntimesForGivenAccount.
@Test
public void shouldReturnUsedRuntimesForGivenAccount() throws Exception {
when(accountManager.getById(any())).thenReturn(account);
when(account.getName()).thenReturn("testAccount");
List<WorkspaceImpl> runtimes = Stream.of(WorkspaceStatus.values()).map(RuntimeResourceUsageTrackerTest::createWorkspace).collect(Collectors.toList());
when(workspaceManager.getByNamespace(anyString(), anyBoolean(), anyInt(), anyLong())).thenReturn(new Page<>(runtimes, 0, runtimes.size(), runtimes.size()));
Optional<Resource> usedRuntimesOpt = runtimeResourceUsageTracker.getUsedResource("account123");
assertTrue(usedRuntimesOpt.isPresent());
Resource usedRuntimes = usedRuntimesOpt.get();
assertEquals(usedRuntimes.getType(), RuntimeResourceType.ID);
assertEquals(usedRuntimes.getAmount(), // except stopped workspaces
WorkspaceStatus.values().length - 1);
assertEquals(usedRuntimes.getUnit(), RuntimeResourceType.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 devspaces-images by redhat-developer.
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()));
}
use of org.eclipse.che.multiuser.resource.model.Resource in project devspaces-images by redhat-developer.
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;
}
use of org.eclipse.che.multiuser.resource.model.Resource in project devspaces-images by redhat-developer.
the class RamResourceUsageTracker method getUsedResource.
@Override
public Optional<Resource> getUsedResource(String accountId) throws NotFoundException, ServerException {
final Account account = accountManager.getById(accountId);
List<WorkspaceImpl> activeWorkspaces = Pages.stream((maxItems, skipCount) -> workspaceManagerProvider.get().getByNamespace(account.getName(), true, maxItems, skipCount)).filter(ws -> STOPPED != ws.getStatus()).collect(Collectors.toList());
long currentlyUsedRamMB = 0;
for (WorkspaceImpl activeWorkspace : activeWorkspaces) {
if (WorkspaceStatus.STARTING.equals(activeWorkspace.getStatus())) {
// starting workspace may not have all machine in runtime
// it is need to calculate ram from environment config
WorkspaceConfigImpl config = activeWorkspace.getConfig();
if (config != null) {
final EnvironmentImpl startingEnvironment = config.getEnvironments().get(activeWorkspace.getRuntime().getActiveEnv());
if (startingEnvironment != null) {
currentlyUsedRamMB += environmentRamCalculator.calculate(startingEnvironment);
}
}
// Estimation of memory for starting workspace with Devfile is not implemented yet
// just ignore such
} else {
currentlyUsedRamMB += environmentRamCalculator.calculate(activeWorkspace.getRuntime());
}
}
if (currentlyUsedRamMB > 0) {
return Optional.of(new ResourceImpl(RamResourceType.ID, currentlyUsedRamMB, RamResourceType.UNIT));
} else {
return Optional.empty();
}
}
Aggregations