Add DB API endpoints: summary, history, date, camera, location
- api/db/summary — overall totals - api/db/history — per-date rows - api/db/date/<date> — single date detail with per-camera breakdown - api/db/camera/<id> — all runs for a specific camera - api/db/location/<name> — per-location summary + history - API.md — full endpoint documentation with schema
This commit is contained in:
1 parent
efd4726bc6
commit
ae86821165
2 files changed
+229
-9
No files matched your search
+80
-9
@@ -66,13 +66,37 @@ def shm_frame(camera_id):
|
||||
return send_file(frame_path, mimetype="image/jpeg", max_age=0, download_name=None)
|
||||
|
||||
|
||||
@app.route("/api/db/history")
|
||||
def db_history():
|
||||
def _get_db():
|
||||
db = app.config["db_path"]
|
||||
if not db or not Path(db).exists():
|
||||
return jsonify([])
|
||||
|
||||
return None
|
||||
conn = sqlite3.connect(db)
|
||||
conn.row_factory = sqlite3.Row
|
||||
return conn
|
||||
|
||||
|
||||
@app.route("/api/db/summary")
|
||||
def db_summary():
|
||||
conn = _get_db()
|
||||
if not conn:
|
||||
return jsonify({})
|
||||
row = conn.execute("""
|
||||
SELECT COUNT(DISTINCT date) AS days,
|
||||
COUNT(DISTINCT location) AS locations,
|
||||
COUNT(*) AS total_runs,
|
||||
SUM(total_entered) AS total_chickens,
|
||||
ROUND(SUM(elapsed_seconds)/3600.0, 1) AS total_hours
|
||||
FROM batch_runs
|
||||
""").fetchone()
|
||||
conn.close()
|
||||
return jsonify(dict(row))
|
||||
|
||||
|
||||
@app.route("/api/db/history")
|
||||
def db_history():
|
||||
conn = _get_db()
|
||||
if not conn:
|
||||
return jsonify([])
|
||||
rows = conn.execute("""
|
||||
SELECT date, location,
|
||||
COUNT(*) AS cams,
|
||||
@@ -84,12 +108,59 @@ def db_history():
|
||||
LIMIT 50
|
||||
""").fetchall()
|
||||
conn.close()
|
||||
return jsonify([dict(r) for r in rows])
|
||||
|
||||
return jsonify([
|
||||
{"date": r[0], "location": r[1], "cams": r[2],
|
||||
"total": r[3], "minutes": r[4]}
|
||||
for r in rows
|
||||
])
|
||||
|
||||
@app.route("/api/db/date/<date>")
|
||||
def db_date(date):
|
||||
conn = _get_db()
|
||||
if not conn:
|
||||
return jsonify({})
|
||||
cameras = conn.execute("""
|
||||
SELECT camera_id, total_entered, frames_processed,
|
||||
ROUND(elapsed_seconds,1) AS elapsed_seconds,
|
||||
stopped_reason, source_video, location
|
||||
FROM batch_runs WHERE date=? ORDER BY camera_id
|
||||
""", (date,)).fetchall()
|
||||
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]})
|
||||
|
||||
|
||||
@app.route("/api/db/camera/<camera_id>")
|
||||
def db_camera(camera_id):
|
||||
conn = _get_db()
|
||||
if not conn:
|
||||
return jsonify([])
|
||||
rows = conn.execute("""
|
||||
SELECT date, location, total_entered, frames_processed,
|
||||
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])
|
||||
|
||||
|
||||
@app.route("/api/db/location/<location>")
|
||||
def db_location(location):
|
||||
conn = _get_db()
|
||||
if not conn:
|
||||
return jsonify({})
|
||||
history = conn.execute("""
|
||||
SELECT date, GROUP_CONCAT(camera_id,', ') AS cameras,
|
||||
SUM(total_entered) AS total,
|
||||
ROUND(SUM(elapsed_seconds)/60.0,1) AS minutes
|
||||
FROM batch_runs WHERE location=? GROUP BY date ORDER BY date DESC LIMIT 50
|
||||
""", (location,)).fetchall()
|
||||
summary = conn.execute("""
|
||||
SELECT COUNT(DISTINCT date) AS days, SUM(total_entered) AS total,
|
||||
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]})
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
Reference in new issue
Block a user