2020from uuid import uuid4
2121
2222from aiohttp import web
23- from common_library .json_serialization import json_dumps , json_loads
23+ from common_library .json_serialization import json_dumps
2424from models_library .api_schemas_clusters_keeper .ec2_instances import EC2InstanceTypeGet
2525from models_library .api_schemas_directorv2 .dynamic_services import (
2626 DynamicServiceGet ,
@@ -1811,16 +1811,24 @@ async def add_project_states_for_user(
18111811 f"{ user_id = } " ,
18121812 f"{ project ['uuid' ]= } " ,
18131813 )
1814- # for templates: the project is never locked and never opened. also the running state is always unknown
1815- share_state = await _get_project_share_state (user_id , project ["uuid" ], app )
1816- project_running_state = RunningState .UNKNOWN
1817- computational_node_states : dict [NodeID , NodeState ] = {}
1818- if computation_task := await director_v2_service .get_computation_task (
1819- app , user_id , project ["uuid" ]
1820- ):
1821- project_running_state = computation_task .state
1822- computational_node_states = computation_task .pipeline_details .node_states
1814+ project_share_state , user_computation_task = await asyncio .gather (
1815+ _get_project_share_state (user_id , project ["uuid" ], app ),
1816+ director_v2_service .get_computation_task (app , user_id , project ["uuid" ]),
1817+ )
1818+ # retrieve the project computational state
1819+ # if the user has no computation task, we assume the project is not running
1820+ project_running_state = (
1821+ user_computation_task .state
1822+ if user_computation_task
1823+ else RunningState .NOT_STARTED
1824+ )
1825+ computational_node_states = (
1826+ user_computation_task .pipeline_details .node_states
1827+ if user_computation_task
1828+ else {}
1829+ )
18231830
1831+ # compose the node states
18241832 for node_uuid , node in project ["workbench" ].items ():
18251833 assert isinstance (node_uuid , str ) # nosec
18261834 assert isinstance (node , dict ) # nosec
@@ -1831,22 +1839,33 @@ async def add_project_states_for_user(
18311839 project_uuid = project ["uuid" ],
18321840 node_id = NodeID (node_uuid ),
18331841 )
1842+ if NodeID (node_uuid ) in computational_node_states :
1843+ node_state = computational_node_states [NodeID (node_uuid )].model_copy (
1844+ update = {"lock_state" : node_lock_state }
1845+ )
1846+ else :
1847+ # if the node is not in the computational state, we create a new one
1848+ service_is_running = node_lock_state .status is NodeShareStatus .OPENED
1849+ node_state = NodeState (
1850+ current_status = (
1851+ RunningState .STARTED
1852+ if service_is_running
1853+ else RunningState .NOT_STARTED
1854+ ),
1855+ lock_state = node_lock_state ,
1856+ )
18341857
1835- # create complete node state
1836- node_state = computational_node_states .get (
1837- NodeID (node_uuid ), NodeState (current_status = RunningState .UNKNOWN )
1838- )
1839- node_state .lock_state = node_lock_state
1840- node_state_dict = json_loads (
1858+ # upgrade the project
1859+ node .setdefault ("state" , {}).update (
18411860 node_state .model_dump_json (by_alias = True , exclude_unset = True )
18421861 )
1843- node .setdefault ("state" , {}).update (node_state_dict )
18441862 if "progress" in node ["state" ] and node ["state" ]["progress" ] is not None :
18451863 # ensure progress is a percentage
18461864 node ["progress" ] = round (node ["state" ]["progress" ] * 100.0 )
18471865
18481866 project ["state" ] = ProjectState (
1849- share_state = share_state , state = ProjectRunningState (value = project_running_state )
1867+ share_state = project_share_state ,
1868+ state = ProjectRunningState (value = project_running_state ),
18501869 ).model_dump (by_alias = True , exclude_unset = True )
18511870 return project
18521871
0 commit comments