From faec85d90b10388f4702dc397c1df7e4dfe502ab Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Thu, 11 Mar 2021 13:12:02 +0100
Subject: [PATCH] statistics are computed using new SubjectTye structure

---
 smash/web/statistics.py            | 18 ++++++++++--------
 smash/web/tests/test_statistics.py | 15 ++++++++++-----
 2 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/smash/web/statistics.py b/smash/web/statistics.py
index 7d9d944b..8509ac31 100644
--- a/smash/web/statistics.py
+++ b/smash/web/statistics.py
@@ -7,7 +7,7 @@ from operator import attrgetter
 from django.db import connection
 from django.db.models import Q, Count
 from web.migration_functions import is_sqlite_db
-from .models import AppointmentType, Appointment, Visit
+from .models import AppointmentType, Appointment, Visit, SubjectType
 
 __author__ = 'Valentin Grouès'
 
@@ -82,7 +82,7 @@ class StatisticsManager(object):
                                 self.statuses_list]
         self.visits_ranks = self._get_visits_ranks()
 
-    def get_statistics_for_month(self, month, year, subject_type=None, visit=None):
+    def get_statistics_for_month(self, month, year, subject_type: SubjectType = None, visit=None):
         """
         Build dict with statistics for a given month of a given year.
         Statistics include:
@@ -154,7 +154,7 @@ class StatisticsManager(object):
             query = QUERY_APPOINTMENTS
             subject_type_clause = ""
             if subject_type is not None:
-                subject_type_clause = " AND web_studysubject.type = '{}'".format(subject_type)
+                subject_type_clause = " AND web_studysubject.type_id = '{}'".format(subject_type.id)
             query = query.format(subject_type_clause)
             with connection.cursor() as cursor:
                 cursor.execute(query, [visit, month, year])
@@ -174,10 +174,10 @@ class StatisticsManager(object):
         return results_appointments
 
     @staticmethod
-    def _get_count_from_filters_or_sql(model, filters, query, visit, month, year, subject_type):
+    def _get_count_from_filters_or_sql(model, filters, query, visit, month, year, subject_type: SubjectType):
         if visit:
             if subject_type is not None:
-                query += " AND web_studysubject.type = '{}'".format(subject_type)
+                query += " AND web_studysubject.type_id = '{}'".format(subject_type.id)
             with connection.cursor() as cursor:
                 cursor.execute(
                     query,
@@ -188,15 +188,17 @@ class StatisticsManager(object):
             count = model.objects.filter(filters).count()
         return count
 
-    def _get_number_visits_started(self, filters_month_year_visits_started, visit, month, year, subject_type=None):
+    def _get_number_visits_started(self, filters_month_year_visits_started, visit, month, year,
+                                   subject_type: SubjectType = None):
         return self._get_count_from_filters_or_sql(Visit, filters_month_year_visits_started, QUERY_VISITS_STARTED_COUNT,
                                                    visit, month, year, subject_type)
 
-    def _get_number_visits_ended(self, filters_month_year_visits_ended, visit, month, year, subject_type=None):
+    def _get_number_visits_ended(self, filters_month_year_visits_ended, visit, month, year,
+                                 subject_type: SubjectType = None):
         return self._get_count_from_filters_or_sql(Visit, filters_month_year_visits_ended, QUERY_VISITS_ENDED_COUNT,
                                                    visit, month, year, subject_type)
 
-    def _get_number_of_appointments(self, filters, visit, month, year, subject_type=None):
+    def _get_number_of_appointments(self, filters, visit, month, year, subject_type: SubjectType = None):
         return self._get_count_from_filters_or_sql(Appointment, filters, QUERY_APPOINTMENTS_COUNT, visit, month, year,
                                                    subject_type)
 
diff --git a/smash/web/tests/test_statistics.py b/smash/web/tests/test_statistics.py
index 3c36dcf7..f48ead71 100644
--- a/smash/web/tests/test_statistics.py
+++ b/smash/web/tests/test_statistics.py
@@ -5,7 +5,8 @@ from django.test import TestCase
 
 from web.models import Visit, AppointmentTypeLink
 from web.statistics import get_previous_year_and_month_for_date, StatisticsManager
-from web.tests.functions import create_appointment, create_appointment_type
+from web.tests.functions import create_appointment, create_appointment_type, get_control_subject_type, \
+    get_patient_subject_type
 from web.views.notifications import get_today_midnight_date
 
 __author__ = 'Valentin Grouès'
@@ -51,18 +52,22 @@ class TestStatistics(TestCase):
         self.check_statistics(statistics, 0, 0, 0, {"C": [0, 0]}, ['Scheduled'])
 
     def test_get_statistics_for_month_one_appointment_subject_type(self):
-        statistics = self.statistics_manager.get_statistics_for_month(self.now.month, self.now.year, subject_type="C")
+        statistics = self.statistics_manager.get_statistics_for_month(self.now.month, self.now.year,
+                                                                      subject_type=get_control_subject_type())
         self.check_statistics(statistics, 0, 0, 1, {"C": [1, 1]}, ['Scheduled'])
 
-        statistics = self.statistics_manager.get_statistics_for_month(self.now.month, self.now.year, subject_type="P")
+        statistics = self.statistics_manager.get_statistics_for_month(self.now.month, self.now.year,
+                                                                      subject_type=get_patient_subject_type())
         self.check_statistics(statistics, 0, 0, 0, {"C": [0, 0]}, ['Scheduled'])
 
     def test_get_statistics_for_month_one_appointment_subject_type_and_visit(self):
-        statistics = self.statistics_manager.get_statistics_for_month(self.now.month, self.now.year, subject_type="C",
+        statistics = self.statistics_manager.get_statistics_for_month(self.now.month, self.now.year,
+                                                                      subject_type=get_control_subject_type(),
                                                                       visit='1')
         self.check_statistics(statistics, 0, 0, 1, {"C": [1, 1]}, ['Scheduled'])
 
-        statistics = self.statistics_manager.get_statistics_for_month(self.now.month, self.now.year, subject_type="P",
+        statistics = self.statistics_manager.get_statistics_for_month(self.now.month, self.now.year,
+                                                                      subject_type=get_patient_subject_type(),
                                                                       visit='1')
         self.check_statistics(statistics, 0, 0, 0, {"C": [0, 0]}, ['Scheduled'])
 
-- 
GitLab