class RamResourceUsageTracker implements ResourceUsageTracker {

  @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();
    }
  }

  @Inject public  RamResourceUsageTracker(
      Provider<WorkspaceManager> workspaceManagerProvider,
      AccountManager accountManager,
      EnvironmentRamCalculator environmentRamCalculator);

}