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])}" + )