stats: add monthly playtimes
Django CI/CD / test (push) Successful in 58s Details
Django CI/CD / build-and-push (push) Successful in 1m54s Details

This commit is contained in:
Lukáš Kucharczyk 2024-04-02 08:18:58 +02:00
parent ff28600710
commit d9b5f0eab2
Signed by: lukas
SSH Key Fingerprint: SHA256:vMuSwvwAvcT6htVAioMP7rzzwMQNi3roESyhv+nAxeg
3 changed files with 26 additions and 2 deletions

View File

@ -3,7 +3,7 @@
## New
* Render notes as Markdown
* Require login by default
* Add stats for dropped purchases
* Add stats for dropped purchases, monthly playtimes
## Improved
* mark refunded purchases red on game overview

View File

@ -73,6 +73,19 @@
</tr>
</tbody>
</table>
<h1 class="text-5xl text-center my-6">Playtime per month</h1>
<table class="responsive-table">
<tbody>
{% for month in month_playtimes %}
<tr>
<td class="px-2 sm:px-4 md:px-6 md:py-2">{{ month.month | date:"F" }}</td>
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ month.playtime }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<h1 class="text-5xl text-center my-6">Purchases</h1>
<table class="responsive-table">
<tbody>

View File

@ -12,8 +12,9 @@ from django.db.models import (
Q,
Sum,
fields,
IntegerField,
)
from django.db.models.functions import TruncDate
from django.db.models.functions import TruncDate, ExtractMonth, TruncMonth
from django.http import (
HttpRequest,
HttpResponse,
@ -443,6 +444,15 @@ def stats(request, year: int = 0):
)
.values("id", "name", "total_playtime")
)
month_playtimes = (
this_year_sessions.annotate(month=TruncMonth("timestamp_start"))
.values("month")
.annotate(playtime=Sum("duration_calculated"))
.order_by("month")
)
for month in month_playtimes:
month["playtime"] = format_duration(month["playtime"], "%2.0H")
highest_session_average_game = (
Game.objects.filter(edition__purchase__session__in=this_year_sessions)
.annotate(
@ -574,6 +584,7 @@ def stats(request, year: int = 0):
"last_play_name": last_play_name,
"last_play_date": last_play_date,
"title": f"{year} Stats",
"month_playtimes": month_playtimes,
}
request.session["return_path"] = request.path