Skip to content

Commit

Permalink
Small improvements on statistics page (#3820)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
annalorenz123 and DeD1rk authored Oct 23, 2024
1 parent 4034614 commit 4e23901
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
9 changes: 5 additions & 4 deletions website/members/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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()
Expand Down
10 changes: 8 additions & 2 deletions website/members/templates/members/statistics.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ <h2 class="mb-4">{% trans "Total amount of Thalia members" %}: {{ total_members
<div class="col-6 ">
<canvas id="total-year-chart"
data-data="{{ cohort_sizes }}"
data-title="Membership Types (previous cohorts)"
data-title="Membership Types per year"
data-chart-type="bar">
</canvas>
</div>
Expand All @@ -46,10 +46,16 @@ <h2 class="mb-4">{% trans "Total amount of Thalia members" %}: {{ total_members
<div class="col-6">
<canvas id="pizza-total-type-chart"
data-data="{{ total_pizza_orders }}"
data-title="Pizza Orders"
data-title="Top 10 Pizza Orders"
data-chart-type="pie">
</canvas>
</div>
<div class="col-6">
<canvas id="borrel-total-type-chart"
data-data="{{ total_sales_orders }}"
data-title="Top 10 Borrel Orders"
data-chart-type="pie">
</div>
<div class="col-6">
<canvas id="active-members-chart"
data-data="{{ active_members }}"
Expand Down
4 changes: 4 additions & 0 deletions website/members/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import activemembers.services as activemembers_services
import events.services as event_services
import pizzas.services
import sales.services
from members import emails, services
from members.decorators import membership_required
from members.models import EmailChange, Member, Membership, Profile
Expand Down Expand Up @@ -212,6 +213,9 @@ def get_context_data(self, **kwargs) -> 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()),
}
)
Expand Down
14 changes: 8 additions & 6 deletions website/pizzas/services.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.db.models import Count
from django.utils import timezone

from events.services import is_organiser
Expand All @@ -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

Expand Down
25 changes: 24 additions & 1 deletion website/sales/services.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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

0 comments on commit 4e23901

Please sign in to comment.