diff --git a/training_content/service.py b/training_content/service.py index 480b773..c543d44 100644 --- a/training_content/service.py +++ b/training_content/service.py @@ -1,3 +1,4 @@ +import json from datetime import datetime from logging import getLogger @@ -42,8 +43,10 @@ class TrainingContentService: **weak_areas } doc_ref = self._db.collection('training').add(training_doc) + return { "id": doc_ref[1].id + # "id": "ZYSDZPk4eUqxhMm4Oz7L" } @staticmethod @@ -70,7 +73,6 @@ class TrainingContentService: tips = {"tips": []} for query in queries: - print(f"{query.category} {query.text}") if query.category == "words": tips["tips"].extend( self._training_content_module.query_knowledge_base(query.text, "word_link") @@ -104,7 +106,8 @@ class TrainingContentService: ' with sentence structure and punctuation.", the "queries" field is where you will write queries ' 'for tips that will be displayed to the student, the category attribute is a collection of ' 'embeddings and the text will be the text used to query the knowledge base. The categories are ' - f'the following [{", ".join(self.TOOLS)}].' + f'the following [{", ".join(self.TOOLS)}]. The exam data will be a json where the key of the field ' + '"exams" is the exam id, an exam can be composed of multiple modules or single modules.' ) }, { @@ -150,42 +153,60 @@ class TrainingContentService: def _sort_out_solutions(self, stats): grouped_stats = {} for stat in stats: - exam_id = stat["exam"] + session_key = f'{str(stat["date"])}-{stat["user"]}' module = stat["module"] - if module not in grouped_stats: - grouped_stats[module] = {} - if exam_id not in grouped_stats[module]: - grouped_stats[module][exam_id] = [] - grouped_stats[module][exam_id].append(stat) + exam_id = stat["exam"] + + if session_key not in grouped_stats: + grouped_stats[session_key] = {} + if module not in grouped_stats[session_key]: + grouped_stats[session_key][module] = { + "stats": [], + "exam_id": exam_id + } + grouped_stats[session_key][module]["stats"].append(stat) exercises = {} exam_map = {} - for module, exams in grouped_stats.items(): - exercises[module] = {} - for exam_id, stat_group in exams.items(): - exam = self._get_doc_by_id(module, exam_id) - exercises[module][exam_id] = {"date": None, "exercises": [], "score": None} + for session_key, modules in grouped_stats.items(): + exercises[session_key] = {} + for module, module_stats in modules.items(): + exercises[session_key][module] = {} + + exam_id = module_stats["exam_id"] + if exam_id not in exercises[session_key][module]: + exercises[session_key][module][exam_id] = {"date": None, "exercises": []} + exam_total_questions = 0 exam_total_correct = 0 - for stat in stat_group: + + for stat in module_stats["stats"]: exam_total_questions += stat["score"]["total"] exam_total_correct += stat["score"]["correct"] - exercises[module][exam_id]["date"] = stat["date"] + exercises[session_key][module][exam_id]["date"] = stat["date"] - if exam_id not in exam_map: - exam_map[exam_id] = {"stat_ids": [], "score": 0} - exam_map[exam_id]["stat_ids"].append(stat["id"]) + if session_key not in exam_map: + exam_map[session_key] = {"stat_ids": [], "score": 0} + exam_map[session_key]["stat_ids"].append(stat["id"]) + exam = self._get_doc_by_id(module, exam_id) if module == "listening": - exercises[module][exam_id]["exercises"].extend(self._get_listening_solutions(stat, exam)) - if module == "reading": - exercises[module][exam_id]["exercises"].extend(self._get_reading_solutions(stat, exam)) - if module == "writing": - exercises[module][exam_id]["exercises"].extend(self._get_writing_prompts_and_answers(stat, exam)) + exercises[session_key][module][exam_id]["exercises"].extend( + self._get_listening_solutions(stat, exam)) + elif module == "reading": + exercises[session_key][module][exam_id]["exercises"].extend( + self._get_reading_solutions(stat, exam)) + elif module == "writing": + exercises[session_key][module][exam_id]["exercises"].extend( + self._get_writing_prompts_and_answers(stat, exam)) + elif module == "level": # same structure as listening + exercises[session_key][module][exam_id]["exercises"].extend( + self._get_listening_solutions(stat, exam)) - exam_map[exam_id]["score"] = round((exam_total_correct / exam_total_questions) * 100) - exam_map[exam_id]["module"] = module - return exercises, exam_map + exam_map[session_key]["score"] = round((exam_total_correct / exam_total_questions) * 100) + exam_map[session_key]["module"] = module + + return {"exams": exercises}, exam_map def _get_writing_prompts_and_answers(self, stat, exam): result = [] @@ -258,8 +279,13 @@ class TrainingContentService: "solutions": exercise["solutions"], "answer": stat["solutions"] }) - else: - # match_sentences + elif stat["type"] == "trueFalse": + result.append({ + "text": text, + "questions": exercise["questions"], + "answer": stat["solutions"] + }) + elif stat["type"] == "matchSentences": result.append({ "text": text, "question": exercise["prompt"],