From 5052ca7dbfbad82be1d0759a54760c6318d7900c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Wed, 8 Nov 2023 16:24:22 +0100 Subject: [PATCH] Add more stats * All finished games * All finished 2023 games * All finished games that were purchased this year * Total sessions * Days played --- CHANGELOG.md | 6 ++++ games/templates/stats.html | 61 +++++++++++++++++++++++++++++++++++++- games/views.py | 23 +++++++++++++- 3 files changed, 88 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c2e17e..e511804 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,12 @@ * Upon migration, this will be set to a year of any of the game's edition that has it set * Purchase: Date Finished * Editions are now unique combination of name and platform +* Add more stats: + * All finished games + * All finished 2023 games + * All finished games that were purchased this year + * Total sessions + * Days played ### Improved * game overview: simplify playtime range display diff --git a/games/templates/stats.html b/games/templates/stats.html index b9d7f2c..08816b3 100644 --- a/games/templates/stats.html +++ b/games/templates/stats.html @@ -27,6 +27,14 @@ Hours {{ total_hours }} + + Sessions + {{ total_sessions }} + + + Days Played + {{ unique_days }} ({{ unique_days_percent }}%) + Games {{ total_games }} @@ -87,7 +95,58 @@ {% endfor %} -

Purchases

+

Finished

+ + + + + + + + + {% for purchase in all_finished_this_year %} + + + + + {% endfor %} + +
NameDate
{{ purchase.edition.name }}{{ purchase.date_finished | date:"d/m/Y" }}
+

Finished (2023 games)

+ + + + + + + + + {% for purchase in this_year_finished_this_year %} + + + + + {% endfor %} + +
NameDate
{{ purchase.edition.name }}{{ purchase.date_finished | date:"d/m/Y" }}
+

Bought and Finished ({{ year }})

+ + + + + + + + + {% for purchase in purchased_this_year_finished_this_year %} + + + + + {% endfor %} + +
NameDate
{{ purchase.edition.name }}{{ purchase.date_finished | date:"d/m/Y" }}
+

All Purchases

diff --git a/games/views.py b/games/views.py index 0a4c210..6989c2d 100644 --- a/games/views.py +++ b/games/views.py @@ -1,7 +1,8 @@ from common.time import format_duration, now as now_with_tz from datetime import datetime, timedelta from django.conf import settings -from django.db.models import Sum, F +from django.db.models import Sum, F, Count +from django.db.models.functions import TruncDate from django.http import HttpRequest, HttpResponse, HttpResponseRedirect from django.shortcuts import redirect, render from django.urls import reverse @@ -276,6 +277,12 @@ def stats(request, year: int = 0): first_day_of_year = datetime(year, 1, 1) last_day_of_year = datetime(year + 1, 1, 1) year_sessions = Session.objects.filter(timestamp_start__year=year) + unique_days = ( + year_sessions.annotate(date=TruncDate("timestamp_start")) + .values("date") + .distinct() + .aggregate(dates=Count("date")) + ) year_played_purchases = Purchase.objects.filter( session__in=year_sessions ).distinct() @@ -288,6 +295,14 @@ def stats(request, year: int = 0): .order_by("date_purchased") ) + all_finished_this_year = Purchase.objects.filter(date_finished__year=year) + this_year_finished_this_year = Purchase.objects.filter( + date_finished__year=year + ).filter(edition__year_released=year) + purchased_this_year_finished_this_year = all_purchased_this_year.filter( + date_finished__year=year + ) + this_year_spendings = all_purchased_this_year.aggregate(total_spent=Sum(F("price"))) total_spent = this_year_spendings["total_spent"] @@ -330,6 +345,12 @@ def stats(request, year: int = 0): "total_spent_currency": selected_currency, "all_purchased_this_year": all_purchased_this_year, "spent_per_game": int(total_spent / all_purchased_this_year.count()), + "all_finished_this_year": all_finished_this_year, + "this_year_finished_this_year": this_year_finished_this_year, + "purchased_this_year_finished_this_year": purchased_this_year_finished_this_year, + "total_sessions": year_sessions.count(), + "unique_days": unique_days["dates"], + "unique_days_percent": int(unique_days["dates"] / 365 * 100), } request.session["return_path"] = request.path