CalculatedStatus { public static CalculatedStatus statusFromStageEntities(Collection<StageEntity> stages) { Collection<HostRoleStatus> stageStatuses = new HashSet<HostRoleStatus>(); Collection<HostRoleCommandEntity> tasks = new HashSet<HostRoleCommandEntity>(); for (StageEntity stage : stages) { Collection<HostRoleCommandEntity> stageTasks = stage.getHostRoleCommands(); HostRoleStatus stageStatus = calculateSummaryStatus(calculateTaskEntityStatusCounts(stageTasks), stageTasks.size(), stage.isSkippable()); stageStatuses.add(stageStatus); tasks.addAll(stageTasks); } HostRoleStatus status = calculateSummaryStatusOfUpgrade(calculateStatusCounts(stageStatuses), stageStatuses.size()); double progressPercent = calculateProgressPercent(calculateTaskEntityStatusCounts(tasks), tasks.size()); return new CalculatedStatus(status, progressPercent); } private  CalculatedStatus(HostRoleStatus status, double percent); private  CalculatedStatus(HostRoleStatus status, HostRoleStatus displayStatus, double percent); HostRoleStatus getStatus(); HostRoleStatus getDisplayStatus(); double getPercent(); static CalculatedStatus statusFromTaskEntities(Collection<HostRoleCommandEntity> tasks, boolean skippable); static CalculatedStatus statusFromStageEntities(Collection<StageEntity> stages); static CalculatedStatus statusFromStages(Collection<Stage> stages); static Map<HostRoleStatus, Integer> calculateStatusCounts(Collection<HostRoleStatus> hostRoleStatuses); static Map<HostRoleStatus, Integer> calculateStatusCountsForTasks(Collection<HostRoleCommand> hostRoleCommands); static Map<StatusType,Map<HostRoleStatus, Integer>> calculateStatusCountsForStage(Collection<TaskStatusListener.ActiveStage> stages); static Map<HostRoleStatus, Integer> calculateStatusCountsForTasks(Collection<HostRoleCommand> hostRoleCommands, StageEntityPK stage); static Map<HostRoleStatus, Integer> calculateTaskEntityStatusCounts(Collection<HostRoleCommandEntity> tasks); static Map<HostRoleStatus, Integer> calculateTaskStatusCounts(
      Map<Long, HostRoleCommandStatusSummaryDTO> stageDto, Set<Long> stageIds); static CalculatedStatus statusFromStageSummary(Map<Long, HostRoleCommandStatusSummaryDTO> stageDto,
      Set<Long> stageIds); static HostRoleStatus calculateSummaryStatus(Map<HostRoleStatus, Integer> counters,
      int total, boolean skippable); static HostRoleStatus calculateSummaryStatusFromPartialSet(Map<HostRoleStatus, Integer> counters,
                                                      boolean skippable); static HostRoleStatus calculateStageStatus(List <HostRoleCommand> hostRoleCommands, Map<HostRoleStatus, Integer> counters, Map<Role, Float> successFactors,
                                                    boolean skippable); static HostRoleStatus getOverallStatusForRequest(Collection<HostRoleStatus> hostRoleStatuses); static HostRoleStatus getOverallDisplayStatusForRequest(Collection<HostRoleStatus> hostRoleStatuses); static HostRoleStatus calculateSummaryDisplayStatus(
      Map<HostRoleStatus, Integer> counters, int total, boolean skippable); static final CalculatedStatus COMPLETED; static final CalculatedStatus PENDING; }
@Test public void testStatusFromStageEntities() throws Exception { Collection<StageEntity> stages = getStageEntities( getTaskEntities(HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED), getTaskEntities(HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED), getTaskEntities(HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED) ); CalculatedStatus status = CalculatedStatus.statusFromStageEntities(stages); assertEquals(HostRoleStatus.COMPLETED, status.getStatus()); assertEquals(100.0, status.getPercent(), 0.1); stages = getStageEntities( getTaskEntities(HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED), getTaskEntities(HostRoleStatus.COMPLETED, HostRoleStatus.IN_PROGRESS, HostRoleStatus.PENDING), getTaskEntities(HostRoleStatus.PENDING, HostRoleStatus.PENDING, HostRoleStatus.PENDING) ); status = CalculatedStatus.statusFromStageEntities(stages); assertEquals(HostRoleStatus.IN_PROGRESS, status.getStatus()); assertEquals(48.3, status.getPercent(), 0.1); stages = getStageEntities( getTaskEntities(HostRoleStatus.PENDING, HostRoleStatus.PENDING, HostRoleStatus.PENDING), getTaskEntities(HostRoleStatus.PENDING, HostRoleStatus.PENDING, HostRoleStatus.PENDING), getTaskEntities(HostRoleStatus.PENDING, HostRoleStatus.PENDING, HostRoleStatus.PENDING) ); status = CalculatedStatus.statusFromStageEntities(stages); assertEquals(HostRoleStatus.PENDING, status.getStatus()); assertEquals(0.0, status.getPercent(), 0.1); stages = getStageEntities( getTaskEntities(HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED), getTaskEntities(HostRoleStatus.COMPLETED, HostRoleStatus.FAILED, HostRoleStatus.PENDING), getTaskEntities(HostRoleStatus.PENDING, HostRoleStatus.PENDING, HostRoleStatus.PENDING) ); status = CalculatedStatus.statusFromStageEntities(stages); assertEquals(HostRoleStatus.FAILED, status.getStatus()); assertEquals(55.55, status.getPercent(), 0.1); stages = getStageEntities( getTaskEntities(HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED), getTaskEntities(HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED, HostRoleStatus.TIMEDOUT), getTaskEntities(HostRoleStatus.PENDING, HostRoleStatus.PENDING, HostRoleStatus.PENDING) ); status = CalculatedStatus.statusFromStageEntities(stages); assertEquals(HostRoleStatus.TIMEDOUT, status.getStatus()); assertEquals(66.66, status.getPercent(), 0.1); stages = getStageEntities( getTaskEntities(HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED), getTaskEntities(HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED, HostRoleStatus.HOLDING), getTaskEntities(HostRoleStatus.PENDING, HostRoleStatus.PENDING, HostRoleStatus.PENDING), getTaskEntities(HostRoleStatus.PENDING, HostRoleStatus.PENDING, HostRoleStatus.PENDING) ); status = CalculatedStatus.statusFromStageEntities(stages); assertEquals(HostRoleStatus.HOLDING, status.getStatus()); assertEquals(47.5, status.getPercent(), 0.1); stages = getStageEntities( getTaskEntities(HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED), getTaskEntities(HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED, HostRoleStatus.HOLDING), getTaskEntities(HostRoleStatus.PENDING, HostRoleStatus.PENDING, HostRoleStatus.PENDING), getTaskEntities(HostRoleStatus.PENDING, HostRoleStatus.PENDING, HostRoleStatus.PENDING) ); status = CalculatedStatus.statusFromStageEntities(stages); assertEquals(HostRoleStatus.HOLDING, status.getStatus()); assertEquals(47.5, status.getPercent(), 0.1); stages = getStageEntities( getTaskEntities(HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED), getTaskEntities(HostRoleStatus.COMPLETED, HostRoleStatus.COMPLETED, HostRoleStatus.HOLDING), getTaskEntities(HostRoleStatus.PENDING, HostRoleStatus.PENDING, HostRoleStatus.PENDING), getTaskEntities(HostRoleStatus.PENDING, HostRoleStatus.PENDING, HostRoleStatus.PENDING) ); status = CalculatedStatus.statusFromStageEntities(stages); assertEquals(HostRoleStatus.HOLDING, status.getStatus()); assertEquals(47.5, status.getPercent(), 0.1); }