File size: 1,546 Bytes
d9820a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Persist detection job progress for UI polling."""
from __future__ import annotations

import json
import logging

from ..database import SessionLocal
from .models import DetectionJob

logger = logging.getLogger(__name__)


def update_job_progress(job_id: int, pct: int, stage: str) -> None:
    """Update progress_pct and progress_stage in job params_json."""
    db = SessionLocal()
    try:
        job = db.query(DetectionJob).filter(DetectionJob.id == job_id).first()
        if not job or job.status not in ("queued", "running"):
            return
        params = {}
        try:
            params = json.loads(job.params_json or "{}")
        except json.JSONDecodeError:
            pass
        params["progress_pct"] = max(0, min(100, int(pct)))
        params["progress_stage"] = stage or ""
        job.params_json = json.dumps(params)
        db.commit()
    except Exception as exc:
        logger.warning("Could not update job %d progress: %s", job_id, exc)
        db.rollback()
    finally:
        db.close()


def get_job_progress(params: dict, status: str) -> tuple[int, str]:
    pct = params.get("progress_pct")
    stage = params.get("progress_stage") or ""
    if status == "completed":
        return 100, stage or "Complete"
    if status == "failed":
        return int(pct) if pct is not None else 0, stage or "Failed"
    if status == "queued":
        return int(pct) if pct is not None else 0, stage or "Queued"
    if pct is None:
        return 10, stage or "Running"
    return int(pct), stage or "Running"