Refactor, remove cruft

This commit is contained in:
Lukáš Kucharczyk 2023-11-09 19:35:57 +01:00
parent 827279fb3d
commit 01ce582b74
1 changed files with 41 additions and 47 deletions

View File

@ -275,65 +275,57 @@ def stats(request, year: int = 0):
return HttpResponseRedirect(reverse("stats_by_year", args=[selected_year])) return HttpResponseRedirect(reverse("stats_by_year", args=[selected_year]))
if year == 0: if year == 0:
year = now_with_tz().year year = now_with_tz().year
first_day_of_year = datetime(year, 1, 1) this_year_sessions = Session.objects.filter(timestamp_start__year=year)
last_day_of_year = datetime(year + 1, 1, 1) selected_currency = "CZK"
year_sessions = Session.objects.filter(timestamp_start__year=year)
unique_days = ( unique_days = (
year_sessions.annotate(date=TruncDate("timestamp_start")) this_year_sessions.annotate(date=TruncDate("timestamp_start"))
.values("date") .values("date")
.distinct() .distinct()
.aggregate(dates=Count("date")) .aggregate(dates=Count("date"))
) )
year_played_purchases = Purchase.objects.filter( this_year_played_purchases = Purchase.objects.filter(
session__in=year_sessions session__in=this_year_sessions
).distinct() ).distinct()
selected_currency = "CZK" this_year_purchases = Purchase.objects.filter(date_purchased__year=year)
all_purchased_this_year = ( this_year_purchases_with_currency = this_year_purchases.filter(
Purchase.objects.filter(date_purchased__year=year) price_currency__exact=selected_currency
.filter(price_currency__exact=selected_currency)
.order_by("date_purchased")
) )
all_purchased_without_refunded_this_year = all_purchased_this_year.not_refunded() this_year_purchases_without_refunded = this_year_purchases_with_currency.filter(
all_purchased_refunded_this_year = ( date_refunded=None
Purchase.objects.filter(date_purchased__year=year)
.filter(price_currency__exact=selected_currency)
.refunded()
.order_by("date_purchased")
) )
this_year_purchases_refunded = this_year_purchases_with_currency.refunded()
purchased_unfinished = all_purchased_without_refunded_this_year.filter( this_year_purchases_unfinished = this_year_purchases_without_refunded.filter(
date_finished__isnull=True date_finished__isnull=True
) )
unfinished_purchases_percent = int( this_year_purchases_unfinished_percent = int(
safe_division( safe_division(
purchased_unfinished.count(), all_purchased_refunded_this_year.count() this_year_purchases_unfinished.count(), this_year_purchases_refunded.count()
) )
* 100 * 100
) )
all_finished_this_year = Purchase.objects.filter(date_finished__year=year).order_by( purchases_finished_this_year = Purchase.objects.filter(date_finished__year=year)
purchases_finished_this_year_released_this_year = (
purchases_finished_this_year.filter(edition__year_released=year).order_by(
"date_finished" "date_finished"
) )
this_year_finished_this_year = (
Purchase.objects.filter(date_finished__year=year)
.filter(edition__year_released=year)
.order_by("date_finished")
) )
purchased_this_year_finished_this_year = ( purchased_this_year_finished_this_year = (
all_purchased_without_refunded_this_year.filter( this_year_purchases_without_refunded.intersection(
date_finished__year=year purchases_finished_this_year
).order_by("date_finished") ).order_by("date_finished")
) )
this_year_spendings = all_purchased_without_refunded_this_year.aggregate( this_year_spendings = this_year_purchases_without_refunded.aggregate(
total_spent=Sum(F("price")) total_spent=Sum(F("price"))
) )
total_spent = this_year_spendings["total_spent"] 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=this_year_sessions)
.annotate( .annotate(
total_playtime=Sum( total_playtime=Sum(
F("edition__purchase__session__duration_calculated") F("edition__purchase__session__duration_calculated")
@ -347,7 +339,7 @@ def stats(request, year: int = 0):
game["formatted_playtime"] = format_duration(game["total_playtime"], "%2.0H") game["formatted_playtime"] = format_duration(game["total_playtime"], "%2.0H")
total_playtime_per_platform = ( total_playtime_per_platform = (
year_sessions.values("purchase__platform__name") this_year_sessions.values("purchase__platform__name")
.annotate(total_playtime=Sum(F("duration_calculated") + F("duration_manual"))) .annotate(total_playtime=Sum(F("duration_calculated") + F("duration_manual")))
.annotate(platform_name=F("purchase__platform__name")) .annotate(platform_name=F("purchase__platform__name"))
.values("platform_name", "total_playtime") .values("platform_name", "total_playtime")
@ -358,16 +350,16 @@ def stats(request, year: int = 0):
backlog_decrease_count = ( backlog_decrease_count = (
Purchase.objects.filter(date_purchased__year__lt=year) Purchase.objects.filter(date_purchased__year__lt=year)
.filter(date_finished__year=year) .intersection(purchases_finished_this_year)
.count() .count()
) )
context = { context = {
"total_hours": format_duration( "total_hours": format_duration(
year_sessions.total_duration_unformatted(), "%2.0H" this_year_sessions.total_duration_unformatted(), "%2.0H"
), ),
"total_games": year_played_purchases.count(), "total_games": this_year_played_purchases.count(),
"total_2023_games": year_played_purchases.filter( "total_2023_games": this_year_played_purchases.filter(
edition__year_released=year edition__year_released=year
).count(), ).count(),
"top_10_games_by_playtime": top_10_games_by_playtime, "top_10_games_by_playtime": top_10_games_by_playtime,
@ -375,27 +367,29 @@ def stats(request, year: int = 0):
"total_playtime_per_platform": total_playtime_per_platform, "total_playtime_per_platform": total_playtime_per_platform,
"total_spent": total_spent, "total_spent": total_spent,
"total_spent_currency": selected_currency, "total_spent_currency": selected_currency,
"all_purchased_this_year": all_purchased_without_refunded_this_year, "all_purchased_this_year": this_year_purchases_without_refunded,
"spent_per_game": int( "spent_per_game": int(
safe_division(total_spent, all_purchased_without_refunded_this_year.count()) safe_division(total_spent, this_year_purchases_without_refunded.count())
), ),
"all_finished_this_year": all_finished_this_year, "all_finished_this_year": purchases_finished_this_year,
"this_year_finished_this_year": this_year_finished_this_year, "this_year_finished_this_year": purchases_finished_this_year_released_this_year,
"purchased_this_year_finished_this_year": purchased_this_year_finished_this_year, "purchased_this_year_finished_this_year": purchased_this_year_finished_this_year,
"total_sessions": year_sessions.count(), "total_sessions": this_year_sessions.count(),
"unique_days": unique_days["dates"], "unique_days": unique_days["dates"],
"unique_days_percent": int(unique_days["dates"] / 365 * 100), "unique_days_percent": int(unique_days["dates"] / 365 * 100),
"purchased_unfinished": purchased_unfinished, "purchased_unfinished": this_year_purchases_unfinished,
"unfinished_purchases_percent": unfinished_purchases_percent, "unfinished_purchases_percent": this_year_purchases_unfinished_percent,
"refunded_percent": int( "refunded_percent": int(
safe_division( safe_division(
all_purchased_refunded_this_year.count(), this_year_purchases_refunded.count(),
all_purchased_this_year.count(), this_year_purchases_with_currency.count(),
) )
* 100 * 100
), ),
"all_purchased_refunded_this_year": all_purchased_refunded_this_year, "all_purchased_refunded_this_year": this_year_purchases_refunded,
"all_purchased_this_year": all_purchased_this_year, "all_purchased_this_year": this_year_purchases_with_currency.order_by(
"date_purchased"
),
"backlog_decrease_count": backlog_decrease_count, "backlog_decrease_count": backlog_decrease_count,
} }