From ad0641f95b645054d35719b870c1973fa24d3246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Thu, 17 Apr 2025 16:15:38 +0200 Subject: [PATCH] Fix playtime stats per year --- games/templates/stats.html | 2 +- games/views/general.py | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/games/templates/stats.html b/games/templates/stats.html index a276c59..db40856 100644 --- a/games/templates/stats.html +++ b/games/templates/stats.html @@ -167,7 +167,7 @@ - {{ game.playtime | format_duration }} + {{ game.total_playtime | format_duration }} {% endfor %} diff --git a/games/views/general.py b/games/views/general.py index 28f615f..35c3eca 100644 --- a/games/views/general.py +++ b/games/views/general.py @@ -364,9 +364,16 @@ def stats(request: HttpRequest, year: int = 0) -> HttpResponse: ) total_spent = this_year_spendings["total_spent"] or 0 - games_with_playtime = Game.objects.filter( - sessions__in=this_year_sessions - ).distinct() + games_with_playtime = ( + Game.objects.filter(sessions__timestamp_start__year=year) + .annotate( + total_playtime=Sum( + F("sessions__duration_calculated"), + ) + ) + .filter(total_playtime__gt=timedelta(0)) + ) + month_playtimes = ( this_year_sessions.annotate(month=TruncMonth("timestamp_start")) .values("month") @@ -380,7 +387,7 @@ def stats(request: HttpRequest, year: int = 0) -> HttpResponse: .order_by("-session_average") .first() ) - top_10_games_by_playtime = games_with_playtime.order_by("-playtime") + top_10_games_by_playtime = games_with_playtime.order_by("-total_playtime") total_playtime_per_platform = ( this_year_sessions.values("game__platform__name")