use of cz.metacentrum.perun.controller.model.FacilityState.FacilityPropagationState in project perun by CESNET.
the class PropagationStatsReaderImpl method getFacilityState.
@Override
public FacilityState getFacilityState(PerunSession session, Facility facility) throws PrivilegeException, FacilityNotExistsException, InternalErrorException {
// get all tasks
List<Task> tasks = taskDao.listAllTasksForFacility(facility.getId());
// define state
FacilityState state = new FacilityState();
state.setFacility(facility);
// if no tasks we can't determine facility state
if (tasks.isEmpty() || tasks == null) {
state.setState(FacilityPropagationState.NOT_DETERMINED);
return state;
} else {
// OK if no change
state.setState(FacilityPropagationState.OK);
}
// fill all available destinations
List<RichDestination> destinations = perun.getServicesManager().getAllRichDestinations(session, facility);
for (RichDestination rd : destinations) {
state.getResults().put(rd.getDestination(), FacilityPropagationState.NOT_DETERMINED);
}
// magic with tasks :-)
for (Task task : tasks) {
// save previous facility state
FacilityPropagationState facState = state.getState();
// PROCESSING and not ERROR before
if (TaskStatus.PROCESSING.equals(task.getStatus()) && (facState != FacilityPropagationState.ERROR)) {
state.setState(FacilityPropagationState.PROCESSING);
} else // ERROR - set ERROR
if (TaskStatus.ERROR.equals(task.getStatus())) {
state.setState(FacilityPropagationState.ERROR);
}
// get destination status
if (task.getExecService().getExecServiceType().equals(ExecService.ExecServiceType.SEND)) {
List<TaskResult> results = taskResultDao.getTaskResultsByTask(task.getId());
Map<Service, Map<Destination, TaskResult>> latestResults = new HashMap<Service, Map<Destination, TaskResult>>();
for (TaskResult res : results) {
if (latestResults.get(res.getService()) == null) {
// put in map since result for service exists
Map<Destination, TaskResult> value = new HashMap<>();
value.put(res.getDestination(), res);
latestResults.put(res.getService(), value);
} else if (latestResults.get(res.getService()) != null && latestResults.get(res.getService()).get(res.getDestination()) == null) {
// put in inner map, since destination for service not yet exists
latestResults.get(res.getService()).put(res.getDestination(), res);
} else {
// update in inner map since this is later task result
if (latestResults.get(res.getService()).get(res.getDestination()).getId() < res.getId()) {
// put in map
latestResults.get(res.getService()).put(res.getDestination(), res);
}
}
}
for (Map<Destination, TaskResult> res : latestResults.values()) {
for (TaskResult result : res.values()) {
// iterate over all latest tasks results
String destination = result.getDestination().getDestination();
FacilityPropagationState propState = state.getResults().get(destination);
// if any error => state is error
if (TaskResult.TaskResultStatus.ERROR.equals(result.getStatus())) {
state.getResults().put(destination, FacilityPropagationState.ERROR);
continue;
}
// if result ok and previous was not bad
if (TaskResult.TaskResultStatus.DONE.equals(result.getStatus())) {
if (FacilityPropagationState.NOT_DETERMINED.equals(propState)) {
state.getResults().put(destination, FacilityPropagationState.OK);
}
}
}
}
}
}
return state;
}
Aggregations