"""Repository for the `batch_runs` table.""" from __future__ import annotations from typing import Any from db.connection import get_db class BatchRunsRepository: async def insert( self, batch_id: str, config_id: int | None, config_name: str, workers: int, total_runs: int, stagger_ms: int, headless: bool, ) -> None: async with get_db() as db: await db.execute( """INSERT INTO batch_runs (batch_id, config_id, config_name, workers, total_runs, stagger_ms, headless) VALUES (?, ?, ?, ?, ?, ?, ?)""", (batch_id, config_id, config_name, workers, total_runs, stagger_ms, int(headless)), ) await db.commit() async def finish( self, batch_id: str, succeeded: int, failed: int, stopped: bool, error: str | None, ) -> None: async with get_db() as db: await db.execute( """UPDATE batch_runs SET finished_at = strftime('%Y-%m-%dT%H:%M:%SZ', 'now'), succeeded = ?, failed = ?, stopped = ?, error = ? WHERE batch_id = ?""", (succeeded, failed, int(stopped), error, batch_id), ) await db.commit() async def list(self, limit: int = 100) -> list[dict[str, Any]]: async with get_db() as db: rows = await (await db.execute( "SELECT * FROM batch_runs ORDER BY id DESC LIMIT ?", (limit,) )).fetchall() return [dict(r) for r in rows] batch_runs_repo = BatchRunsRepository()