From 5898fbf86dd46af95cd6339a856846f255d478e3 Mon Sep 17 00:00:00 2001 From: moanos Date: Sun, 20 Jul 2025 15:43:59 +0200 Subject: [PATCH] feat: Add number of animals per sex to metrics --- src/fellchensammlung/models.py | 2 +- src/fellchensammlung/tools/metrics.py | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index cdead1f..13f783d 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -360,7 +360,7 @@ class AdoptionNotice(models.Model): def num_per_sex(self): num_per_sex = dict() for sex in SexChoices: - num_per_sex[sex] = self.animals.filter(sex=sex).count + num_per_sex[sex] = self.animals.filter(sex=sex).count() return num_per_sex @property diff --git a/src/fellchensammlung/tools/metrics.py b/src/fellchensammlung/tools/metrics.py index 71b1af2..1175722 100644 --- a/src/fellchensammlung/tools/metrics.py +++ b/src/fellchensammlung/tools/metrics.py @@ -8,8 +8,9 @@ def gather_metrics_data(): """Adoption notices""" num_adoption_notices = AdoptionNotice.objects.count() - num_adoption_notices_active = AdoptionNotice.objects.filter( - adoptionnoticestatus__major_status=AdoptionNoticeStatus.ACTIVE).count() + adoption_notices_active = AdoptionNotice.objects.filter( + adoptionnoticestatus__major_status=AdoptionNoticeStatus.ACTIVE) + num_adoption_notices_active = adoption_notices_active.count() num_adoption_notices_closed = AdoptionNotice.objects.filter( adoptionnoticestatus__major_status=AdoptionNoticeStatus.CLOSED).count() num_adoption_notices_disabled = AdoptionNotice.objects.filter( @@ -18,6 +19,19 @@ def gather_metrics_data(): adoptionnoticestatus__major_status=AdoptionNoticeStatus.AWAITING_ACTION).count() adoption_notices_without_location = AdoptionNotice.objects.filter(location__isnull=True).count() + + active_animals = 0 + active_animals_per_sex = {} + for adoption_notice in adoption_notices_active: + nps = adoption_notice.num_per_sex + for sex in nps: + number_of_animals = nps[sex] + try: + active_animals_per_sex[sex] += number_of_animals + except KeyError: + active_animals_per_sex[sex] = number_of_animals + active_animals += number_of_animals + data = { 'users': num_user, 'staff': num_staff, @@ -29,6 +43,8 @@ def gather_metrics_data(): 'disabled': num_adoption_notices_disabled, 'awaiting_action': num_adoption_notices_awaiting_action, }, - 'adoption_notices_without_location': adoption_notices_without_location + 'adoption_notices_without_location': adoption_notices_without_location, + 'active_animals': active_animals, + 'active_animals_per_sex': active_animals_per_sex } return data