From 9603b37196e7aaa3a10180030dfb18e2c220acf3 Mon Sep 17 00:00:00 2001 From: Christian Cueni Date: Tue, 30 Jul 2024 14:03:45 +0200 Subject: [PATCH] Add command for company stats --- .../management/commands/count_vv_members.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 server/vbv_lernwelt/course_session/management/commands/count_vv_members.py diff --git a/server/vbv_lernwelt/course_session/management/commands/count_vv_members.py b/server/vbv_lernwelt/course_session/management/commands/count_vv_members.py new file mode 100644 index 00000000..35b62be1 --- /dev/null +++ b/server/vbv_lernwelt/course_session/management/commands/count_vv_members.py @@ -0,0 +1,30 @@ +import djclick as click +import structlog +from django.db.models import Count + +from vbv_lernwelt.course.models import CourseSessionUser + +logger = structlog.get_logger(__name__) + + +@click.command() +def command(): + VV_COURSE_SESSIONS = [1, 2, 3] # DE, FR, IT + + # Aggregation of users per organisation for a specific course session + user_counts = ( + CourseSessionUser.objects.filter(course_session__id__in=VV_COURSE_SESSIONS) + .values("user__organisation__organisation_id", "user__organisation__name_de") + .annotate(user_count=Count("id")) + .order_by("user__organisation__organisation_id") + ) + + for entry in user_counts: + print( + f"Organisation Name: {entry['user__organisation__name_de']}, " + f"User Count: {entry['user_count']}" + ) + + print( + f"Total number of users: {sum([entry['user_count'] for entry in user_counts])}" + )