Optimisation migration absences->assiduites

This commit is contained in:
Emmanuel Viennet 2023-07-19 10:37:56 +02:00
parent cde5960899
commit cb0b890f1f

View File

@ -7,6 +7,8 @@ from datetime import date, datetime, time, timedelta
from json import dump, dumps from json import dump, dumps
from sqlalchemy import not_ from sqlalchemy import not_
from flask import g
from app import db from app import db
from app.models import ( from app.models import (
Absence, Absence,
@ -30,6 +32,7 @@ from app.scodoc.sco_utils import (
localize_datetime, localize_datetime,
print_progress_bar, print_progress_bar,
) )
from app.scodoc import notesdb as ndb
class _glob: class _glob:
@ -38,7 +41,6 @@ class _glob:
DEBUG: bool = False DEBUG: bool = False
PROBLEMS: dict[int, list[str]] = {} PROBLEMS: dict[int, list[str]] = {}
DEPT_ETUDIDS: dict[int, Identite] = {} DEPT_ETUDIDS: dict[int, Identite] = {}
MODULES: dict[tuple[int, int]] = {}
COMPTE: list[int, int] = [] COMPTE: list[int, int] = []
ERR_ETU: list[int] = [] ERR_ETU: list[int] = []
MERGER_ASSI: "_Merger" = None MERGER_ASSI: "_Merger" = None
@ -97,42 +99,70 @@ class _Merger:
date_deb = _Merger._tuple_to_date(self.deb) date_deb = _Merger._tuple_to_date(self.deb)
date_fin = _Merger._tuple_to_date(self.fin, end=True) date_fin = _Merger._tuple_to_date(self.fin, end=True)
retour = Justificatif.fast_create_justificatif( _glob.cursor.execute(
etudid=self.etudid, """INSERT INTO justificatifs
date_debut=date_deb, (etudid,date_debut,date_fin,etat,raison,entry_date)
date_fin=date_fin, VALUES (%(etudid)s,%(date_debut)s,%(date_fin)s,%(etat)s,%(raison)s,%(entry_date)s)
etat=EtatJustificatif.VALIDE, """,
raison=self.raison, {
entry_date=self.entry_date, "etudid": self.etudid,
"date_debut": date_deb,
"date_fin": date_fin,
"etat": EtatJustificatif.VALIDE,
"raison": self.raison,
"entry_date": self.entry_date,
},
) )
return retour # retour = Justificatif.fast_create_justificatif(
# etudid=self.etudid,
# date_debut=date_deb,
# date_fin=date_fin,
# etat=EtatJustificatif.VALIDE,
# raison=self.raison,
# entry_date=self.entry_date,
# )
# return retour
def _to_assi(self): def _to_assi(self):
date_deb = _Merger._tuple_to_date(self.deb) date_deb = _Merger._tuple_to_date(self.deb)
date_fin = _Merger._tuple_to_date(self.fin, end=True) date_fin = _Merger._tuple_to_date(self.fin, end=True)
retour = Assiduite.fast_create_assiduite( _glob.cursor.execute(
etudid=self.etudid, """INSERT INTO assiduites
date_debut=date_deb, (etudid,date_debut,date_fin,etat,moduleimpl_id,"desc",entry_date)
date_fin=date_fin, VALUES (%(etudid)s,%(date_debut)s,%(date_fin)s,%(etat)s,%(moduleimpl_id)s,%(desc)s,%(entry_date)s)
etat=EtatAssiduite.ABSENT, """,
moduleimpl_id=self.moduleimpl, {
description=self.raison, "etudid": self.etudid,
entry_date=self.entry_date, "date_debut": date_deb,
"date_fin": date_fin,
"etat": EtatAssiduite.ABSENT,
"moduleimpl_id": self.moduleimpl,
"desc": self.raison,
"entry_date": self.entry_date,
},
) )
return retour
# retour = Assiduite.fast_create_assiduite(
# etudid=self.etudid,
# date_debut=date_deb,
# date_fin=date_fin,
# etat=EtatAssiduite.ABSENT,
# moduleimpl_id=self.moduleimpl,
# description=self.raison,
# entry_date=self.entry_date,
# )
# return retour
def export(self): def export(self):
"""Génère un nouvel objet Assiduité ou Justificatif""" """Génère un nouvel objet Assiduité ou Justificatif"""
obj: Assiduite or Justificatif = None obj: Assiduite or Justificatif = None
if self.est_abs: if self.est_abs:
_glob.COMPTE[0] += 1 _glob.COMPTE[0] += 1
obj = self._to_assi() self._to_assi()
else: else:
_glob.COMPTE[1] += 1 _glob.COMPTE[1] += 1
obj = self._to_justif() self._to_justif()
db.session.add(obj)
class _Statistics: class _Statistics:
@ -243,6 +273,10 @@ def migrate_abs_to_assiduites(
evening: list[str] = str(evening).split(":") evening: list[str] = str(evening).split(":")
_glob.EVENING = time(int(evening[0]), int(evening[1])) _glob.EVENING = time(int(evening[0]), int(evening[1]))
ndb.open_db_connection()
_glob.cnx = g.db_conn
_glob.cursor = _glob.cnx.cursor()
if dept is None: if dept is None:
prof_total = Profiler("MigrationTotal") prof_total = Profiler("MigrationTotal")
prof_total.start() prof_total.start()
@ -287,7 +321,6 @@ def migrate_dept(dept_name: str, stats: _Statistics, time_elapsed: Profiler):
return return
_glob.DEPT_ETUDIDS = {e.id for e in Identite.query.filter_by(dept_id=dept.id)} _glob.DEPT_ETUDIDS = {e.id for e in Identite.query.filter_by(dept_id=dept.id)}
_glob.MODULES = {}
_glob.COMPTE = [0, 0] _glob.COMPTE = [0, 0]
_glob.ERR_ETU = [] _glob.ERR_ETU = []
_glob.MERGER_ASSI = None _glob.MERGER_ASSI = None
@ -299,9 +332,17 @@ def migrate_dept(dept_name: str, stats: _Statistics, time_elapsed: Profiler):
print_progress_bar(0, absences_len, "Progression", "effectué", autosize=True) print_progress_bar(0, absences_len, "Progression", "effectué", autosize=True)
etuds_modimpl_ids = {}
for i, abs_ in enumerate(absences): for i, abs_ in enumerate(absences):
etud_modimpl_ids = etuds_modimpl_ids.get(abs_.etudid)
if etud_modimpl_ids is None:
etud_modimpl_ids = {
ins.moduleimpl_id
for ins in ModuleImplInscription.query.filter_by(etudid=abs_.etudid)
}
etuds_modimpl_ids[abs_.etudid] = etud_modimpl_ids
try: try:
_from_abs_to_assiduite_justificatif(abs_) _from_abs_to_assiduite_justificatif(abs_, etud_modimpl_ids)
except ValueError as e: except ValueError as e:
stats.add_problem(abs_, e.args[0]) stats.add_problem(abs_, e.args[0])
@ -322,14 +363,14 @@ def migrate_dept(dept_name: str, stats: _Statistics, time_elapsed: Profiler):
"effectué", "effectué",
autosize=True, autosize=True,
) )
db.session.commit() _glob.cnx.commit()
if _glob.MERGER_ASSI is not None: if _glob.MERGER_ASSI is not None:
_glob.MERGER_ASSI.export() _glob.MERGER_ASSI.export()
if _glob.MERGER_JUST is not None: if _glob.MERGER_JUST is not None:
_glob.MERGER_JUST.export() _glob.MERGER_JUST.export()
db.session.commit() _glob.cnx.commit()
print_progress_bar( print_progress_bar(
absences_len, absences_len,
@ -379,24 +420,15 @@ def migrate_dept(dept_name: str, stats: _Statistics, time_elapsed: Profiler):
print(dumps(statistiques, indent=2)) print(dumps(statistiques, indent=2))
def _from_abs_to_assiduite_justificatif(_abs: Absence): def _from_abs_to_assiduite_justificatif(_abs: Absence, etud_modimpl_ids: set[int]):
if _abs.etudid not in _glob.DEPT_ETUDIDS: if _abs.etudid not in _glob.DEPT_ETUDIDS:
raise ValueError("Etudiant inexistant") raise ValueError("Etudiant inexistant")
if _abs.estabs: if _abs.estabs:
moduleimpl_id: int = _abs.moduleimpl_id if (_abs.moduleimpl_id is not None) and (
if ( _abs.moduleimpl_id not in etud_modimpl_ids
moduleimpl_id is not None
and (_abs.etudid, _abs.moduleimpl_id) not in _glob.MODULES
): ):
moduleimpl_inscription: ModuleImplInscription = (
ModuleImplInscription.query.filter_by(
moduleimpl_id=_abs.moduleimpl_id, etudid=_abs.etudid
).first()
)
if moduleimpl_inscription is None:
raise ValueError("Moduleimpl_id incorrect ou étudiant non inscrit") raise ValueError("Moduleimpl_id incorrect ou étudiant non inscrit")
_glob.MODULES[(_abs.etudid, _abs.moduleimpl_id)] = True
if _glob.MERGER_ASSI is None: if _glob.MERGER_ASSI is None:
_glob.MERGER_ASSI = _Merger(_abs, True) _glob.MERGER_ASSI = _Merger(_abs, True)