mirror of
https://github.com/clearml/clearml-server
synced 2025-02-07 13:33:42 +00:00
![allegroai](/assets/img/avatar_default.png)
Support refresh flag in debug image samples Remove silent_dequeue_fail param to prevent status change in case task wasn't queued Add organizations.get_user_companies Fix reset should also reset active_duration Add api_version to server.info
46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
from pymongo.database import Database
|
|
|
|
|
|
def _add_active_duration(db: Database):
|
|
active_duration_key = "active_duration"
|
|
query = {"$or": [{active_duration_key: {"$eq": None}}, {active_duration_key: {"$eq": 0}}]}
|
|
collection = db["task"]
|
|
for doc in collection.find(
|
|
filter=query, projection=[active_duration_key, "status", "started", "completed"]
|
|
):
|
|
started = doc.get("started")
|
|
completed = doc.get("completed")
|
|
running = doc.get("status") == "running"
|
|
active_duration_value = doc.get(active_duration_key)
|
|
if active_duration_value == 0:
|
|
collection.update_one(
|
|
{"_id": doc["_id"]},
|
|
{"$set": {active_duration_key: None}},
|
|
)
|
|
elif started and active_duration_value is None:
|
|
collection.update_one(
|
|
{"_id": doc["_id"]},
|
|
{"$set": {active_duration_key: _get_active_duration(completed, running, started)}},
|
|
)
|
|
|
|
|
|
def _get_active_duration(
|
|
completed: datetime, running: bool, started: datetime
|
|
) -> Optional[float]:
|
|
if running:
|
|
return (datetime.utcnow() - started).total_seconds()
|
|
elif completed:
|
|
return (completed - started).total_seconds()
|
|
else:
|
|
return None
|
|
|
|
|
|
def migrate_backend(db: Database):
|
|
"""
|
|
Add active_duration field to tasks
|
|
"""
|
|
_add_active_duration(db)
|