From 7ff0a9b4898e36069aca011fba0a0a1f353327de Mon Sep 17 00:00:00 2001 From: moanos Date: Fri, 3 Oct 2025 18:33:03 +0200 Subject: [PATCH] feat: Add stats --- src/fellchensammlung/tools/metrics.py | 32 +++++++++++++++++++++++++-- src/fellchensammlung/views.py | 13 ++--------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/fellchensammlung/tools/metrics.py b/src/fellchensammlung/tools/metrics.py index 32f6756..162d6eb 100644 --- a/src/fellchensammlung/tools/metrics.py +++ b/src/fellchensammlung/tools/metrics.py @@ -1,4 +1,22 @@ -from fellchensammlung.models import User, AdoptionNotice, AdoptionNoticeStatusChoices +from datetime import timedelta + +from django.utils import timezone + +from fellchensammlung.models import User, AdoptionNotice, AdoptionNoticeStatusChoices, Animal, RescueOrganization + + +def get_rescue_org_check_stats(): + timeframe = timezone.now().date() - timedelta(days=14) + num_rescue_orgs_to_check = RescueOrganization.objects.filter(exclude_from_check=False).filter( + last_checked__lt=timeframe).count() + num_rescue_orgs_checked = RescueOrganization.objects.filter(exclude_from_check=False).filter( + last_checked__gte=timeframe).count() + + try: + percentage_checked = 100 * num_rescue_orgs_checked / (num_rescue_orgs_to_check + num_rescue_orgs_checked) + except ZeroDivisionError: + percentage_checked = 100 + return num_rescue_orgs_to_check, num_rescue_orgs_checked, percentage_checked def gather_metrics_data(): @@ -32,6 +50,10 @@ def gather_metrics_data(): active_animals_per_sex[sex] = number_of_animals active_animals += number_of_animals + num_animal_shelters = RescueOrganization.objects.all().count() + + num_rescue_orgs_to_check, num_rescue_orgs_checked, percentage_checked = get_rescue_org_check_stats() + data = { 'users': num_user, 'staff': num_staff, @@ -45,6 +67,12 @@ def gather_metrics_data(): }, 'adoption_notices_without_location': adoption_notices_without_location, 'active_animals': active_animals, - 'active_animals_per_sex': active_animals_per_sex + 'active_animals_per_sex': active_animals_per_sex, + 'rescue_organizations': num_animal_shelters, + 'rescue_organization_check': { + 'rescue_orgs_to_check': num_rescue_orgs_to_check, + 'rescue_orgs_checked': num_rescue_orgs_checked, + 'percentage_checked': percentage_checked, + } } return data diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index 4400fe9..121def5 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -30,7 +30,7 @@ from .models import Language, Announcement from .tools import i18n, img from .tools.fedi import post_an_to_fedi from .tools.geo import GeoAPI, zoom_level_for_radius -from .tools.metrics import gather_metrics_data +from .tools.metrics import gather_metrics_data, get_rescue_org_check_stats from .tools.admin import clean_locations, get_unchecked_adoption_notices, deactivate_unchecked_adoption_notices, \ deactivate_404_adoption_notices, send_test_email from .tasks import post_adoption_notice_save @@ -870,16 +870,7 @@ def rescue_organization_check(request, context=None): org.id: RescueOrgInternalComment(instance=org) for org in rescue_orgs_to_comment } - timeframe = timezone.now().date() - timedelta(days=14) - num_rescue_orgs_to_check = RescueOrganization.objects.filter(exclude_from_check=False).filter( - last_checked__lt=timeframe).count() - num_rescue_orgs_checked = RescueOrganization.objects.filter(exclude_from_check=False).filter( - last_checked__gte=timeframe).count() - - try: - percentage_checked = 100 * num_rescue_orgs_checked / (num_rescue_orgs_to_check + num_rescue_orgs_checked) - except ZeroDivisionError: - percentage_checked = 100 + num_rescue_orgs_to_check, num_rescue_orgs_checked, percentage_checked = get_rescue_org_check_stats() context["rescue_orgs_to_check"] = rescue_orgs_to_check context["rescue_orgs_last_checked"] = rescue_orgs_last_checked