40 lines
1.5 KiB
Python
40 lines
1.5 KiB
Python
|
|
"""
|
||
|
|
Statistics Endpoints for Mitai Jinkendo
|
||
|
|
|
||
|
|
Dashboard statistics showing entry counts across all categories.
|
||
|
|
"""
|
||
|
|
from typing import Optional
|
||
|
|
|
||
|
|
from fastapi import APIRouter, Header, Depends
|
||
|
|
|
||
|
|
from db import get_db, get_cursor
|
||
|
|
from auth import require_auth
|
||
|
|
from routers.profiles import get_pid
|
||
|
|
|
||
|
|
router = APIRouter(prefix="/api", tags=["stats"])
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/stats")
|
||
|
|
def get_stats(x_profile_id: Optional[str]=Header(default=None), session: dict=Depends(require_auth)):
|
||
|
|
"""Get entry counts for all tracking categories."""
|
||
|
|
pid = get_pid(x_profile_id)
|
||
|
|
with get_db() as conn:
|
||
|
|
cur = get_cursor(conn)
|
||
|
|
cur.execute("SELECT COUNT(*) as count FROM weight_log WHERE profile_id=%s",(pid,))
|
||
|
|
weight_count = cur.fetchone()['count']
|
||
|
|
cur.execute("SELECT COUNT(*) as count FROM circumference_log WHERE profile_id=%s",(pid,))
|
||
|
|
circ_count = cur.fetchone()['count']
|
||
|
|
cur.execute("SELECT COUNT(*) as count FROM caliper_log WHERE profile_id=%s",(pid,))
|
||
|
|
caliper_count = cur.fetchone()['count']
|
||
|
|
cur.execute("SELECT COUNT(*) as count FROM nutrition_log WHERE profile_id=%s",(pid,))
|
||
|
|
nutrition_count = cur.fetchone()['count']
|
||
|
|
cur.execute("SELECT COUNT(*) as count FROM activity_log WHERE profile_id=%s",(pid,))
|
||
|
|
activity_count = cur.fetchone()['count']
|
||
|
|
return {
|
||
|
|
"weight_count": weight_count,
|
||
|
|
"circ_count": circ_count,
|
||
|
|
"caliper_count": caliper_count,
|
||
|
|
"nutrition_count": nutrition_count,
|
||
|
|
"activity_count": activity_count
|
||
|
|
}
|