Add structured job progress tracking

This commit is contained in:
2026-06-30 14:18:02 +08:00
parent 93af8bcd3a
commit 442b521705
8 changed files with 454 additions and 9 deletions

View File

@@ -21,6 +21,7 @@ from .modules.weights.service import load_manifest, sync_weights, verify_weights
from .agents.evaluation_agent import evaluate_project
from .agents.validation_agent import validate_project
from .paths import ensure_inside
from .progress import progress_from_log_path
from .schemas import DatasetCreate, DatasetYoloYamlRequest, JobCreate, ProfileCreate, WeightSyncRequest
app = FastAPI(title="Seg Data Server Net", version="0.1.0")
@@ -34,6 +35,15 @@ app.add_middleware(
)
def _job_with_progress(job: dict, include_log_tail: bool = False) -> dict:
enriched = dict(job)
max_bytes = 65536 if include_log_tail else 32768
enriched["progress"] = progress_from_log_path(enriched["log_path"], enriched["status"], max_bytes=max_bytes)
if include_log_tail:
enriched["log_tail"] = db.log_tail(enriched["log_path"], max_bytes=max_bytes)
return enriched
@app.on_event("startup")
def startup() -> None:
db.init_db()
@@ -135,14 +145,14 @@ def api_save_profile(profile: ProfileCreate) -> dict:
@app.post("/api/jobs")
def api_create_job(request: JobCreate) -> dict:
try:
return create_job(request)
return _job_with_progress(create_job(request))
except Exception as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
@app.get("/api/jobs")
def api_jobs(limit: int = 100) -> list[dict]:
return db.list_jobs(limit)
return [_job_with_progress(job) for job in db.list_jobs(limit)]
@app.get("/api/jobs/{job_id}")
@@ -150,8 +160,7 @@ def api_job(job_id: str) -> dict:
job = db.get_job(job_id)
if not job:
raise HTTPException(status_code=404, detail="job not found")
job["log_tail"] = db.log_tail(job["log_path"])
return job
return _job_with_progress(job, include_log_tail=True)
@app.post("/api/jobs/{job_id}/cancel")
@@ -159,7 +168,7 @@ def api_cancel_job(job_id: str) -> dict:
job = cancel_job(job_id)
if not job:
raise HTTPException(status_code=404, detail="job not found")
return job
return _job_with_progress(job)
@app.get("/api/jobs/{job_id}/events")
@@ -171,6 +180,7 @@ async def api_job_events(job_id: str):
if not job:
yield "event: error\ndata: job not found\n\n"
return
job = _job_with_progress(job)
path = Path(job["log_path"])
chunk = ""
if path.exists():