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"> <table class="responsive-table">
<thead> <thead>
<tr> <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">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">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">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> </tr>
<tbody> <tbody>
<tr> <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_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_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">{{ 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> </tr>
</tbody> </tbody>
</table> </table>
@ -43,7 +49,7 @@
{% for game in top_10_games_by_playtime %} {% for game in top_10_games_by_playtime %}
<tr> <tr>
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono"> <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> </a>
</td> </td>
@ -63,11 +69,30 @@
<tbody> <tbody>
{% for item in total_playtime_per_platform %} {% for item in total_playtime_per_platform %}
<tr> <tr>
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ item.platform_name }} </td> <td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ item.platform_name }}</td>
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ item.formatted_playtime }}</td> <td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ item.formatted_playtime }}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </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> </div>
{% endblock content %} {% endblock content %}

View File

@ -243,10 +243,21 @@ def stats(request, year: int = 0):
year = now_with_tz().year year = now_with_tz().year
first_day_of_year = datetime(year, 1, 1) first_day_of_year = datetime(year, 1, 1)
last_day_of_year = datetime(year + 1, 1, 1) last_day_of_year = datetime(year + 1, 1, 1)
year_sessions = Session.objects.filter( year_sessions = Session.objects.filter(timestamp_start__year=year)
timestamp_start__gte=first_day_of_year year_played_purchases = Purchase.objects.filter(
).filter(timestamp_start__lt=last_day_of_year) session__in=year_sessions
year_purchases = Purchase.objects.filter(session__in=year_sessions).distinct() ).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 = ( games_with_playtime = (
Game.objects.filter(edition__purchase__session__in=year_sessions) Game.objects.filter(edition__purchase__session__in=year_sessions)
@ -276,11 +287,17 @@ def stats(request, year: int = 0):
"total_hours": format_duration( "total_hours": format_duration(
year_sessions.total_duration_unformatted(), "%2.0H" year_sessions.total_duration_unformatted(), "%2.0H"
), ),
"total_games": year_purchases.count(), "total_games": year_played_purchases.count(),
"total_2023_games": year_purchases.filter(edition__year_released=year).count(), "total_2023_games": year_played_purchases.filter(
edition__year_released=year
).count(),
"top_10_games_by_playtime": top_10_games_by_playtime, "top_10_games_by_playtime": top_10_games_by_playtime,
"year": year, "year": year,
"total_playtime_per_platform": total_playtime_per_platform, "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) return render(request, "stats.html", context)