Skip to content

Commit ef7b79e

Browse files
committed
Add endpoint to fetch sensitivity analysis results
1 parent e44dbed commit ef7b79e

1 file changed

Lines changed: 60 additions & 4 deletions

File tree

fastapi_app/webapp.py

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,7 @@ async def check_task(task_id: str) -> JSONResponse:
202202
"status": res.state,
203203
"results": None,
204204
}
205-
if res.state == states.PENDING:
206-
task["status"] = res.state
207-
else:
205+
if res.state != states.PENDING:
208206
task["status"] = "DONE"
209207
results_as_dict = json.loads(res.result)
210208
server_info = results_as_dict.pop("SERVER")
@@ -218,6 +216,65 @@ async def check_task(task_id: str) -> JSONResponse:
218216
return JSONResponse(content=jsonable_encoder(task))
219217

220218

219+
@app.get("/check-sensitivity-analysis/{task_id}")
220+
async def check_sensitivity_analysis(task_id: str) -> JSONResponse:
221+
sensitivity_analysis = celery_app.AsyncResult(task_id)
222+
223+
task = {
224+
"server_info": None,
225+
"mvs_version": None,
226+
"id": task_id,
227+
"status": sensitivity_analysis.state,
228+
"results": dict(reference_simulation_id=None, sensitivity_analysis_steps=None),
229+
}
230+
if sensitivity_analysis.state != states.PENDING:
231+
sa_results = sensitivity_analysis.result
232+
if "ERROR" in sa_results:
233+
task["status"] = "ERROR"
234+
task["results"] = sa_results
235+
else:
236+
# fetch results of each sensitivity analysis steps
237+
sa_step_ids = sa_results["sensitivity_analysis_ids"]
238+
239+
if "ERROR" in sa_step_ids:
240+
task["status"] = "ERROR"
241+
task["results"]["sensitivity_analysis_ids"] = sa_step_ids
242+
answers = None
243+
else:
244+
server_info = None
245+
answers = []
246+
for sa_step_id in sa_step_ids:
247+
sa_step = celery_app.AsyncResult(sa_step_id)
248+
if sa_step.ready():
249+
results_as_dict = json.loads(sa_step.result)
250+
server_info = results_as_dict.pop("SERVER")
251+
if "ERROR" in results_as_dict:
252+
253+
temp = copy.deepcopy(results_as_dict)
254+
temp.pop("step_idx")
255+
results_as_dict["output_values"] = temp
256+
answers.append(results_as_dict)
257+
else:
258+
task["status"] = states.PENDING
259+
answers = None
260+
break
261+
262+
if answers is not None:
263+
264+
task["status"] = "DONE"
265+
task["server_info"] = server_info
266+
task["mvs_version"] = MVS_SERVER_VERSIONS.get(server_info, "unknown")
267+
task["results"]["sensitivity_analysis_steps"] = [
268+
d["output_values"]
269+
for d in sorted(answers, key=lambda item: item["step_idx"])
270+
]
271+
# the "result" of the main simulation here is the mvs token leading to the simulations results
272+
# that can be checked with url /check/{task_id}
273+
task["results"]["reference_simulation_id"] = sa_results["ref_sim_id"]
274+
275+
return JSONResponse(content=jsonable_encoder(task))
276+
277+
221278
@app.get("/get_lp_file/{task_id}")
222279
async def get_lp_file(task_id: str) -> Response:
223280
res = celery_app.AsyncResult(task_id)
@@ -257,4 +314,3 @@ async def get_lp_file(task_id: str) -> Response:
257314
response = "There is no LP file output, did you check the LP file option when you started your simulation?"
258315

259316
return response
260-

0 commit comments

Comments
 (0)