From 033c6b828bcefb8f146797b26cdbaff0b88cd15d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Denis=20Rodrigues?= <joao.denisr@gmail.com>
Date: Mon, 7 Aug 2017 10:00:11 -0300
Subject: [PATCH] Cache student + merge_dcits

---
 src/script.py           | 28 +++++++++++++++++++---------
 src/student/analysis.py | 18 +++++++-----------
 src/utils/data.py       | 17 +++++++++++------
 3 files changed, 37 insertions(+), 26 deletions(-)

diff --git a/src/script.py b/src/script.py
index 384b885..0338532 100644
--- a/src/script.py
+++ b/src/script.py
@@ -67,8 +67,14 @@ def generate_data():
 def generate_degree_data(degree, path):
     print("Fazendo analises do Curso - {}".format(degree.name))
     average_grad = average_graduation(degree) # media_formandos
-    dic = merge_dicts(graph_average_ira(degree), graph_average_ira_evasion_semester(degree), graph_average_ira_graduation(degree), ['average_ira', 'semester_evasion', 'graduation'])
 
+    dic = merge_dicts(
+         ['average_ira', 'semester_evasion', 'graduation'],
+         graph_average_ira(degree),
+         graph_average_ira_evasion_semester(degree),
+         graph_average_ira_graduation(degree)
+    )
+    
     degree_data = {
         'time_graduation': average_time_graduation_degree(degree),
         'graduation_rate': average_grad[0],
@@ -104,11 +110,15 @@ def generate_student_data(degree, path):
         student_klasses = StudentKlass.objects.filter(student=student)
         amount_courses_semester = get_student_courses_completed(student_klasses)
         failures_semester = semester_pass_rate(student)
-#        failures_amount_courses_semester = merge_dicts(failures_semester, amount_courses_semester)
+        failures_amount_courses_semester = merge_dicts(
+            ['reprovacoes', 'cursadas'],
+            failures_semester,
+            amount_courses_semester,
+        )
 
         ira_courses = sorted(ira_amount_courses(student).items())
-        pass_rate = pass_rate(student_klasses)
-        pass_rate_semester = sorted(failures_semester.items())
+        pass_rate = calculate_pass_rate(student_klasses)
+        pass_rate_semester = sorted(failures_amount_courses_semester.items())
         position = sorted(get_student_position(student).items())
         real_period = get_real_period(student)
         intended_period = get_intended_period(student)
@@ -118,12 +128,12 @@ def generate_student_data(degree, path):
         dict_position = {}
         dict_pass = {}
 
-        for item, course_pass, pos in zip(ira_courses, pass_rate_semester, position):
+        for item, course_pass, pos in zip(ira_courses,pass_rate_semester, position):
             ca = list(course_pass)
             i = list(item)
             p = list(pos)
-            d_pass, d_done = ap[1]
-            date = ap[0].split('/')
+            d_pass, d_done = ca[1]
+            date = ca[0].split('/')
 
             semester_data = {}
             data = '{}/{}'.format(date[0], date[1])
@@ -157,7 +167,7 @@ def generate_student_data(degree, path):
         }
 
         counter += 1
-        with io.open(student_path + '/' + student.grr + '.json', 'w', encondig = 'utf8') as output:
+        with io.open(student_path + '/' + student.grr + '.json', 'w', encoding = 'utf8') as output:
             str_ = json.dumps(student_data, indent = 3, sort_keys = True,
                 separators=(',', ': '), ensure_ascii = False)
             output.write(to_unicode(str_))
@@ -177,7 +187,7 @@ def generate_admission_list_data(degree, path):
     return
 
 def generate_course_data(degree, path):
-    print("\t - Fazendo analises das disciplinas")
+    print("\t- Fazendo analises das disciplinas")
     return
 
 def generate_course_general_data(degree, path):
diff --git a/src/student/analysis.py b/src/student/analysis.py
index ce1e909..8d83815 100644
--- a/src/student/analysis.py
+++ b/src/student/analysis.py
@@ -16,12 +16,11 @@ def pass_amount(student_klasses): # calcular_indice_aprovacao
             if student_klass.situation in SITUATION_PASS:
                 amount_pass += 1
 
-
     return -1 if amount_courses == 0 else amount_pass
 
-def pass_rate(student_klasses): # indice_aprovacao
+def calculate_pass_rate(student_klasses): # indice_aprovacao
     amount_pass = 0
-    amout_courses = 0
+    amount_courses = 0
     for sk in student_klasses:
         if sk.situation in SITUATIONS_COURSE_COMPLETED:
             amount_courses += 1
@@ -31,22 +30,19 @@ def pass_rate(student_klasses): # indice_aprovacao
 
 def semester_pass_rate(student): # calcular_indice_aprovacao_semestral
     index = {}
+
     amount_semesters = student.get_time_in_degree()
     year = student.admission.year
     semester = student.admission.semester 
 
     for i in range(0, amount_semesters):
-        #semester_student_klass = student.studentklass_set.filter(klass__year = year, klass__semester = semester
-        #semester_index = pass_amount
         semester_index = student.studentklass_set.filter(klass__year = year, klass__semester = semester, situation__in = SITUATION_PASS).count()
-
         if semester_index > 0:
             key = "{}/{}".format(year, semester)
             index[key] = semester_index
 
         semester = (semester % 2) + 1
         semester += semester % 2
-
     return index
 
 def get_student_courses_completed(student_klasses):
@@ -129,7 +125,7 @@ def get_student_position(student):
 
     positions = {semester: value['position'] / value['amount_student'] for semester, value in positions.items()}
 
-    return position
+    return positions
 
 def ira_amount_courses(student):
     ira_semesters = get_ira_semester(student)
@@ -145,15 +141,15 @@ def ira_amount_courses(student):
 
 def get_real_period(student, last_period = None):
     if last_period is None:
-        last_period = student.curriculum.get_amount_of_semesters()
+        last_period = student.current_curriculum.get_amount_of_semesters()
 
     real_period = 0
     period_completed = True
     while period_completed and real_period <= last_period:
         real_period += 1
+        courses_period = student.current_curriculum.courses.filter(coursecurriculum__period = real_period)
+        courses_passed_period = StudentKlass.objects.filter(klass__course__in = courses_period, student = student, situation__in = SITUATION_CONCLUDED)
 
-        courses_period = student.current_grade.courses.filter(coursecurriculum__period = real_period)
-        courses_passed_period = courses_period.filter(klass__studentklass__student = student, klass__studentkass__situation__in = SITUATION_CONCLUDED)
         period_completed = len(courses_passed_period) == len(courses_period)
 
     if real_period > last_period:
diff --git a/src/utils/data.py b/src/utils/data.py
index dae3ab0..32a9082 100644
--- a/src/utils/data.py
+++ b/src/utils/data.py
@@ -23,7 +23,7 @@ SITUATION_CONCLUDED = ( # Situacoes concluidas
 )
 
 SITUATION_PASS = ( # Situacoes aprovacao
-    u'Aprovado'
+    u'Aprovado',
     u'Aprovado Adiantamento',
     u'Equivalência de Disciplinas',
     u'Aprovado Conhecimento',
@@ -90,11 +90,16 @@ SITUATION_FAILURE_COMPLETED = ( # Situacoes reprovacao cursada ate o fim
 def difference_between_semesters(year_start, semester_start, year_end, semester_end):
     return 2 * (year_end - year_start) + (semester_end - semester_start) + 1
 
-def merge_dicts(dict1, dict2, dict3, keys):
+def merge_dicts(keys, *args):
     dict_out = {}
-    for key, value in dict1.items():
-        v2 = dict2[key] if key in dict2 else None
-        v3 = dict3[key] if key in dict3 else None
-        dict_out[key] = {keys[0]: value, keys[1]: v2, keys[2]: v3}
+    size = len(args)
+
+    for key, value in args[0].items():
+        values = []
+        values.append(value)
+        for i in range(1, size):
+            values.append(args[i][key] if key in args[i] else None)
+
+        dict_out[key] = {keys[i]: values[i] for i in range(0, size)}
 
     return dict_out
-- 
GitLab