o
    rh                     @   s|   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 dZdZdZd	ZG d
d deZdd Zdd ZdS )    N)Config)abort)ApiException)sleepzsimcore/services/comp/opencorz0simcore/services/comp/rabbit-ss-0d-cardiac-modelz/simcore/services/comp/human-gb-0d-cardiac-modelz*simcore/services/comp/kember-cardiac-modelc                   @   s   e Zd ZdS )SimulationExceptionN)__name__
__module____qualname__ r
   r
   </home/cmiss/Jenkins/workspace/SPARC-API/app/osparc/osparc.pyr      s    r   c              
   C   s  | d d }|t krd| vrtddd nd| v r,|tkr+|tkr+|tkr+tddd ntdd	d zttjtj	tj
tjd
}d| d v }|t krtjdd}t| d d | |d zt|}||j}W n ty } ztd| dd }~ww |  n>|rtjdd}|| d d d  |d zt|}||j}	W n ty } ztd| dd }~ww |  t|}
z|
|| d d }W n ty } ztd| dd }~ww |t kr| d d |d}n|r|	| d d | d d d < | d d }|
|j|jt|}|
|j|j|j}|jdvr1tdd|j|j|jddd}W |S  tyg } zdt |j!dkrX|j!d nd d!}W Y d }~|S d }~ww )"NsolvernameZopencori  zMissing OpenCOR settings)descriptionosparczUnknown oSPARC solverzMissing oSPARC settingshostusernamepasswordinputzw+)modeZjson_configr   z9the simulation configuration file could not be uploaded ()valuez-the solver input file could not be uploaded (versionz-the requested solver could not be retrieved (	model_url)r   config_file
job_inputs>   Z	PUBLISHEDPENDINGz)the simulation job could not be submittedok)r   r   )job_idr   )statusdatanokunknownr   r   )"OPENCOR_SOLVERr   DATASET_4_SOLVERDATASET_17_SOLVERDATASET_78_SOLVERr   	ApiClientConfigurationr   OSPARC_API_URLOSPARC_API_KEYOSPARC_API_SECRETtempfileNamedTemporaryFilejsondumpseekFilesApiupload_filer   r   r   closewrite
SolversApiZget_solver_releaseZ
create_jobidr   Z	JobInputsZ	start_jobstatelenargs)r    solver_name
api_clientZhas_solver_inputZtemp_config_file	files_apir   eZtemp_input_file
input_filesolvers_apir   r   jobr   resr
   r
   r   start_simulation   s   	









rC   c              
   C   s  zt t jtjtjtjd}t |}| d }| d d }| d d }||||}|j	dkr4t
d|j	dkrz	||||}W n tyV } zt
d	| d
d }~ww zt |}	|	|jt|j d  j}
W n ty } zt
d| d
d }~ww t|
d}ddi}|tkrt||d< n| |d< |  W |S ddi}W |S  t
y } zdt|jdkr|jd ndd}W Y d }~|S d }~ww )Nr   r   r   r   r   FAILEDzthe simulation failedSUCCESSz3the simulation job outputs could not be retrieved (r   r   z/the simulation results could not be retrieved (rr   r   resultsr!   r"   r#   )r   r(   r)   r   r*   r+   r,   r6   Zinspect_jobr8   r   Zget_job_outputsr   r2   Zdownload_filerG   listkeysr7   openr$   r/   loadreadr4   r9   r:   )r    r<   r@   r   r;   Zsolver_versionr   outputsr>   r=   Zresults_filenameZresults_filerB   r
   r
   r   check_simulation   sj   







	rN   )r/   r   r-   
app.configr   flaskr   Zosparc.restr   timer   r$   r%   r&   r'   	Exceptionr   rC   rN   r
   r
   r
   r   <module>   s    w