Ukandu commited on
Commit
7cef2c1
·
verified ·
1 Parent(s): 51ce49c

Create runs.py

Browse files
Files changed (1) hide show
  1. app/registry/runs.py +106 -0
app/registry/runs.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import duckdb
2
+ from pathlib import Path
3
+ from datetime import datetime
4
+ import uuid
5
+ import json
6
+
7
+ DB_PATH = Path("data/runs.duckdb")
8
+ DB_PATH.parent.mkdir(exist_ok=True)
9
+
10
+ conn = duckdb.connect(DB_PATH.as_posix())
11
+
12
+ conn.execute("""
13
+ CREATE TABLE IF NOT EXISTS runs (
14
+ run_id TEXT PRIMARY KEY,
15
+ dataset_id TEXT,
16
+ algorithm TEXT,
17
+ status TEXT,
18
+ progress INTEGER,
19
+ metrics JSON,
20
+ error TEXT,
21
+ created_at TIMESTAMP,
22
+ updated_at TIMESTAMP
23
+ )
24
+ """)
25
+
26
+ def create_run(dataset_id: str, algorithm: str) -> str:
27
+ run_id = str(uuid.uuid4())
28
+ now = datetime.utcnow()
29
+
30
+ conn.execute(
31
+ """
32
+ INSERT INTO runs VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
33
+ """,
34
+ (
35
+ run_id,
36
+ dataset_id,
37
+ algorithm,
38
+ "queued",
39
+ 0,
40
+ None,
41
+ None,
42
+ now,
43
+ now
44
+ )
45
+ )
46
+ return run_id
47
+
48
+
49
+ def update_run(
50
+ run_id: str,
51
+ status: str = None,
52
+ progress: int = None,
53
+ metrics: dict = None,
54
+ error: str = None
55
+ ):
56
+ now = datetime.utcnow()
57
+
58
+ row = conn.execute(
59
+ "SELECT * FROM runs WHERE run_id = ?",
60
+ (run_id,)
61
+ ).fetchone()
62
+
63
+ if not row:
64
+ return
65
+
66
+ conn.execute(
67
+ """
68
+ UPDATE runs SET
69
+ status = COALESCE(?, status),
70
+ progress = COALESCE(?, progress),
71
+ metrics = COALESCE(?, metrics),
72
+ error = COALESCE(?, error),
73
+ updated_at = ?
74
+ WHERE run_id = ?
75
+ """,
76
+ (
77
+ status,
78
+ progress,
79
+ json.dumps(metrics) if metrics else None,
80
+ error,
81
+ now,
82
+ run_id
83
+ )
84
+ )
85
+
86
+
87
+ def get_run(run_id: str) -> dict | None:
88
+ row = conn.execute(
89
+ "SELECT * FROM runs WHERE run_id = ?",
90
+ (run_id,)
91
+ ).fetchone()
92
+
93
+ if not row:
94
+ return None
95
+
96
+ keys = [
97
+ "run_id", "dataset_id", "algorithm", "status",
98
+ "progress", "metrics", "error",
99
+ "created_at", "updated_at"
100
+ ]
101
+
102
+ record = dict(zip(keys, row))
103
+ if record["metrics"]:
104
+ record["metrics"] = json.loads(record["metrics"])
105
+
106
+ return record