Actualizar backend/core/data_manager.py

This commit is contained in:
2026-07-10 14:10:55 -05:00
parent f21c37c162
commit 0e0909f230

View File

@@ -58,6 +58,11 @@ class DataManager:
self.query_cronograma_sql = ""
self.query_facturas_sql = ""
# Caché en memoria de las consultas ANUALES de cobranza (evita re-ejecutar
# la consulta del año completo en cada combinación de filtros).
self._cache_cronograma = {} # str(ano) -> filas
self._cache_facturas = {} # str(ano) -> filas
self.cargar_toda_configuracion()
# =========================================================================
@@ -645,6 +650,10 @@ class DataManager:
return []
def ejecutar_consulta_cronograma_cobranza(self, ano):
clave = str(ano)
en_cache = self._cache_cronograma.get(clave)
if en_cache is not None:
return en_cache
try:
if not self.query_cronograma_sql:
return []
@@ -654,12 +663,17 @@ class DataManager:
columns = [col[0] for col in cursor.description]
results = [dict(zip(columns, row)) for row in cursor.fetchall()]
conn.close()
self._cache_cronograma[clave] = results # cachear SOLO si tuvo éxito
return results
except Exception as e:
print(f"❌ Error cronograma cobranza: {e}")
return []
def ejecutar_consulta_facturas_cobranza(self, ano):
clave = str(ano)
en_cache = self._cache_facturas.get(clave)
if en_cache is not None:
return en_cache
try:
if not self.query_facturas_sql:
return []
@@ -669,11 +683,22 @@ class DataManager:
columns = [col[0] for col in cursor.description]
results = [dict(zip(columns, row)) for row in cursor.fetchall()]
conn.close()
self._cache_facturas[clave] = results # cachear SOLO si tuvo éxito
return results
except Exception as e:
print(f"❌ Error facturas cobranza: {e}")
return []
def limpiar_cache_cobranza(self, ano=None):
"""Vacía el caché anual de cobranza para forzar re-lectura desde SQL.
Se llama en cada refresco (manual o ciclo de 15 min)."""
if ano is None:
self._cache_cronograma.clear()
self._cache_facturas.clear()
else:
self._cache_cronograma.pop(str(ano), None)
self._cache_facturas.pop(str(ano), None)
def ejecutar_consulta_saldos_anual(self, ano):
try:
if not self.query_matriculas_sql: