Compare commits

..

No commits in common. "2bd07e5f2d6bf28811082519c43c5bbd85b1bed8" and "f13ed8a078a6725ac4852309c6ecb5229e5f84cb" have entirely different histories.

2 changed files with 20 additions and 21 deletions

View File

@ -39,14 +39,14 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for game in top_10_games_by_playtime %} {% for purchase in top_10_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 href="{% url 'view_game' purchase.edition.game.id %}">{{ purchase.edition.name }}
</a> </a>
</td> </td>
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ game.formatted_playtime }}</td> <td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ purchase.formatted_playtime }}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>

View File

@ -247,27 +247,25 @@ def stats(request, year: int = 0):
timestamp_start__gte=first_day_of_year timestamp_start__gte=first_day_of_year
).filter(timestamp_start__lt=last_day_of_year) ).filter(timestamp_start__lt=last_day_of_year)
year_purchases = Purchase.objects.filter(session__in=year_sessions).distinct() year_purchases = Purchase.objects.filter(session__in=year_sessions).distinct()
year_purchases_with_playtime = year_purchases.annotate(
games_with_playtime = ( total_playtime=Sum(
Game.objects.filter(edition__purchase__session__in=year_sessions) F("session__duration_calculated") + F("session__duration_manual")
.annotate(
total_playtime=Sum(
F("edition__purchase__session__duration_calculated")
+ F("edition__purchase__session__duration_manual")
)
) )
.values("id", "name", "total_playtime")
) )
top_10_games_by_playtime = games_with_playtime.order_by("-total_playtime")[:10] top_10_by_playtime = year_purchases_with_playtime.order_by("-total_playtime")[:10]
for game in top_10_games_by_playtime: for purchase in top_10_by_playtime:
game["formatted_playtime"] = format_duration(game["total_playtime"], "%2.0H") purchase.formatted_playtime = format_duration(purchase.total_playtime, "%2.0H")
total_playtime_per_platform = ( total_playtime_per_platform = (
year_sessions.values("purchase__platform__name") year_sessions.values("purchase__platform__name") # Group by platform name
.annotate(total_playtime=Sum(F("duration_calculated") + F("duration_manual"))) .annotate(
.annotate(platform_name=F("purchase__platform__name")) total_playtime=Sum(F("duration_calculated") + F("duration_manual"))
.values("platform_name", "total_playtime") ) # Sum the duration_calculated for each group
.order_by("-total_playtime") .annotate(platform_name=F("purchase__platform__name")) # Rename the field
.values(
"platform_name", "total_playtime"
) # Select the renamed field and total_playtime
.order_by("-total_playtime") # Optional: Order by the renamed platform name
) )
for item in total_playtime_per_platform: for item in total_playtime_per_platform:
item["formatted_playtime"] = format_duration(item["total_playtime"], "%2.0H") item["formatted_playtime"] = format_duration(item["total_playtime"], "%2.0H")
@ -278,7 +276,8 @@ def stats(request, year: int = 0):
), ),
"total_games": year_purchases.count(), "total_games": year_purchases.count(),
"total_2023_games": year_purchases.filter(edition__year_released=year).count(), "total_2023_games": year_purchases.filter(edition__year_released=year).count(),
"top_10_games_by_playtime": top_10_games_by_playtime, "top_10_by_playtime_formatted": top_10_by_playtime,
"top_10_by_playtime": top_10_by_playtime,
"year": year, "year": year,
"total_playtime_per_platform": total_playtime_per_platform, "total_playtime_per_platform": total_playtime_per_platform,
} }