Skip to content

fastflowtransform.artifacts

Artifacts helpers (file-based + builders).

load_last_run_durations

load_last_run_durations(project_dir, *, artifacts_mode=None, artifacts_store=None, env_name=None, model_engine=None)

Best-effort reader for the last run_results.json.

Returns: { model_name: duration_in_seconds }. On any error or missing file: {}.

Source code in src/fastflowtransform/artifacts/files.py
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
def load_last_run_durations(
    project_dir: Path,
    *,
    artifacts_mode: str | None = None,
    artifacts_store: Any | None = None,
    env_name: str | None = None,
    model_engine: str | None = None,
) -> dict[str, float]:
    """
    Best-effort reader for the last run_results.json.

    Returns: { model_name: duration_in_seconds }.
    On any error or missing file: {}.
    """
    mode = (artifacts_mode or "files").strip().lower()
    raw: dict[str, Any] | None = None

    if mode == "db" and artifacts_store is not None:
        try:
            raw = artifacts_store.get_latest_artifact("run_results", env_name, model_engine)
        except Exception:
            return {}

    if raw is None:
        path = _target_dir(project_dir)
        if not path.exists():
            return {}
        try:
            raw = json.loads(path.read_text(encoding="utf-8"))
        except Exception:
            return {}

    # tolerate a few possible shapes
    items: list[dict[str, Any]] = (
        raw.get("results") or raw.get("node_results") or raw.get("nodes") or []
    )

    out: dict[str, float] = {}
    for item in items:
        if not isinstance(item, dict):
            continue
        name = item.get("name")
        dur_ms = item.get("duration_ms")
        if isinstance(name, str) and isinstance(dur_ms, (int, float)):
            out[name] = float(dur_ms) / 1000.0
    return out