@@ -136,15 +136,51 @@ def get_cvat_task_id(self, project_id, create):
136136 return task_id , task_name
137137
138138 def task_status (self ):
139+ """
140+ Fetches the status of a CVAT task based on the state of its jobs.
141+ Returns:
142+ - "completed" if all jobs are completed.
143+ - "in_progress" if at least one job is not completed.
144+ - None if an error occurs or the task does not exist.
145+ """
146+ # Get the project and task IDs
139147 project_id = self .get_cvat_project_id (create = False )
140148 if project_id is None :
141149 return None
150+
142151 task_id , _ = self .get_cvat_task_id (project_id , create = False )
143152 if task_id is None :
144153 return None
145154
146- r = requests .get (f"{ self .api_url } /api/tasks/{ task_id } " , auth = self .auth ).json ()
147- return r .get ("status" )
155+ # Fetch task details
156+ task_url = f"{ self .api_url } /api/tasks/{ task_id } "
157+ task_response = requests .get (task_url , auth = self .auth )
158+
159+ if task_response .status_code != 200 :
160+ return None # Task could not be fetched
161+
162+ task_data = task_response .json ()
163+
164+ # Get the jobs URL from the task details
165+ jobs_url = task_data .get ("jobs" , {}).get ("url" )
166+ if not jobs_url :
167+ return None # No jobs URL found for the task
168+
169+ # Fetch jobs for the task
170+ jobs_response = requests .get (jobs_url , auth = self .auth )
171+ if jobs_response .status_code != 200 :
172+ return None # Jobs could not be fetched
173+
174+ # Parse jobs and check their states
175+ jobs = jobs_response .json ().get ("results" , [])
176+ if not jobs :
177+ return None # No jobs found for the task
178+
179+ # Check if all jobs have state "completed"
180+ all_completed = all (job .get ("state" ) == "completed" for job in jobs )
181+
182+ # Return "completed" if all jobs are completed; otherwise "in_progress"
183+ return "completed" if all_completed else "in_progress"
148184
149185 def upload_to_cvat (self , samples ):
150186 project_id = self .get_cvat_project_id (create = True )
0 commit comments