From c38f1d2244e82f5221178162249830d47d1d7f68 Mon Sep 17 00:00:00 2001 From: moanos Date: Thu, 6 Jun 2024 23:16:57 +0200 Subject: [PATCH] feat: Add basic metrics --- src/fellchensammlung/tools/metrics.py | 22 ++++++++++++++++++++++ src/fellchensammlung/urls.py | 1 + src/fellchensammlung/views.py | 8 +++++++- 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/fellchensammlung/tools/metrics.py diff --git a/src/fellchensammlung/tools/metrics.py b/src/fellchensammlung/tools/metrics.py new file mode 100644 index 0000000..69481d6 --- /dev/null +++ b/src/fellchensammlung/tools/metrics.py @@ -0,0 +1,22 @@ +from fellchensammlung.models import User, AdoptionNotice, AdoptionNoticeStatus + + +def gather_metrics_data(): + """USERS""" + num_user = User.objects.count() + num_staff = User.objects.filter(is_staff=True).count() + + """Adoption notices""" + num_adoption_notices = AdoptionNotice.objects.count() + num_adoption_notices_active = AdoptionNotice.objects.filter(adoptionnoticestatus__major_status=AdoptionNoticeStatus.ACTIVE).count() + + adoption_notices_without_location = AdoptionNotice.objects.filter(location__isnull=True).count() + data = { + 'users': num_user, + 'staff': num_staff, + + 'adoption_notices': num_adoption_notices, + 'adoption_notices_active': num_adoption_notices_active, + 'adoption_notices_without_location': adoption_notices_without_location + } + return data diff --git a/src/fellchensammlung/urls.py b/src/fellchensammlung/urls.py index 42a9b7d..949a6c9 100644 --- a/src/fellchensammlung/urls.py +++ b/src/fellchensammlung/urls.py @@ -9,6 +9,7 @@ from . import views urlpatterns = [ path("", views.index, name="index"), path("rss/", LatestAdoptionNoticesFeed(), name="rss"), + path("metrics/", views.metrics, name="metrics"), # ex: /animal/5/ path("tier//", views.animal_detail, name="animal-detail"), # ex: /animal/5/edit diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index f932918..f3ebfb3 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -1,6 +1,6 @@ import logging -from django.http import HttpResponseRedirect +from django.http import HttpResponseRedirect, JsonResponse from django.shortcuts import render, redirect from django.urls import reverse from django.contrib.auth.decorators import login_required @@ -17,6 +17,7 @@ from .forms import AdoptionNoticeForm, ImageForm, ReportAdoptionNoticeForm, Comm AdoptionNoticeSearchForm from .models import Language, Announcement from .tools.geo import GeoAPI +from .tools.metrics import gather_metrics_data def index(request): @@ -298,3 +299,8 @@ def modqueue(request): open_reports = Report.objects.filter(status=Report.WAITING) context = {"reports": open_reports} return render(request, 'fellchensammlung/modqueue.html', context=context) + + +def metrics(request): + data = gather_metrics_data() + return JsonResponse(data)