from __future__ import annotations from typing import Any from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy.orm import Session from ...diff import compute_diff, filter_by_owner from ...snapshot import Snapshot from .. import models from ..db import get_db router = APIRouter(tags=["diff"]) @router.get("/diff") def get_diff( old: int = Query(..., description="older snapshot id"), new: int = Query(..., description="newer snapshot id"), owner: str | None = Query(None, description="restrict to one class, e.g. CMC_Animo"), db: Session = Depends(get_db), ) -> dict[str, Any]: a = db.get(models.Snapshot, old) b = db.get(models.Snapshot, new) if a is None or b is None: raise HTTPException(404, "snapshot not found") diff = compute_diff(Snapshot(a.data), Snapshot(b.data)) if owner: diff = filter_by_owner(diff, owner) return diff