-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
74 lines (65 loc) · 1.99 KB
/
Copy pathdatabase.py
File metadata and controls
74 lines (65 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import sqlite3
import json
from datetime import datetime
DB_PATH = "scans.db"
def init_db():
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute("""
CREATE TABLE IF NOT EXISTS scans (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT NOT NULL,
mode TEXT NOT NULL,
product_count INTEGER DEFAULT 0,
avg_freshness_score REAL DEFAULT 0,
flagged_count INTEGER DEFAULT 0,
shelf_gaps INTEGER DEFAULT 0,
detections_json TEXT
)
""")
conn.commit()
conn.close()
def log_scan(mode, product_count, avg_freshness, flagged_count, shelf_gaps, detections):
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute("""
INSERT INTO scans (timestamp, mode, product_count, avg_freshness_score, flagged_count, shelf_gaps, detections_json)
VALUES (?, ?, ?, ?, ?, ?, ?)
""", (
datetime.now().isoformat(),
mode,
product_count,
round(avg_freshness, 2),
flagged_count,
shelf_gaps,
json.dumps(detections)
))
conn.commit()
conn.close()
def get_history(limit=20):
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute("""
SELECT id, timestamp, mode, product_count, avg_freshness_score, flagged_count, shelf_gaps
FROM scans ORDER BY id DESC LIMIT ?
""", (limit,))
rows = c.fetchall()
conn.close()
return [
{
"id": r[0], "timestamp": r[1], "mode": r[2],
"product_count": r[3], "avg_freshness": r[4],
"flagged_count": r[5], "shelf_gaps": r[6]
}
for r in rows
]
def get_trend_data(limit=10):
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute("""
SELECT timestamp, avg_freshness_score, product_count, flagged_count
FROM scans WHERE mode='fruit' ORDER BY id DESC LIMIT ?
""", (limit,))
rows = c.fetchall()
conn.close()
return list(reversed(rows))