Add command for company stats

This commit is contained in:
Christian Cueni 2024-07-30 14:03:45 +02:00
parent a024a2f244
commit 9603b37196
1 changed files with 30 additions and 0 deletions

View File

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