diff --git a/tools/migrate_abs_to_assiduites.py b/tools/migrate_abs_to_assiduites.py index e89db9459..a2c763a63 100644 --- a/tools/migrate_abs_to_assiduites.py +++ b/tools/migrate_abs_to_assiduites.py @@ -17,7 +17,7 @@ from app.scodoc.sco_utils import ( printProgressBar, ) from datetime import time, datetime, date -from json import dump +from json import dump, dumps class _glob: @@ -28,6 +28,64 @@ class _glob: CURRENT_ETU: list = [] MODULES: list[tuple[int, int]] = [] COMPTE: list[int, int] = [] + ERR_ETU: list[int] = [] + + +class _Statistics: + def __init__(self) -> None: + self.object: dict = {"total": 0} + self.year: int = None + + def __set_year(self, year: int): + if year not in self.object: + self.object[year] = { + "etuds_inexistant": [], + "abs_invalide": {}, + } + self.year = year + return self + + def __add_etud(self, etudid: int): + if etudid not in self.object[self.year]["etuds_inexistant"]: + self.object[self.year]["etuds_inexistant"].append(etudid) + return self + + def __add_abs(self, abs: int, err: str): + if abs not in self.object[self.year]["abs_invalide"]: + self.object[self.year]["abs_invalide"][abs] = [err] + else: + self.object[self.year]["abs_invalide"][abs].append(err) + + return self + + def add_problem(self, abs: Absence, err: str): + abs.jour: date + pivot: date = date(abs.jour.year, 9, 15) + year: int = abs.jour.year + if pivot < abs.jour: + year += 1 + self.__set_year(year) + + if err == "Etudiant inexistant": + self.__add_etud(abs.etudid) + else: + self.__add_abs(abs.id, err) + + self.object["total"] += 1 + + def compute_stats(self) -> dict: + stats: dict = {"total": self.object["total"]} + for year in self.object: + if year == "total": + continue + stats[year] = {} + stats[year]["etuds_inexistant"] = len(self.object[year]["etuds_inexistant"]) + stats[year]["abs_invalide"] = len(self.object[year]["abs_invalide"]) + + return stats + + def export(self, file): + dump(self.object, file, indent=2) def migrate_abs_to_assiduites( @@ -52,6 +110,7 @@ def migrate_abs_to_assiduites( .etudid: relation -> Identite """ Profiler.clear() + stats: _Statistics = _Statistics() time_elapsed: Profiler = Profiler("migration") time_elapsed.start() @@ -86,10 +145,10 @@ def migrate_abs_to_assiduites( _glob.DUPLICATED = [] _glob.DUPLICATIONS_ASSIDUITES = {} _glob.DUPLICATIONS_JUSTIFICATIFS = {} - _glob.PROBLEMS = {} _glob.CURRENT_ETU = [] _glob.MODULES = [] _glob.COMPTE = [0, 0] + _glob.ERR_ETU = [] absences_len: int = absences.count() @@ -109,9 +168,7 @@ def migrate_abs_to_assiduites( db.session.add(generated) _glob.COMPTE[0] += 1 except Exception as e: - if abs.id not in _glob.PROBLEMS: - _glob.PROBLEMS[abs.id] = [] - _glob.PROBLEMS[abs.id].append(e.args[0]) + stats.add_problem(abs, e.args[0]) try: if abs.estjust: @@ -123,9 +180,7 @@ def migrate_abs_to_assiduites( _glob.COMPTE[1] += 1 except Exception as e: - if abs.id not in _glob.PROBLEMS: - _glob.PROBLEMS[abs.id] = [] - _glob.PROBLEMS[abs.id].append(e.args[0]) + stats.add_problem(abs, e.args[0]) if i % 10 == 0: printProgressBar( @@ -160,27 +215,29 @@ def migrate_abs_to_assiduites( "Progression", "effectué", autosize=True, - finish_msg=f"{ProgressBarColors.GREEN}Les absences ont bien été migrées.{ProgressBarColors.RESET}", ) time_elapsed.stop() + + statistiques: dict = stats.compute_stats() print( f"{ProgressBarColors.GREEN}La migration a pris {time_elapsed.elapsed():.2f} secondes {ProgressBarColors.RESET}" ) print( - f"{ProgressBarColors.RED}Il y a eu {len(_glob.PROBLEMS)} absences qui n'ont pas pu être migrée." + f"{ProgressBarColors.RED}{statistiques['total']} absences qui n'ont pas pu être migrée." ) print( - f"Vous retrouverez un fichier json {ProgressBarColors.GREEN}/tmp/scodoc_migration_abs.json{ProgressBarColors.RED} contenant les ids des absences ainsi que les erreurs liées." + f"Vous retrouverez un fichier json {ProgressBarColors.GREEN}/tmp/scodoc_migration_abs.json{ProgressBarColors.RED} contenant les problèmes de migrations" ) with open("/tmp/scodoc_migration_abs.json", "w", encoding="utf-8") as file: - dump(_glob.PROBLEMS, file) + stats.export(file) print( f"{ProgressBarColors.CYAN}{_glob.COMPTE[0]} assiduités et {_glob.COMPTE[1]} justificatifs ont été générés.{ProgressBarColors.RESET}" ) - # afficher nombre justificatifs généré par rapport au nombre de justificatifs + + print(dumps(statistiques, indent=2)) def _from_abs_to_assiduite( @@ -205,7 +262,7 @@ def _from_abs_to_assiduite( ) if duplicata is not None: _glob.DUPLICATED.append(duplicata) - return "Duplicated" + return "Duplicata" desc: str = _abs.description entry_date: datetime = _abs.entry_date @@ -213,7 +270,7 @@ def _from_abs_to_assiduite( if _abs.etudid not in _glob.CURRENT_ETU: etud: Identite = Identite.query.filter_by(id=_abs.etudid).first() if etud is None: - return "No Etud" + raise Exception("Etudiant inexistant") _glob.CURRENT_ETU.append(_abs.etudid) moduleimpl_id: int = _abs.moduleimpl_id @@ -272,7 +329,7 @@ def _from_abs_to_justificatif( if _abs.etudid not in _glob.CURRENT_ETU: etud: Identite = Identite.query.filter_by(id=_abs.etudid).first() if etud is None: - return "No Etud" + raise Exception("Etudiant inexistant") _glob.CURRENT_ETU.append(_abs.etudid) retour = Justificatif.fast_create_justificatif(