Feat Prototipos Widgets y Fix Worker Telegramas

This commit is contained in:
2025-08-25 10:25:54 -03:00
parent 8192185bc5
commit 4a6318c18a
42 changed files with 1635 additions and 1092 deletions

View File

@@ -237,8 +237,6 @@ public class LowPriorityDataWorker : BackgroundService
}
}
/// <summary>
/// Sondea la proyección de bancas a nivel Provincial y por Sección Electoral.
/// Esta versión es completamente robusta: maneja respuestas de API vacías o con fechas mal formadas,
@@ -410,21 +408,16 @@ public class LowPriorityDataWorker : BackgroundService
if (!partidos.Any() || !categorias.Any()) return;
// --- LÓGICA DE GOTEO LENTO ---
// Procesamos una combinación (partido/categoría) a la vez.
foreach (var partido in partidos)
{
foreach (var categoria in categorias)
{
// Si la aplicación se apaga, salimos inmediatamente.
if (stoppingToken.IsCancellationRequested) return;
// Obtenemos la lista de IDs.
var listaTelegramasApi = await _apiService.GetTelegramasTotalizadosAsync(authToken, partido.DistritoId!, partido.SeccionId!, categoria.Id);
if (listaTelegramasApi is { Count: > 0 })
{
// Usamos un DbContext propio para este bloque para asegurar que los cambios se guarden.
using var innerScope = _serviceProvider.CreateScope();
var innerDbContext = innerScope.ServiceProvider.GetRequiredService<EleccionesDbContext>();
@@ -439,7 +432,6 @@ public class LowPriorityDataWorker : BackgroundService
{
_logger.LogInformation("Se encontraron {count} telegramas nuevos en '{partido}' para '{cat}'. Descargando...", nuevosTelegramasIds.Count, partido.Nombre, categoria.Nombre);
// Descargamos los archivos de uno en uno, con una pausa entre cada uno.
foreach (var mesaId in nuevosTelegramasIds)
{
if (stoppingToken.IsCancellationRequested) return;
@@ -447,24 +439,37 @@ public class LowPriorityDataWorker : BackgroundService
var telegramaFile = await _apiService.GetTelegramaFileAsync(authToken, mesaId);
if (telegramaFile != null)
{
var nuevoTelegrama = new Telegrama
// --- INICIO DE LA CORRECCIÓN ---
// 1. Buscamos el AmbitoGeografico específico de la MESA que estamos procesando.
var ambitoMesa = await innerDbContext.AmbitosGeograficos
.AsNoTracking()
.FirstOrDefaultAsync(a => a.MesaId == mesaId, stoppingToken);
// 2. Solo guardamos el telegrama si encontramos su ámbito de mesa correspondiente.
if (ambitoMesa != null)
{
Id = telegramaFile.NombreArchivo,
AmbitoGeograficoId = partido.Id,
ContenidoBase64 = telegramaFile.Imagen,
FechaEscaneo = DateTime.Parse(telegramaFile.FechaEscaneo).ToUniversalTime(),
FechaTotalizacion = DateTime.Parse(telegramaFile.FechaTotalizacion).ToUniversalTime()
};
await innerDbContext.Telegramas.AddAsync(nuevoTelegrama, stoppingToken);
var nuevoTelegrama = new Telegrama
{
Id = telegramaFile.NombreArchivo,
// 3. Usamos el ID del ÁMBITO DE LA MESA, no el del municipio.
AmbitoGeograficoId = ambitoMesa.Id,
ContenidoBase64 = telegramaFile.Imagen,
FechaEscaneo = DateTime.Parse(telegramaFile.FechaEscaneo).ToUniversalTime(),
FechaTotalizacion = DateTime.Parse(telegramaFile.FechaTotalizacion).ToUniversalTime()
};
await innerDbContext.Telegramas.AddAsync(nuevoTelegrama, stoppingToken);
}
else
{
_logger.LogWarning("No se encontró un ámbito geográfico para la mesa con MesaId {MesaId}. El telegrama no será guardado.", mesaId);
}
// --- FIN DE LA CORRECCIÓN ---
}
// PAUSA DELIBERADA: Esperamos un poco para no parecer un bot.
await Task.Delay(250, stoppingToken); // 250ms de espera = 4 peticiones/segundo máximo.
await Task.Delay(250, stoppingToken);
}
await innerDbContext.SaveChangesAsync(stoppingToken);
}
}
// PAUSA DELIBERADA: Esperamos un poco entre cada consulta de lista de telegramas.
await Task.Delay(100, stoppingToken);
}
}