forked from zakaria/chicken-counting-sukawarna-det
Auto-init DB on dashboard start if file missing
_init_db() creates the schema at startup so /api/db/* endpoints work immediately without needing a batch run first.
This commit is contained in:
1 parent
dc447264b7
commit
e7c26d3f86
1 file changed
+21
-1
+21
-1
@@ -33,10 +33,27 @@ _db_conn = None
|
||||
_db_lock = threading.Lock()
|
||||
|
||||
|
||||
def _init_db(db_path: str) -> None:
|
||||
conn = sqlite3.connect(db_path)
|
||||
conn.execute("PRAGMA journal_mode=WAL")
|
||||
conn.execute("""CREATE TABLE IF NOT EXISTS batch_runs (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
date TEXT NOT NULL, location TEXT NOT NULL, camera_id TEXT NOT NULL,
|
||||
total_entered INTEGER NOT NULL DEFAULT 0,
|
||||
frames_processed INTEGER NOT NULL DEFAULT 0,
|
||||
elapsed_seconds REAL NOT NULL DEFAULT 0.0,
|
||||
stopped_reason TEXT NOT NULL DEFAULT '',
|
||||
source_video TEXT NOT NULL DEFAULT '',
|
||||
generated_at TEXT NOT NULL DEFAULT '',
|
||||
UNIQUE(date, location, camera_id))""")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
def _get_db():
|
||||
global _db_conn
|
||||
db = app.config["db_path"]
|
||||
if not db or not Path(db).exists():
|
||||
if not db:
|
||||
return None
|
||||
with _db_lock:
|
||||
if _db_conn is None:
|
||||
@@ -196,6 +213,9 @@ def main():
|
||||
app.config["run_date"] = args.date
|
||||
app.config["db_path"] = str(Path(args.db).resolve()) if args.db else ""
|
||||
|
||||
if app.config["db_path"]:
|
||||
_init_db(app.config["db_path"])
|
||||
|
||||
print(f"[dashboard] http://0.0.0.0:{args.port} shm={args.shm_dir} db={args.db}" + (f" date={args.date}" if args.date else ""))
|
||||
app.run(host="0.0.0.0", port=args.port, debug=args.debug, threaded=True)
|
||||
|
||||
|
||||
Reference in new issue
Block a user