Skip to content

Commit

Permalink
feat: Base the days for the average per day calculation on the day of…
Browse files Browse the repository at this point in the history
… the start of drinking until today
  • Loading branch information
drikusroor committed Oct 10, 2024
1 parent e8eb621 commit 7f4c63d
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/drinks/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from datetime import date
from rest_framework import viewsets
from rest_framework import serializers
from django.utils import timezone
Expand All @@ -9,7 +10,7 @@
from rest_framework.response import Response
from django.db.models import Sum
from django.views.generic import TemplateView
from django.db.models import Count, Avg
from django.db.models import Count
from django.db.models.functions import TruncDate
from django.core.serializers.json import DjangoJSONEncoder

Expand Down Expand Up @@ -212,13 +213,23 @@ def get_context_data(self, **kwargs):
drinks_with_location, cls=CustomJSONEncoder
)

# TODO: Improve this by basing the amount of days on the first consumption's day
# old_start_date = ItineraryStop.objects.order_by("start_date").first().start_date
start_date = (
DrinkConsumption.objects.order_by("date")
.annotate(date_day=TruncDate("date"))
.first()
.date_day
)
today_date = date.today()
total_days_amount = (today_date - start_date).days + 1

print(total_days_amount)

# Average drinks per day per type
avg_drinks = (
DrinkType.objects.all()
.annotate(
avg_per_day=Sum("drink_consumptions__amount")
/ Count(TruncDate("drink_consumptions__date"), distinct=True)
)
.annotate(avg_per_day=Sum("drink_consumptions__amount") / total_days_amount)
.order_by("-avg_per_day")
)
context["avg_drinks_per_day"] = list(avg_drinks)
Expand Down

0 comments on commit 7f4c63d

Please sign in to comment.