From b08ceda706e635213a2671ec976802f954432d40 Mon Sep 17 00:00:00 2001
From: Michael Bauer <michael-bauer@posteo.de>
Date: Wed, 8 May 2024 17:50:48 +0200
Subject: [PATCH] Implement statistics query types

Using count wrapper types to avoid duplicating the counted types.
---
 .../coursebook/statistics/statistics.graphql  | 26 +++++++++++--------
 aleksis/apps/alsijil/schema/__init__.py       |  4 +++
 aleksis/apps/alsijil/schema/statistics.py     | 19 ++++++++++++++
 3 files changed, 38 insertions(+), 11 deletions(-)
 create mode 100644 aleksis/apps/alsijil/schema/statistics.py

diff --git a/aleksis/apps/alsijil/frontend/components/coursebook/statistics/statistics.graphql b/aleksis/apps/alsijil/frontend/components/coursebook/statistics/statistics.graphql
index af610d68d..266702ea6 100644
--- a/aleksis/apps/alsijil/frontend/components/coursebook/statistics/statistics.graphql
+++ b/aleksis/apps/alsijil/frontend/components/coursebook/statistics/statistics.graphql
@@ -2,23 +2,27 @@ fragment statistics on StatisticsByPersonType {
   participationCount
   absenceCount
   absenceReasons {
-    id
-    shortName
-    name
-    colour
-    countAsAbsent
     count
+    absenceReason {
+      id
+      shortName
+      name
+      colour
+      countAsAbsent
+    }
   }
   tardinessSum
   tardinessCount
   extraMarks {
-    id
-    shortName
-    name
-    colourFg
-    colourBg
-    showInCourseBook
     count
+    extraMark {
+      id
+      shortName
+      name
+      colourFg
+      colourBg
+      showInCourseBook
+    }
   }
 }
 
diff --git a/aleksis/apps/alsijil/schema/__init__.py b/aleksis/apps/alsijil/schema/__init__.py
index 46174ddb7..6ac95c44e 100644
--- a/aleksis/apps/alsijil/schema/__init__.py
+++ b/aleksis/apps/alsijil/schema/__init__.py
@@ -22,6 +22,7 @@ from .absences import (
     LessonsForPersonType,
     AbsencesBatchCreateMutation,
 )
+from .statistics import StatisticsByPersonType
 
 class Query(graphene.ObjectType):
     documentations = FilterOrderList(DocumentationType)
@@ -48,6 +49,9 @@ class Query(graphene.ObjectType):
         end=graphene.Date(required=True),
     )
 
+    statistics_by_person = graphene.Field(StatisticsByPersonType)
+    statistics_by_group = graphene.List(StatisticsByPersonType)
+
     def resolve_documentations_by_course_id(root, info, course_id, **kwargs):
         documentations = Documentation.objects.filter(
             Q(course__pk=course_id) | Q(amends__course__pk=course_id)
diff --git a/aleksis/apps/alsijil/schema/statistics.py b/aleksis/apps/alsijil/schema/statistics.py
new file mode 100644
index 000000000..ed9fff17b
--- /dev/null
+++ b/aleksis/apps/alsijil/schema/statistics.py
@@ -0,0 +1,19 @@
+import graphene
+from aleksis.apps.kolego.schema.absence import AbsenceReasonType
+from .personal_note import ExtraMarkType
+
+class AbsenceReasonWithCountType(graphene.ObjectType):
+    absence_reason = graphene.Field(AbsenceReasonType)
+    count = graphene.Int()
+
+class ExtraMarkWithCountType(graphene.ObjectType):
+    extra_mark = graphene.Field(ExtraMarkType)
+    count = graphene.Int()
+
+class StatisticsByPersonType(graphene.ObjectType):
+    participation_count = graphene.Int()
+    absence_count = graphene.Int()
+    absence_reasons = graphene.List(AbsenceReasonWithCountType)
+    tardiness_sum = graphene.Int()
+    tardiness_count = graphene.Int()
+    extra_marks = graphene.List(ExtraMarkWithCountType)
-- 
GitLab