diff --git a/db/models.py b/db/models.py index 4db5dbe..382b71e 100644 --- a/db/models.py +++ b/db/models.py @@ -916,3 +916,29 @@ def get_stats_drink_types(): drink["icon"] = drink_type_info["icon"] return drinks + +def get_stats_drink_hourly(): + """ + Retrieve hourly drink statistics from the database. + Executes a SQL query to count the number of drinks grouped by hour of the day + (00-23). Returns a list of dictionaries containing the hour and count for each + hour. Hours without any drinks are included with a count of 0. + Returns: + list[dict]: A list of dictionaries with keys 'hour' (string, e.g., "00", "01", ..., "23") + and 'count' (int). The list is sorted by hour in ascending order. + Returns an empty list if no drinks exist in the database. + """ + + t = text("SELECT strftime('%H', timestamp), count(*) FROM drinks GROUP BY strftime('%H', timestamp);") + with engine.connect() as connection: + result = connection.execute(t).fetchall() + if not result: + return [] + hourly_stats = [{"hour": row[0], "count": row[1]} for row in result] + # sort by hour and add hours without drinks with count 0 + for hour in range(24): + if not any(stat["hour"] == f"{hour:02d}" for stat in hourly_stats): + hourly_stats.append({"hour": f"{hour:02d}", "count": 0}) + hourly_stats.sort(key=lambda x: x["hour"]) + + return hourly_stats diff --git a/main.py b/main.py index e68395f..fcffbbc 100644 --- a/main.py +++ b/main.py @@ -500,12 +500,14 @@ def stats(request: Request): raise HTTPException(status_code=404, detail="User not found") drink_types = db.models.get_stats_drink_types() + hourly_stats = db.models.get_stats_drink_hourly() return templates.TemplateResponse("stats.html", { "request": request, "user": user_authentik, "user_db_id": user_db_id, "stats_drink_types": drink_types, + "stats_drink_hourly": hourly_stats, }) @app.post("/add_drink_type") diff --git a/templates/stats.html b/templates/stats.html index d4fa0f9..3582b46 100644 --- a/templates/stats.html +++ b/templates/stats.html @@ -7,12 +7,18 @@