Ukandu commited on
Commit
c4e455e
·
verified ·
1 Parent(s): 534a7ee

Update app/services/storage.py

Browse files
Files changed (1) hide show
  1. app/services/storage.py +41 -11
app/services/storage.py CHANGED
@@ -1,6 +1,7 @@
1
  import duckdb
2
  import uuid
3
  from pathlib import Path
 
4
 
5
  DB_PATH = Path("data/beeline.duckdb")
6
  DB_PATH.parent.mkdir(parents=True, exist_ok=True)
@@ -9,12 +10,12 @@ conn = duckdb.connect(DB_PATH.as_posix())
9
 
10
  conn.execute("""
11
  CREATE TABLE IF NOT EXISTS runs (
12
- run_id TEXT,
13
  dataset TEXT,
14
  algorithm TEXT,
15
  status TEXT,
16
  progress INT,
17
- created_at TEXT
18
  )
19
  """)
20
 
@@ -30,11 +31,31 @@ CREATE TABLE IF NOT EXISTS metrics (
30
  def create_run(dataset: str, algorithm: str) -> str:
31
  run_id = str(uuid.uuid4())
32
  conn.execute(
33
- "INSERT INTO runs VALUES (?, ?, ?, ?, ?, ?)",
34
- (run_id, dataset, algorithm, "running", progress, created_at)
 
 
 
 
 
 
 
 
 
35
  )
36
  return run_id
37
 
 
 
 
 
 
 
 
 
 
 
 
38
  def save_metrics(run_id: str, metrics: dict):
39
  conn.execute(
40
  "INSERT INTO metrics VALUES (?, ?, ?, ?)",
@@ -45,15 +66,24 @@ def save_metrics(run_id: str, metrics: dict):
45
  metrics["f1"]
46
  )
47
  )
48
- conn.execute(
49
- "UPDATE runs SET status = 'completed' WHERE run_id = ?",
 
 
 
 
50
  (run_id,)
51
- )
 
 
 
 
 
 
 
 
 
52
 
53
- def list_runs():
54
- return conn.execute(
55
- "SELECT * FROM runs"
56
- ).fetchdf().to_dict("records")
57
 
58
  def get_run_metrics(run_id: str):
59
  row = conn.execute(
 
1
  import duckdb
2
  import uuid
3
  from pathlib import Path
4
+ from datetime import datetime
5
 
6
  DB_PATH = Path("data/beeline.duckdb")
7
  DB_PATH.parent.mkdir(parents=True, exist_ok=True)
 
10
 
11
  conn.execute("""
12
  CREATE TABLE IF NOT EXISTS runs (
13
+ run_id TEXT PRIMARY KEY,
14
  dataset TEXT,
15
  algorithm TEXT,
16
  status TEXT,
17
  progress INT,
18
+ created_at TIMESTAMP
19
  )
20
  """)
21
 
 
31
  def create_run(dataset: str, algorithm: str) -> str:
32
  run_id = str(uuid.uuid4())
33
  conn.execute(
34
+ """
35
+ INSERT INTO runs VALUES (?, ?, ?, ?, ?, ?)
36
+ """,
37
+ (
38
+ run_id,
39
+ dataset,
40
+ algorithm,
41
+ "running",
42
+ 0,
43
+ datetime.utcnow()
44
+ )
45
  )
46
  return run_id
47
 
48
+
49
+ def update_run(run_id: str, status: str, progress: int):
50
+ conn.execute(
51
+ """
52
+ UPDATE runs SET status = ?, progress = ?
53
+ WHERE run_id = ?
54
+ """,
55
+ (status, progress, run_id)
56
+ )
57
+
58
+
59
  def save_metrics(run_id: str, metrics: dict):
60
  conn.execute(
61
  "INSERT INTO metrics VALUES (?, ?, ?, ?)",
 
66
  metrics["f1"]
67
  )
68
  )
69
+ update_run(run_id, "completed", 100)
70
+
71
+
72
+ def get_run(run_id: str):
73
+ row = conn.execute(
74
+ "SELECT run_id, status, progress FROM runs WHERE run_id = ?",
75
  (run_id,)
76
+ ).fetchone()
77
+
78
+ if not row:
79
+ return None
80
+
81
+ return {
82
+ "run_id": row[0],
83
+ "status": row[1],
84
+ "progress": row[2]
85
+ }
86
 
 
 
 
 
87
 
88
  def get_run_metrics(run_id: str):
89
  row = conn.execute(