ReservationUsageCalculator { public Map<String, ReservationUsage> buildUsage() { Map<String, ResourceAccumulator> accumulatorMap = new HashMap<>(); List<Pair<Job, List<Task>>> jobsAndTasks = jobOperations.getJobsAndTasks(); Set<String> capacityGroupNames = capacityManagementService.getApplicationSLAs().stream() .map(ApplicationSLA::getAppName) .collect(Collectors.toSet()); for (Pair<Job, List<Task>> jobAndTasks : jobsAndTasks) { Job job = jobAndTasks.getLeft(); String capacityGroup = capacityGroupNames.contains(job.getJobDescriptor().getCapacityGroup()) ? job.getJobDescriptor().getCapacityGroup() : ApplicationSlaManagementService.DEFAULT_APPLICATION; ResourceAccumulator accumulator = accumulatorMap.computeIfAbsent( capacityGroup, cp -> new ResourceAccumulator() ); processJob(accumulator, jobAndTasks); } Map<String, ReservationUsage> result = new HashMap<>(); accumulatorMap.forEach((capacityGroup, accumulator) -> result.put(capacityGroup, accumulator.toReservationUsage())); capacityManagementService.getApplicationSLAs().forEach(capacityGroup -> { if (!result.containsKey(capacityGroup.getAppName())) { result.put(capacityGroup.getAppName(), ReservationUsage.none()); } }); return result; } @Inject  ReservationUsageCalculator(ReadOnlyJobOperations jobOperations,
                                      ApplicationSlaManagementService capacityManagementService); Map<String, ReservationUsage> buildUsage(); ReservationUsage buildCapacityGroupUsage(String capacityGroupName);  }
@Test public void testBuildUsage() { Map<String, ReservationUsage> usage = calculator.buildUsage(); ReservationUsage cg1Usage = usage.get("cg1"); ReservationUsage cg2Usage = usage.get("cg2"); assertThat(cg1Usage).isNotNull(); assertThat(cg2Usage).isNotNull(); assertThat(cg1Usage.getCpu()).isEqualTo(CONTAINER_RESOURCES.getCpu() * 4); assertThat(cg1Usage.getCpu() * 2).isEqualTo(cg2Usage.getCpu()); assertThat(cg1Usage.getMemoryMB()).isEqualTo(CONTAINER_RESOURCES.getMemoryMB() * 4); assertThat(cg1Usage.getMemoryMB() * 2).isEqualTo(cg2Usage.getMemoryMB()); assertThat(cg1Usage.getDiskMB()).isEqualTo(CONTAINER_RESOURCES.getDiskMB() * 4); assertThat(cg1Usage.getDiskMB() * 2).isEqualTo(cg2Usage.getDiskMB()); assertThat(cg1Usage.getNetworkMbs()).isEqualTo(CONTAINER_RESOURCES.getNetworkMbps() * 4); assertThat(cg1Usage.getNetworkMbs() * 2).isEqualTo(cg2Usage.getNetworkMbs()); }