from uuid import UUID from psycopg import AsyncConnection class AnalysisRepository: @staticmethod async def list_runs(conn: AsyncConnection) -> list[dict]: async with conn.cursor() as cur: await cur.execute( """ SELECT run_id, name, run_type, created_by, created_at, started_at, status, parameters FROM analysis.runs ORDER BY created_at DESC, run_id """ ) return await cur.fetchall() @staticmethod async def get_run(conn: AsyncConnection, run_id: UUID) -> dict | None: async with conn.cursor() as cur: await cur.execute( """ SELECT run_id, name, run_type, created_by, created_at, started_at, status, parameters FROM analysis.runs WHERE run_id = %s """, (run_id,), ) return await cur.fetchone() @staticmethod async def list_results( conn: AsyncConnection, run_id: UUID, result_type: str | None = None ) -> list[dict]: query = """ SELECT result_id, run_id, result_type, node_id, link_id, payload, created_at FROM analysis.results WHERE run_id = %s """ params: tuple[UUID] | tuple[UUID, str] = (run_id,) if result_type is not None: query += " AND result_type = %s" params = (run_id, result_type) query += " ORDER BY created_at, result_id" async with conn.cursor() as cur: await cur.execute(query, params) return await cur.fetchall()