Skip to content

Commit 9b0105a

Browse files
committed
Add group status update logic after experiment completion
1 parent 6ff015c commit 9b0105a

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

client/api/endpoints.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,50 @@ async def get_experiment_stats_optimized(experiment_id: str, db) -> Dict:
316316
}
317317

318318

319+
async def check_and_update_group_status(experiment_id: str):
320+
"""
321+
Checks if all experiments in the group containing the given experiment_id
322+
are completed or failed. If so, updates the group status to COMPLETED.
323+
"""
324+
try:
325+
db = await get_database()
326+
327+
# Find groups that contain this experiment
328+
cursor = db.groups.find({"experiment_ids": experiment_id})
329+
330+
async for group in cursor:
331+
group_id = group["_id"]
332+
experiment_ids = group.get("experiment_ids", [])
333+
334+
if not experiment_ids:
335+
continue
336+
337+
# Convert string IDs to ObjectIds
338+
exp_object_ids = []
339+
for eid in experiment_ids:
340+
if ObjectId.is_valid(eid):
341+
exp_object_ids.append(ObjectId(eid))
342+
343+
if not exp_object_ids:
344+
continue
345+
346+
# Count experiments that are NOT completed or failed
347+
unfinished_count = await db.experiments.count_documents({
348+
"_id": {"$in": exp_object_ids},
349+
"state": {"$nin": [ExperimentState.COMPLETED, ExperimentState.FAILED]}
350+
})
351+
352+
if unfinished_count == 0:
353+
logger.info(f"All experiments in group {group_id} are finished. Marking group as COMPLETED.")
354+
await db.groups.update_one(
355+
{"_id": group_id},
356+
{"$set": {"state": ExperimentState.COMPLETED}}
357+
)
358+
359+
except Exception as e:
360+
logger.error(f"Error checking group status for experiment {experiment_id}: {str(e)}")
361+
362+
319363
async def run_experiment(experiment_id: str):
320364
"""Фоновая задача для выполнения эксперимента с динамической нагрузкой."""
321365
if experiment_id in active_experiments:
@@ -511,6 +555,8 @@ async def run_experiment(experiment_id: str):
511555
logger.error(f"Failed to update experiment state: {str(update_error)}")
512556
finally:
513557
active_experiments.remove(experiment_id)
558+
# Check and update group status
559+
await check_and_update_group_status(experiment_id)
514560

515561

516562
@router.get("/experiment_stats")

0 commit comments

Comments
 (0)