Add more stats
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Lukáš Kucharczyk 2023-11-02 20:12:32 +01:00
parent 8423fd02b4
commit e67aa3fda1
2 changed files with 53 additions and 11 deletions

View File

@ -19,15 +19,21 @@
<table class="responsive-table">
<thead>
<tr>
<th class="px-2 sm:px-4 md:px-6 md:py-2">Total hours</th>
<th class="px-2 sm:px-4 md:px-6 md:py-2">Total games</th>
<th class="px-2 sm:px-4 md:px-6 md:py-2">Released that year</th>
<th class="px-2 sm:px-4 md:px-6 md:py-2">Hours</th>
<th class="px-2 sm:px-4 md:px-6 md:py-2">Games</th>
<th class="px-2 sm:px-4 md:px-6 md:py-2">Games ({{ year }})</th>
<th class="px-2 sm:px-4 md:px-6 md:py-2">Purchases</th>
<th class="px-2 sm:px-4 md:px-6 md:py-2">Spendings ({{ total_spent_currency }})</th>
<th class="px-2 sm:px-4 md:px-6 md:py-2">{{ total_spent_currency }}/game</th>
</tr>
<tbody>
<tr>
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ total_hours }}</td>
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ total_games }}</td>
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ total_2023_games }}</td>
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ all_purchased_this_year.count }}</td>
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ total_spent }}</td>
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ spent_per_game }}</td>
</tr>
</tbody>
</table>
@ -43,7 +49,7 @@
{% for game in top_10_games_by_playtime %}
<tr>
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">
<a href="{% url 'view_game' game.id %}">{{ game.name }}
<a class="underline decoration-slate-500 sm:decoration-2" href="{% url 'view_game' game.id %}">{{ game.name }}
</a>
</td>
@ -69,5 +75,24 @@
{% endfor %}
</tbody>
</table>
<h1 class="text-5xl text-center my-6">Purchases</h1>
<table class="responsive-table">
<thead>
<tr>
<th class="px-2 sm:px-4 md:px-6 md:py-2 purchase-name truncate max-w-20char">Name</th>
<th class="px-2 sm:px-4 md:px-6 md:py-2">Price ({{ total_spent_currency }})</th>
<th class="px-2 sm:px-4 md:px-6 md:py-2">Date</th>
</tr>
</thead>
<tbody>
{% for purchase in all_purchased_this_year %}
<tr>
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono"><a class="underline decoration-slate-500 sm:decoration-2" href="{% url 'edit_purchase' purchase.id %}">{{ purchase.edition.name }}</a></td>
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ purchase.price }}</td>
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ purchase.date_purchased | date:"d/m/Y" }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock content %}

View File

@ -243,10 +243,21 @@ def stats(request, year: int = 0):
year = now_with_tz().year
first_day_of_year = datetime(year, 1, 1)
last_day_of_year = datetime(year + 1, 1, 1)
year_sessions = Session.objects.filter(
timestamp_start__gte=first_day_of_year
).filter(timestamp_start__lt=last_day_of_year)
year_purchases = Purchase.objects.filter(session__in=year_sessions).distinct()
year_sessions = Session.objects.filter(timestamp_start__year=year)
year_played_purchases = Purchase.objects.filter(
session__in=year_sessions
).distinct()
selected_currency = "CZK"
all_purchased_this_year = (
Purchase.objects.filter(date_purchased__year=year)
.filter(price_currency__exact=selected_currency)
.filter(date_refunded__exact=None)
.order_by("date_purchased")
)
this_year_spendings = all_purchased_this_year.aggregate(total_spent=Sum(F("price")))
total_spent = this_year_spendings["total_spent"]
games_with_playtime = (
Game.objects.filter(edition__purchase__session__in=year_sessions)
@ -276,11 +287,17 @@ def stats(request, year: int = 0):
"total_hours": format_duration(
year_sessions.total_duration_unformatted(), "%2.0H"
),
"total_games": year_purchases.count(),
"total_2023_games": year_purchases.filter(edition__year_released=year).count(),
"total_games": year_played_purchases.count(),
"total_2023_games": year_played_purchases.filter(
edition__year_released=year
).count(),
"top_10_games_by_playtime": top_10_games_by_playtime,
"year": year,
"total_playtime_per_platform": total_playtime_per_platform,
"total_spent": total_spent,
"total_spent_currency": selected_currency,
"all_purchased_this_year": all_purchased_this_year,
"spent_per_game": int(total_spent / all_purchased_this_year.count()),
}
return render(request, "stats.html", context)