use of bio.terra.stairway.Flight in project terra-workspace-manager by DataBiosphere.
the class JobService method mapFlightStateToApiJobReport.
public ApiJobReport mapFlightStateToApiJobReport(FlightState flightState) {
FlightMap inputParameters = flightState.getInputParameters();
String description = inputParameters.get(JobMapKeys.DESCRIPTION.getKeyName(), String.class);
FlightStatus flightStatus = flightState.getFlightStatus();
String submittedDate = flightState.getSubmitted().toString();
ApiJobReport.StatusEnum jobStatus = getJobStatus(flightStatus);
String completedDate = null;
HttpStatus statusCode = HttpStatus.ACCEPTED;
if (jobStatus != StatusEnum.RUNNING) {
// If the job is completed, the JobReport should include a result code indicating success or
// failure. For failed jobs, this code is the error code. For successful jobs, this is the
// code specified by the flight if present, or a default of 200 if not.
completedDate = flightState.getCompleted().get().toString();
switch(jobStatus) {
case FAILED:
int errorCode = flightState.getException().map(e -> ErrorReportUtils.buildApiErrorReport(e).getStatusCode()).orElseThrow(() -> new InvalidResultStateException("Flight failed with no exception reported"));
statusCode = HttpStatus.valueOf(errorCode);
break;
case SUCCEEDED:
FlightMap resultMap = getResultMap(flightState);
statusCode = resultMap.get(JobMapKeys.STATUS_CODE.getKeyName(), HttpStatus.class);
if (statusCode == null) {
statusCode = HttpStatus.OK;
}
break;
default:
throw new IllegalStateException("Cannot get status code of flight in unknown state " + jobStatus);
}
}
ApiJobReport jobReport = new ApiJobReport().id(flightState.getFlightId()).description(description).status(jobStatus).statusCode(statusCode.value()).submitted(submittedDate).completed(completedDate).resultURL(resultUrlFromFlightState(flightState));
return jobReport;
}
Aggregations