forked from zakaria/chicken-counting-sukawarna-det
Reduce dashboard CPU: cache DB, suppress logs, add systemd service
- Suppress Flask request logs (werkzeug ERROR only) - Cache SQLite connection with WAL + 8MB cache - Increase poll_ms 500->1000, history refresh 30s->60s - Show 'waiting for pipeline...' when /dev/shm empty - Add chicken-dashboard.service for systemd auto-start
This commit is contained in:
1 parent
9512fc7f4d
commit
dc447264b7
3 files changed
+58
-11
No files matched your search
+24
-6
@@ -9,20 +9,43 @@ from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import logging
|
||||
import sqlite3
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
||||
from flask import Flask, Response, jsonify, render_template, send_file
|
||||
|
||||
# Suppress Flask request logs
|
||||
log = logging.getLogger("werkzeug")
|
||||
log.setLevel(logging.ERROR)
|
||||
|
||||
DEFAULT_SHM_DIR = "/dev/shm"
|
||||
DEFAULT_PORT = 8080
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config["shm_dir"] = DEFAULT_SHM_DIR
|
||||
app.config["poll_ms"] = 500
|
||||
app.config["poll_ms"] = 1000
|
||||
app.config["run_date"] = ""
|
||||
app.config["db_path"] = ""
|
||||
|
||||
_db_conn = None
|
||||
_db_lock = threading.Lock()
|
||||
|
||||
|
||||
def _get_db():
|
||||
global _db_conn
|
||||
db = app.config["db_path"]
|
||||
if not db or not Path(db).exists():
|
||||
return None
|
||||
with _db_lock:
|
||||
if _db_conn is None:
|
||||
_db_conn = sqlite3.connect(db, check_same_thread=False)
|
||||
_db_conn.row_factory = sqlite3.Row
|
||||
_db_conn.execute("PRAGMA journal_mode=WAL")
|
||||
_db_conn.execute("PRAGMA cache_size=-8000")
|
||||
return _db_conn
|
||||
|
||||
|
||||
def _discover_cameras():
|
||||
shm = Path(app.config["shm_dir"])
|
||||
@@ -88,7 +111,6 @@ def db_summary():
|
||||
ROUND(SUM(elapsed_seconds)/3600.0, 1) AS total_hours
|
||||
FROM batch_runs
|
||||
""").fetchone()
|
||||
conn.close()
|
||||
return jsonify(dict(row))
|
||||
|
||||
|
||||
@@ -107,7 +129,6 @@ def db_history():
|
||||
ORDER BY date DESC, location
|
||||
LIMIT 50
|
||||
""").fetchall()
|
||||
conn.close()
|
||||
return jsonify([dict(r) for r in rows])
|
||||
|
||||
|
||||
@@ -125,7 +146,6 @@ def db_date(date):
|
||||
total = conn.execute(
|
||||
"SELECT SUM(total_entered) AS total, ROUND(SUM(elapsed_seconds)/60.0,1) AS minutes "
|
||||
"FROM batch_runs WHERE date=?", (date,)).fetchone()
|
||||
conn.close()
|
||||
return jsonify({"date": date, "total": dict(total), "cameras": [dict(r) for r in cameras]})
|
||||
|
||||
|
||||
@@ -139,7 +159,6 @@ def db_camera(camera_id):
|
||||
ROUND(elapsed_seconds,1) AS elapsed_seconds, stopped_reason
|
||||
FROM batch_runs WHERE camera_id=? ORDER BY date DESC LIMIT 50
|
||||
""", (camera_id,)).fetchall()
|
||||
conn.close()
|
||||
return jsonify([dict(r) for r in rows])
|
||||
|
||||
|
||||
@@ -159,7 +178,6 @@ def db_location(location):
|
||||
ROUND(SUM(elapsed_seconds)/3600.0,1) AS hours
|
||||
FROM batch_runs WHERE location=?
|
||||
""", (location,)).fetchone()
|
||||
conn.close()
|
||||
return jsonify({"location": location, "summary": dict(summary), "history": [dict(r) for r in history]})
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user