From 4e239015980d04249e0daa514f73db4bd08bd018 Mon Sep 17 00:00:00 2001 From: Anna Lorenz <127238592+annalorenz123@users.noreply.github.com> Date: Wed, 23 Oct 2024 21:39:55 +0200 Subject: [PATCH] Small improvements on statistics page (#3820) * Fixed messy pizza chart to top 10 and added pie chart top 10 borrel orders * Removed unnecessary line * Update website/members/views.py --------- Co-authored-by: Dirk Doesburg --- website/members/services.py | 9 ++++--- .../members/templates/members/statistics.html | 10 ++++++-- website/members/views.py | 4 +++ website/pizzas/services.py | 14 ++++++----- website/sales/services.py | 25 ++++++++++++++++++- 5 files changed, 49 insertions(+), 13 deletions(-) diff --git a/website/members/services.py b/website/members/services.py index 30281ace0..e5ef12f61 100644 --- a/website/members/services.py +++ b/website/members/services.py @@ -123,8 +123,8 @@ def gen_stats_member_type() -> dict[str, list]: def gen_stats_year() -> dict[str, list]: - """Generate statistics on how many members (and other membership types) there were in each cohort.""" - years = range(2015, datetime_to_lectureyear(date.today())) + """Generate statistics on how many members (and other membership types) there were in each year.""" + years = range(2015, datetime_to_lectureyear(date.today()) + 1) data = { "labels": list(years), @@ -137,9 +137,10 @@ def gen_stats_year() -> dict[str, list]: for index, (key, _) in enumerate(Membership.MEMBERSHIP_TYPES): for year in years: data["datasets"][index]["data"].append( - Membership.objects.filter(since__lte=date(year=year, month=9, day=1)) + Membership.objects.filter(since__lte=date(year=year, month=10, day=1)) .filter( - Q(until__isnull=True) | Q(until__gt=date(year=year, month=9, day=1)) + Q(until__isnull=True) + | Q(until__gt=date(year=year, month=10, day=1)) ) .filter(type=key) .count() diff --git a/website/members/templates/members/statistics.html b/website/members/templates/members/statistics.html index d5d8ae110..826ad29f3 100644 --- a/website/members/templates/members/statistics.html +++ b/website/members/templates/members/statistics.html @@ -20,7 +20,7 @@

{% trans "Total amount of Thalia members" %}: {{ total_members
@@ -46,10 +46,16 @@

{% trans "Total amount of Thalia members" %}: {{ total_members
+
+ +
dict: "total_pizza_orders": json.dumps( pizzas.services.gen_stats_pizza_orders() ), + "total_sales_orders": json.dumps( + sales.services.gen_stats_sales_orders() + ), "active_members": json.dumps(services.gen_stats_active_members()), } ) diff --git a/website/pizzas/services.py b/website/pizzas/services.py index f00899d52..a77211db3 100644 --- a/website/pizzas/services.py +++ b/website/pizzas/services.py @@ -1,3 +1,4 @@ +from django.db.models import Count from django.utils import timezone from events.services import is_organiser @@ -14,12 +15,13 @@ def gen_stats_pizza_orders() -> dict: ], } - for product in Product.objects.all(): - orders = FoodOrder.objects.filter(product=product).count() - - if orders > 0: - data["labels"].append(product.name) - data["datasets"][0]["data"].append(orders) + for product in ( + Product.objects.annotate(count=Count("foodorder")) + .filter(count__gt=0) + .order_by("-count")[:10] + ): + data["labels"].append(product.name) + data["datasets"][0]["data"].append(product.count) return data diff --git a/website/sales/services.py b/website/sales/services.py index 696dcc3cd..5d9261990 100644 --- a/website/sales/services.py +++ b/website/sales/services.py @@ -1,6 +1,7 @@ +from django.db.models import Sum from django.utils import timezone -from sales.models.order import Order +from sales.models.order import Order, OrderItem def is_adult(member): @@ -33,3 +34,25 @@ def execute_data_minimisation(dry_run=False): if not dry_run: queryset.update(payer=None) return queryset.all() + + +def gen_stats_sales_orders() -> dict: + """Generate statistics about number of orders per product.""" + data = { + "labels": [], + "datasets": [ + {"data": []}, + ], + } + + for product, count in ( + OrderItem.objects.values("product_name") + .annotate(count=Sum("amount")) + .filter(count__gt=0) + .order_by("-count") + .values_list("product_name", "count")[:10] + ): + data["labels"].append(product) + data["datasets"][0]["data"].append(count) + + return data