From d25eb1089d9a64518167a2db8db040d1dbf1250f Mon Sep 17 00:00:00 2001
From: janmax <j.michal@stud.uni-goettingen.de>
Date: Sat, 10 Feb 2018 23:53:05 +0100
Subject: [PATCH] A very simple base for an endpoint that displays basic stats

---
 core/models.py             |  2 +-
 core/urls.py               |  1 +
 core/views/common_views.py | 20 ++++++++++++++++++++
 3 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/core/models.py b/core/models.py
index 6572cc2b..f4d8b26b 100644
--- a/core/models.py
+++ b/core/models.py
@@ -133,7 +133,7 @@ class SubmissionType(models.Model):
                     Case(
                         When(
                             Q(submissions__feedback__isnull=False) &
-                            Q(submissions__feedback__status=Feedback.ACCEPTED),
+                            Q(submissions__feedback__is_final=True),
                             then=Value(1)), output_field=IntegerField(),
                     )
                 )
diff --git a/core/urls.py b/core/urls.py
index 61a0cfc5..d47a1085 100644
--- a/core/urls.py
+++ b/core/urls.py
@@ -17,6 +17,7 @@ router.register('tutor', views.TutorApiViewSet, base_name='tutor')
 router.register('subscription', views.SubscriptionApiViewSet,
                 base_name='subscription')
 router.register('assignment', views.AssignmentApiViewSet)
+router.register('statistics', views.StatisticsEndpoint, base_name='statistics')
 
 # regular views that are not viewsets
 regular_views_urlpatterns = [
diff --git a/core/views/common_views.py b/core/views/common_views.py
index 7c353592..da6ab82c 100644
--- a/core/views/common_views.py
+++ b/core/views/common_views.py
@@ -4,6 +4,7 @@ user to be authenticated and most are only accessible by one user group """
 import logging
 
 from django.conf import settings
+from django.db.models import Avg
 from rest_framework import generics, mixins, viewsets
 from rest_framework.decorators import api_view
 from rest_framework.response import Response
@@ -84,6 +85,25 @@ class SubmissionTypeApiView(viewsets.ReadOnlyModelViewSet):
     serializer_class = SubmissionTypeSerializer
 
 
+class StatisticsEndpoint(viewsets.ViewSet):
+
+    def list(self, request, *args, **kwargs):
+        return Response({
+            'submissions_per_type':
+            models.SubmissionType.objects.first().submissions.count(),
+
+            'submissions_per_student':
+            models.SubmissionType.objects.count(),
+
+            'current_mean_score':
+            models.Feedback.objects.aggregate(avg=Avg('score')).get('avg', 0),
+
+            'submission_type_progress':
+                models.SubmissionType.get_annotated_feedback_count().values(
+                    'feedback_count', 'pk', 'percentage')
+        })
+
+
 class SubmissionViewSet(viewsets.ReadOnlyModelViewSet):
     permission_classes = (IsTutorOrReviewer, )
     serializer_class = SubmissionNoTypeSerializer
-- 
GitLab