updates :)
This commit is contained in:
@@ -54,6 +54,23 @@ CREATE TABLE IF NOT EXISTS flow_configs (
|
||||
target_item_ids_json TEXT NOT NULL DEFAULT '[]',
|
||||
target_url TEXT NOT NULL DEFAULT ''
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS batch_runs (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
batch_id TEXT NOT NULL UNIQUE,
|
||||
config_id INTEGER,
|
||||
config_name TEXT NOT NULL DEFAULT '',
|
||||
workers INTEGER NOT NULL DEFAULT 1,
|
||||
total_runs INTEGER NOT NULL DEFAULT 0,
|
||||
stagger_ms INTEGER NOT NULL DEFAULT 0,
|
||||
headless INTEGER NOT NULL DEFAULT 1,
|
||||
started_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
|
||||
finished_at TEXT,
|
||||
succeeded INTEGER NOT NULL DEFAULT 0,
|
||||
failed INTEGER NOT NULL DEFAULT 0,
|
||||
stopped INTEGER NOT NULL DEFAULT 0,
|
||||
error TEXT
|
||||
);
|
||||
"""
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
from db.repositories.batch_runs import BatchRunsRepository, batch_runs_repo
|
||||
from db.repositories.flow_configs import FlowConfigRepository, flow_config_repo
|
||||
from db.repositories.runs import RunsRepository, runs_repo
|
||||
|
||||
__all__ = [
|
||||
"RunsRepository", "runs_repo",
|
||||
"FlowConfigRepository", "flow_config_repo",
|
||||
"BatchRunsRepository", "batch_runs_repo",
|
||||
]
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
"""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()
|
||||
@@ -9,7 +9,7 @@ from db.connection import get_db
|
||||
FlowConfigRow = dict[str, Any]
|
||||
|
||||
_FIELDS = [
|
||||
"name", "is_active",
|
||||
"name",
|
||||
"search_texts_json", "search_box_selector", "search_box_selector_type",
|
||||
"search_submit_selector", "search_submit_selector_type",
|
||||
"search_results_selector", "search_results_selector_type",
|
||||
@@ -54,18 +54,16 @@ class FlowConfigRepository:
|
||||
)).fetchone()
|
||||
return _deserialise(dict(row)) if row else None
|
||||
|
||||
async def get_active(self) -> FlowConfigRow | None:
|
||||
async def get_first(self) -> FlowConfigRow | None:
|
||||
async with get_db() as db:
|
||||
row = await (await db.execute(
|
||||
"SELECT * FROM flow_configs WHERE is_active=1 ORDER BY id DESC LIMIT 1"
|
||||
"SELECT * FROM flow_configs ORDER BY id ASC LIMIT 1"
|
||||
)).fetchone()
|
||||
return _deserialise(dict(row)) if row else None
|
||||
|
||||
async def create(self, data: dict[str, Any]) -> int:
|
||||
data = _serialise(data)
|
||||
async with get_db() as db:
|
||||
if data.get("is_active"):
|
||||
await db.execute("UPDATE flow_configs SET is_active=0")
|
||||
placeholders = ", ".join("?" for _ in _FIELDS)
|
||||
cols = ", ".join(_FIELDS)
|
||||
values = [data.get(f) for f in _FIELDS]
|
||||
@@ -78,8 +76,6 @@ class FlowConfigRepository:
|
||||
async def update(self, cfg_id: int, data: dict[str, Any]) -> None:
|
||||
data = _serialise(data)
|
||||
async with get_db() as db:
|
||||
if data.get("is_active"):
|
||||
await db.execute("UPDATE flow_configs SET is_active=0")
|
||||
set_clause = ", ".join(f"{f}=?" for f in _FIELDS)
|
||||
values = [data.get(f) for f in _FIELDS] + [cfg_id]
|
||||
await db.execute(
|
||||
@@ -87,12 +83,6 @@ class FlowConfigRepository:
|
||||
)
|
||||
await db.commit()
|
||||
|
||||
async def activate(self, cfg_id: int) -> None:
|
||||
async with get_db() as db:
|
||||
await db.execute("UPDATE flow_configs SET is_active=0")
|
||||
await db.execute("UPDATE flow_configs SET is_active=1 WHERE id=?", (cfg_id,))
|
||||
await db.commit()
|
||||
|
||||
async def delete(self, cfg_id: int) -> None:
|
||||
async with get_db() as db:
|
||||
await db.execute("DELETE FROM flow_configs WHERE id=?", (cfg_id,))
|
||||
|
||||
Reference in New Issue
Block a user