Feat(holidays): Implement database-backed holiday detection system

- Adds a new `MercadosFeriados` table to the database to persist market holidays.
- Implements `HolidayDataFetcher` to update holidays weekly from Finnhub API.
- Implements `IHolidayService` with in-memory caching to check for holidays efficiently.
- Worker service now skips fetcher execution on market holidays.
- Adds a new API endpoint `/api/mercados/es-feriado/{mercado}`.
- Integrates a non-blocking holiday alert into the `BolsaLocalWidget`."
This commit is contained in:
2025-07-15 11:20:28 -03:00
parent 640b7d1ece
commit e1e23f5315
20 changed files with 592 additions and 122 deletions

View File

@@ -1,4 +1,5 @@
import { useState } from 'react'; // Importaciones de React y Material-UI
import React, { useState, useRef } from 'react';
import { import {
Box, CircularProgress, Alert, Table, TableBody, TableCell, TableContainer, Box, CircularProgress, Alert, Table, TableBody, TableCell, TableContainer,
TableHead, TableRow, Paper, Typography, Dialog, DialogTitle, TableHead, TableRow, Paper, Typography, Dialog, DialogTitle,
@@ -8,12 +9,19 @@ import CloseIcon from '@mui/icons-material/Close';
import ArrowUpwardIcon from '@mui/icons-material/ArrowUpward'; import ArrowUpwardIcon from '@mui/icons-material/ArrowUpward';
import ArrowDownwardIcon from '@mui/icons-material/ArrowDownward'; import ArrowDownwardIcon from '@mui/icons-material/ArrowDownward';
import RemoveIcon from '@mui/icons-material/Remove'; import RemoveIcon from '@mui/icons-material/Remove';
import { formatFullDateTime, formatCurrency } from '../utils/formatters';
import type { CotizacionBolsa } from '../models/mercadoModels';
import { useApiData } from '../hooks/useApiData';
import { HistoricalChartWidget } from './HistoricalChartWidget';
import { PiChartLineUpBold } from 'react-icons/pi'; import { PiChartLineUpBold } from 'react-icons/pi';
// Importaciones de nuestros modelos, hooks y utilidades
import type { CotizacionBolsa } from '../models/mercadoModels';
import { useApiData } from '../hooks/useApiData';
import { useIsHoliday } from '../hooks/useIsHoliday';
import { formatFullDateTime, formatCurrency } from '../utils/formatters';
import { HistoricalChartWidget } from './HistoricalChartWidget';
import { HolidayAlert } from './common/HolidayAlert';
/**
* Sub-componente para mostrar la variación porcentual con un icono y color apropiado.
*/
const Variacion = ({ value }: { value: number }) => { const Variacion = ({ value }: { value: number }) => {
const color = value > 0 ? 'success.main' : value < 0 ? 'error.main' : 'text.secondary'; const color = value > 0 ? 'success.main' : value < 0 ? 'error.main' : 'text.secondary';
const Icon = value > 0 ? ArrowUpwardIcon : value < 0 ? ArrowDownwardIcon : RemoveIcon; const Icon = value > 0 ? ArrowUpwardIcon : value < 0 ? ArrowDownwardIcon : RemoveIcon;
@@ -25,31 +33,22 @@ const Variacion = ({ value }: { value: number }) => {
); );
}; };
export const BolsaLocalWidget = () => { /**
const { data, loading, error } = useApiData<CotizacionBolsa[]>('/mercados/bolsa/local'); * Sub-componente que renderiza la tabla de acciones detalladas.
const [selectedTicker, setSelectedTicker] = useState<string | null>(null); * Se extrae para mantener el componente principal más limpio.
*/
const RenderContent = ({ data, handleOpenModal }: {
data: CotizacionBolsa[],
handleOpenModal: (ticker: string, event: React.MouseEvent<HTMLButtonElement>) => void,
}) => {
// Filtramos para obtener solo las acciones, excluyendo el índice.
const panelPrincipal = data.filter(d => d.ticker !== '^MERV');
const handleRowClick = (ticker: string) => setSelectedTicker(ticker); if (panelPrincipal.length === 0) {
const handleCloseDialog = () => setSelectedTicker(null); return <Alert severity="info">No hay acciones líderes para mostrar en este momento.</Alert>;
const panelPrincipal = data?.filter(d => d.ticker !== '^MERV') || [];
if (loading) {
return <Box sx={{ display: 'flex', justifyContent: 'center', p: 4 }}><CircularProgress /></Box>;
}
if (error) {
return <Alert severity="error">{error}</Alert>;
}
if (!data || data.length === 0) {
return <Alert severity="info">No hay datos disponibles para el mercado local.</Alert>;
} }
return ( return (
<>
{panelPrincipal.length > 0 && (
<TableContainer component={Paper}> <TableContainer component={Paper}>
<Box sx={{ p: 1, m: 0 }}> <Box sx={{ p: 1, m: 0 }}>
<Typography variant="caption" sx={{ fontStyle: 'italic', color: 'text.secondary' }}> <Typography variant="caption" sx={{ fontStyle: 'italic', color: 'text.secondary' }}>
@@ -79,7 +78,7 @@ export const BolsaLocalWidget = () => {
<IconButton <IconButton
aria-label={`ver historial de ${row.ticker}`} aria-label={`ver historial de ${row.ticker}`}
size="small" size="small"
onClick={() => handleRowClick(row.ticker)} onClick={(event) => handleOpenModal(row.ticker, event)}
sx={{ sx={{
boxShadow: '0 1px 3px rgba(0,0,0,0.1)', boxShadow: '0 1px 3px rgba(0,0,0,0.1)',
transition: 'all 0.2s ease-in-out', transition: 'all 0.2s ease-in-out',
@@ -94,22 +93,95 @@ export const BolsaLocalWidget = () => {
</TableBody> </TableBody>
</Table> </Table>
</TableContainer> </TableContainer>
);
};
/**
* Widget principal para la sección "Bolsa Local".
* Muestra una tarjeta de héroe para el MERVAL y una tabla detallada para las acciones líderes.
*/
export const BolsaLocalWidget = () => {
// Hooks para obtener los datos y el estado de feriado. Las llamadas se disparan en paralelo.
const { data, loading: dataLoading, error: dataError } = useApiData<CotizacionBolsa[]>('/mercados/bolsa/local');
const isHoliday = useIsHoliday('BA');
// Estado y referencia para manejar el modal del gráfico.
const [selectedTicker, setSelectedTicker] = useState<string | null>(null);
const triggerButtonRef = useRef<HTMLButtonElement | null>(null);
const handleOpenModal = (ticker: string, event: React.MouseEvent<HTMLButtonElement>) => {
triggerButtonRef.current = event.currentTarget;
setSelectedTicker(ticker);
};
const handleCloseDialog = () => {
setSelectedTicker(null);
// Devuelve el foco al botón que abrió el modal para mejorar la accesibilidad.
setTimeout(() => {
triggerButtonRef.current?.focus();
}, 0);
};
// Estado de carga unificado: el componente está "cargando" si los datos principales
// o la información del feriado todavía no han llegado.
const isLoading = dataLoading || isHoliday === null;
if (isLoading) {
return <Box sx={{ display: 'flex', justifyContent: 'center', p: 4 }}><CircularProgress /></Box>;
}
if (dataError) {
return <Alert severity="error">{dataError}</Alert>;
}
// Si no hay ningún dato en absoluto, mostramos un mensaje final.
if (!data || data.length === 0) {
// Si sabemos que es feriado, la alerta de feriado tiene prioridad.
if (isHoliday) {
return <HolidayAlert />;
}
return <Alert severity="info">No hay datos disponibles para el mercado local.</Alert>;
}
return (
<>
{/* Si es feriado, mostramos la alerta informativa en la parte superior. */}
{isHoliday && (
<Box sx={{ mb: 2 }}>
<HolidayAlert />
</Box>
)} )}
{/* La tabla de acciones detalladas se muestra siempre que haya datos para ella. */}
<RenderContent
data={data}
handleOpenModal={handleOpenModal}
/>
{/* El Dialog para mostrar el gráfico histórico. */}
<Dialog <Dialog
open={Boolean(selectedTicker)} onClose={handleCloseDialog} maxWidth="md" fullWidth open={Boolean(selectedTicker)}
onClose={handleCloseDialog}
maxWidth="md"
fullWidth
sx={{ '& .MuiDialog-paper': { overflow: 'visible' } }} sx={{ '& .MuiDialog-paper': { overflow: 'visible' } }}
> >
<IconButton <IconButton
aria-label="close" onClick={handleCloseDialog} aria-label="close"
onClick={handleCloseDialog}
sx={{ sx={{
position: 'absolute', top: -15, right: -15, color: (theme) => theme.palette.grey[500], position: 'absolute', top: -15, right: -15,
backgroundColor: 'white', boxShadow: 3, '&:hover': { backgroundColor: 'grey.100' }, color: (theme) => theme.palette.grey[500],
backgroundColor: 'white', boxShadow: 3,
'&:hover': { backgroundColor: 'grey.100' },
}} }}
> >
<CloseIcon /> <CloseIcon />
</IconButton> </IconButton>
<DialogTitle sx={{ m: 0, p: 2 }}>Historial de 30 días para: {selectedTicker}</DialogTitle> <DialogTitle sx={{ m: 0, p: 2 }}>
Historial de 30 días para: {selectedTicker}
</DialogTitle>
<DialogContent dividers> <DialogContent dividers>
{selectedTicker && <HistoricalChartWidget ticker={selectedTicker} mercado="Local" dias={30} />} {selectedTicker && <HistoricalChartWidget ticker={selectedTicker} mercado="Local" dias={30} />}
</DialogContent> </DialogContent>

View File

@@ -17,6 +17,9 @@ import { formatInteger, formatDateOnly } from '../utils/formatters';
import { GrainsHistoricalChartWidget } from './GrainsHistoricalChartWidget'; import { GrainsHistoricalChartWidget } from './GrainsHistoricalChartWidget';
import { LuBean } from 'react-icons/lu'; import { LuBean } from 'react-icons/lu';
import { useIsHoliday } from '../hooks/useIsHoliday';
import { HolidayAlert } from './common/HolidayAlert';
const getGrainIcon = (nombre: string) => { const getGrainIcon = (nombre: string) => {
switch (nombre.toLowerCase()) { switch (nombre.toLowerCase()) {
case 'girasol': return <GiSunflower size={28} color="#fbc02d" />; case 'girasol': return <GiSunflower size={28} color="#fbc02d" />;
@@ -96,6 +99,7 @@ const GranoCard = ({ grano, onChartClick }: { grano: CotizacionGrano, onChartCli
export const GranosCardWidget = () => { export const GranosCardWidget = () => {
const { data, loading, error } = useApiData<CotizacionGrano[]>('/mercados/granos'); const { data, loading, error } = useApiData<CotizacionGrano[]>('/mercados/granos');
const isHoliday = useIsHoliday('BA');
const [selectedGrano, setSelectedGrano] = useState<string | null>(null); const [selectedGrano, setSelectedGrano] = useState<string | null>(null);
const triggerButtonRef = useRef<HTMLButtonElement | null>(null); const triggerButtonRef = useRef<HTMLButtonElement | null>(null);
@@ -112,6 +116,7 @@ export const GranosCardWidget = () => {
}; };
if (loading) { if (loading) {
// El spinner de carga sigue siendo prioritario
return <Box sx={{ display: 'flex', justifyContent: 'center', p: 4 }}><CircularProgress /></Box>; return <Box sx={{ display: 'flex', justifyContent: 'center', p: 4 }}><CircularProgress /></Box>;
} }
@@ -125,6 +130,12 @@ export const GranosCardWidget = () => {
return ( return (
<> <>
{/* Si es feriado (y la comprobación ha terminado), mostramos la alerta encima */}
{isHoliday === true && (
<Box sx={{ mb: 2 }}> {/* Añadimos un margen inferior a la alerta */}
<HolidayAlert />
</Box>
)}
<Box <Box
sx={{ sx={{
display: 'flex', display: 'flex',

View File

@@ -52,8 +52,8 @@ export const MervalHeroCard = () => {
<Paper elevation={3} sx={{ p: 2, mb: 3 }}> <Paper elevation={3} sx={{ p: 2, mb: 3 }}>
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', mb: 2 }}> <Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', mb: 2 }}>
<Box> <Box>
<Typography variant="h5" component="h2" sx={{ fontWeight: 'bold' }}>Índice S&P MERVAL</Typography> <Typography variant="h6" component="h2" sx={{ fontWeight: 'bold' }}>Índice S&P MERVAL</Typography>
<Typography variant="h3" component="p" sx={{ fontWeight: 'bold', mt:1 }}>{formatCurrency2Decimal(mervalData.precioActual)}</Typography> <Typography variant="h4" component="p" sx={{ fontWeight: 'bold', mt:1 }}>{formatCurrency2Decimal(mervalData.precioActual)}</Typography>
</Box> </Box>
<Box sx={{ pt: 2 }}> <Box sx={{ pt: 2 }}>
<VariacionMerval actual={mervalData.precioActual} anterior={mervalData.cierreAnterior} /> <VariacionMerval actual={mervalData.precioActual} anterior={mervalData.cierreAnterior} />

View File

@@ -51,8 +51,8 @@ export const UsaIndexHeroCard = () => {
<Paper elevation={3} sx={{ p: 2, mb: 3 }}> <Paper elevation={3} sx={{ p: 2, mb: 3 }}>
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', mb: 2 }}> <Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', mb: 2 }}>
<Box> <Box>
<Typography variant="h5" component="h2" sx={{ fontWeight: 'bold' }}>S&P 500 Index</Typography> <Typography variant="h6" component="h2" sx={{ fontWeight: 'bold' }}>S&P 500 Index</Typography>
<Typography variant="h3" component="p" sx={{ fontWeight: 'bold', mt:1 }}>{formatInteger(indexData.precioActual)}</Typography> <Typography variant="h4" component="p" sx={{ fontWeight: 'bold', mt:1 }}>{formatInteger(indexData.precioActual)}</Typography>
</Box> </Box>
<Box sx={{ pt: 2 }}> <Box sx={{ pt: 2 }}>
<VariacionIndex actual={indexData.precioActual} anterior={indexData.cierreAnterior} /> <VariacionIndex actual={indexData.precioActual} anterior={indexData.cierreAnterior} />

View File

@@ -0,0 +1,10 @@
import { Alert } from '@mui/material';
import CelebrationIcon from '@mui/icons-material/Celebration';
export const HolidayAlert = () => {
return (
<Alert severity="info" icon={<CelebrationIcon fontSize="inherit" />}>
Mercado cerrado por feriado.
</Alert>
);
};

View File

@@ -0,0 +1,24 @@
import { useState, useEffect } from 'react';
import apiClient from '../api/apiClient';
export function useIsHoliday(marketCode: 'BA' | 'US') {
const [isHoliday, setIsHoliday] = useState<boolean | null>(null);
useEffect(() => {
const checkHoliday = async () => {
try {
const response = await apiClient.get<boolean>(`/api/mercados/es-feriado/${marketCode}`);
setIsHoliday(response.data);
console.log(`Feriado para ${marketCode}: ${response.data}`);
} catch (error) {
console.error(`Error al verificar feriado para ${marketCode}:`, error);
// Si la API de feriados falla, asumimos que no es feriado para no bloquear la UI.
setIsHoliday(false);
}
};
checkHoliday();
}, [marketCode]);
return isHoliday;
}

View File

@@ -1,5 +1,6 @@
using Mercados.Core.Entities; using Mercados.Core.Entities;
using Mercados.Infrastructure.Persistence.Repositories; using Mercados.Infrastructure.Persistence.Repositories;
using Mercados.Infrastructure.Services;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace Mercados.Api.Controllers namespace Mercados.Api.Controllers
@@ -11,6 +12,7 @@ namespace Mercados.Api.Controllers
private readonly ICotizacionBolsaRepository _bolsaRepo; private readonly ICotizacionBolsaRepository _bolsaRepo;
private readonly ICotizacionGranoRepository _granoRepo; private readonly ICotizacionGranoRepository _granoRepo;
private readonly ICotizacionGanadoRepository _ganadoRepo; private readonly ICotizacionGanadoRepository _ganadoRepo;
private readonly IHolidayService _holidayService;
private readonly ILogger<MercadosController> _logger; private readonly ILogger<MercadosController> _logger;
// Inyectamos TODOS los repositorios que necesita el controlador. // Inyectamos TODOS los repositorios que necesita el controlador.
@@ -18,11 +20,13 @@ namespace Mercados.Api.Controllers
ICotizacionBolsaRepository bolsaRepo, ICotizacionBolsaRepository bolsaRepo,
ICotizacionGranoRepository granoRepo, ICotizacionGranoRepository granoRepo,
ICotizacionGanadoRepository ganadoRepo, ICotizacionGanadoRepository ganadoRepo,
IHolidayService holidayService,
ILogger<MercadosController> logger) ILogger<MercadosController> logger)
{ {
_bolsaRepo = bolsaRepo; _bolsaRepo = bolsaRepo;
_granoRepo = granoRepo; _granoRepo = granoRepo;
_ganadoRepo = ganadoRepo; _ganadoRepo = ganadoRepo;
_holidayService = holidayService;
_logger = logger; _logger = logger;
} }
@@ -147,5 +151,30 @@ namespace Mercados.Api.Controllers
return StatusCode(500, "Ocurrió un error interno en el servidor."); return StatusCode(500, "Ocurrió un error interno en el servidor.");
} }
} }
[HttpGet("es-feriado/{mercado}")]
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> IsMarketHoliday(string mercado)
{
try
{
// Usamos la fecha actual en la zona horaria de Argentina
TimeZoneInfo argentinaTimeZone;
try { argentinaTimeZone = TimeZoneInfo.FindSystemTimeZoneById("America/Argentina/Buenos_Aires"); }
catch { argentinaTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Argentina Standard Time"); }
var todayInArgentina = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, argentinaTimeZone);
var esFeriado = await _holidayService.IsMarketHolidayAsync(mercado.ToUpper(), todayInArgentina);
return Ok(esFeriado);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error al comprobar si es feriado para el mercado {Mercado}.", mercado);
// Si hay un error, devolvemos 'false' para no bloquear la UI innecesariamente.
return Ok(false);
}
}
} }
} }

View File

@@ -5,6 +5,7 @@ using Mercados.Infrastructure.Persistence;
using Mercados.Infrastructure.Persistence.Repositories; using Mercados.Infrastructure.Persistence.Repositories;
using Mercados.Api.Utils; using Mercados.Api.Utils;
using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.HttpOverrides;
using Mercados.Infrastructure.Services;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@@ -32,6 +33,10 @@ builder.Services.AddScoped<ICotizacionGanadoRepository, CotizacionGanadoReposito
builder.Services.AddScoped<ICotizacionGranoRepository, CotizacionGranoRepository>(); builder.Services.AddScoped<ICotizacionGranoRepository, CotizacionGranoRepository>();
builder.Services.AddScoped<ICotizacionBolsaRepository, CotizacionBolsaRepository>(); builder.Services.AddScoped<ICotizacionBolsaRepository, CotizacionBolsaRepository>();
builder.Services.AddScoped<IFuenteDatoRepository, FuenteDatoRepository>(); builder.Services.AddScoped<IFuenteDatoRepository, FuenteDatoRepository>();
builder.Services.AddScoped<IMercadoFeriadoRepository, MercadoFeriadoRepository>();
builder.Services.AddMemoryCache();
builder.Services.AddScoped<IMercadoFeriadoRepository, MercadoFeriadoRepository>();
builder.Services.AddScoped<IHolidayService, FinnhubHolidayService>();
// Configuración de FluentMigrator (perfecto) // Configuración de FluentMigrator (perfecto)
builder.Services builder.Services

View File

@@ -0,0 +1,10 @@
namespace Mercados.Core.Entities
{
public class MercadoFeriado
{
public long Id { get; set; }
public string CodigoMercado { get; set; } = string.Empty; // "US" o "BA"
public DateTime Fecha { get; set; }
public string? Nombre { get; set; } // Nombre del feriado, si la API lo provee
}
}

View File

@@ -2,7 +2,7 @@ using FluentMigrator;
namespace Mercados.Database.Migrations namespace Mercados.Database.Migrations
{ {
[Migration(20240702133000)] [Migration(20250702133000)]
public class AddNameToStocks : Migration public class AddNameToStocks : Migration
{ {
public override void Up() public override void Up()

View File

@@ -0,0 +1,31 @@
using FluentMigrator;
namespace Mercados.Database.Migrations
{
[Migration(20250714150000)]
public class CreateMercadoFeriadoTable : Migration
{
private const string TableName = "MercadosFeriados";
public override void Up()
{
Create.Table(TableName)
.WithColumn("Id").AsInt64().PrimaryKey().Identity()
.WithColumn("CodigoMercado").AsString(10).NotNullable()
.WithColumn("Fecha").AsDate().NotNullable() // Usamos AsDate() para guardar solo la fecha
.WithColumn("Nombre").AsString(255).Nullable();
// Creamos un índice para buscar rápidamente por mercado y fecha
Create.Index($"IX_{TableName}_CodigoMercado_Fecha")
.OnTable(TableName)
.OnColumn("CodigoMercado").Ascending()
.OnColumn("Fecha").Ascending()
.WithOptions().Unique();
}
public override void Down()
{
Delete.Table(TableName);
}
}
}

View File

@@ -0,0 +1,84 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Net.Http.Json;
using Mercados.Core.Entities;
using Mercados.Infrastructure.Persistence.Repositories;
using System.Text.Json.Serialization;
namespace Mercados.Infrastructure.DataFetchers
{
// Añadimos las clases DTO necesarias para deserializar la respuesta de Finnhub
public class MarketHolidayResponse
{
[JsonPropertyName("data")]
public List<MarketHoliday>? Data { get; set; }
}
public class MarketHoliday
{
[JsonPropertyName("at")]
public string? At { get; set; }
[JsonIgnore]
public DateOnly Date => DateOnly.FromDateTime(DateTime.Parse(At!));
}
public class HolidayDataFetcher : IDataFetcher
{
public string SourceName => "Holidays";
private readonly string[] _marketCodes = { "US", "BA" };
private readonly IHttpClientFactory _httpClientFactory;
private readonly IMercadoFeriadoRepository _feriadoRepository;
private readonly IConfiguration _configuration;
private readonly ILogger<HolidayDataFetcher> _logger;
public HolidayDataFetcher(
IHttpClientFactory httpClientFactory,
IMercadoFeriadoRepository feriadoRepository,
IConfiguration configuration,
ILogger<HolidayDataFetcher> logger)
{
_httpClientFactory = httpClientFactory;
_feriadoRepository = feriadoRepository;
_configuration = configuration;
_logger = logger;
}
public async Task<(bool Success, string Message)> FetchDataAsync()
{
_logger.LogInformation("Iniciando actualización de feriados desde Finnhub.");
var apiKey = _configuration["ApiKeys:Finnhub"];
if (string.IsNullOrEmpty(apiKey)) return (false, "API Key de Finnhub no configurada.");
var client = _httpClientFactory.CreateClient("FinnhubDataFetcher");
foreach (var marketCode in _marketCodes)
{
try
{
var apiUrl = $"https://finnhub.io/api/v1/stock/market-holiday?exchange={marketCode}&token={apiKey}";
// Ahora la deserialización funcionará porque la clase existe
var response = await client.GetFromJsonAsync<MarketHolidayResponse>(apiUrl);
if (response?.Data != null)
{
var nuevosFeriados = response.Data.Select(h => new MercadoFeriado
{
CodigoMercado = marketCode,
Fecha = h.Date.ToDateTime(TimeOnly.MinValue),
Nombre = "Feriado Bursátil"
}).ToList();
await _feriadoRepository.ReemplazarFeriadosPorMercadoAsync(marketCode, nuevosFeriados);
_logger.LogInformation("Feriados para {MarketCode} actualizados exitosamente: {Count} registros.", marketCode, nuevosFeriados.Count);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Falló la obtención de feriados para {MarketCode}.", marketCode);
}
}
return (true, "Actualización de feriados completada.");
}
}
}

View File

@@ -9,6 +9,7 @@
<PackageReference Include="Dapper" Version="2.1.66" /> <PackageReference Include="Dapper" Version="2.1.66" />
<PackageReference Include="MailKit" Version="4.13.0" /> <PackageReference Include="MailKit" Version="4.13.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.2" /> <PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.2" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.7" />
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.6" /> <PackageReference Include="Microsoft.Extensions.Http" Version="9.0.6" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="9.0.6" /> <PackageReference Include="Microsoft.Extensions.Http.Polly" Version="9.0.6" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="9.0.6" /> <PackageReference Include="System.Text.Encoding.CodePages" Version="9.0.6" />

View File

@@ -0,0 +1,10 @@
using Mercados.Core.Entities;
namespace Mercados.Infrastructure.Persistence.Repositories
{
public interface IMercadoFeriadoRepository : IBaseRepository
{
Task<IEnumerable<MercadoFeriado>> ObtenerPorMercadoYAnioAsync(string codigoMercado, int anio);
Task ReemplazarFeriadosPorMercadoAsync(string codigoMercado, IEnumerable<MercadoFeriado> nuevosFeriados);
}
}

View File

@@ -0,0 +1,56 @@
using Dapper;
using Mercados.Core.Entities;
using System.Data;
namespace Mercados.Infrastructure.Persistence.Repositories
{
public class MercadoFeriadoRepository : IMercadoFeriadoRepository
{
private readonly IDbConnectionFactory _connectionFactory;
public MercadoFeriadoRepository(IDbConnectionFactory connectionFactory)
{
_connectionFactory = connectionFactory;
}
public async Task<IEnumerable<MercadoFeriado>> ObtenerPorMercadoYAnioAsync(string codigoMercado, int anio)
{
using IDbConnection connection = _connectionFactory.CreateConnection();
const string sql = @"
SELECT * FROM MercadosFeriados
WHERE CodigoMercado = @CodigoMercado AND YEAR(Fecha) = @Anio;";
return await connection.QueryAsync<MercadoFeriado>(sql, new { CodigoMercado = codigoMercado, Anio = anio });
}
public async Task ReemplazarFeriadosPorMercadoAsync(string codigoMercado, IEnumerable<MercadoFeriado> nuevosFeriados)
{
using IDbConnection connection = _connectionFactory.CreateConnection();
connection.Open();
using var transaction = connection.BeginTransaction();
try
{
// Borramos todos los feriados del año en curso para ese mercado
var anio = nuevosFeriados.FirstOrDefault()?.Fecha.Year;
if (anio.HasValue)
{
const string deleteSql = "DELETE FROM MercadosFeriados WHERE CodigoMercado = @CodigoMercado AND YEAR(Fecha) = @Anio;";
await connection.ExecuteAsync(deleteSql, new { CodigoMercado = codigoMercado, Anio = anio.Value }, transaction);
}
// Insertamos los nuevos
const string insertSql = @"
INSERT INTO MercadosFeriados (CodigoMercado, Fecha, Nombre)
VALUES (@CodigoMercado, @Fecha, @Nombre);";
await connection.ExecuteAsync(insertSql, nuevosFeriados, transaction);
transaction.Commit();
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@@ -0,0 +1,50 @@
using Mercados.Core.Entities;
using Mercados.Infrastructure.Persistence.Repositories;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
namespace Mercados.Infrastructure.Services
{
public class FinnhubHolidayService : IHolidayService
{
private readonly IMercadoFeriadoRepository _feriadoRepository;
private readonly IMemoryCache _cache;
private readonly ILogger<FinnhubHolidayService> _logger;
public FinnhubHolidayService(
IMercadoFeriadoRepository feriadoRepository,
IMemoryCache cache,
ILogger<FinnhubHolidayService> logger)
{
_feriadoRepository = feriadoRepository;
_cache = cache;
_logger = logger;
}
public async Task<bool> IsMarketHolidayAsync(string marketCode, DateTime date)
{
var dateOnly = DateOnly.FromDateTime(date);
var cacheKey = $"holidays_{marketCode}_{date.Year}";
if (!_cache.TryGetValue(cacheKey, out HashSet<DateOnly>? holidays))
{
_logger.LogInformation("Caché de feriados no encontrada para {MarketCode}. Obteniendo desde la base de datos.", marketCode);
try
{
// Llama a NUESTRA base de datos, no a la API externa.
var feriadosDesdeDb = await _feriadoRepository.ObtenerPorMercadoYAnioAsync(marketCode, date.Year);
holidays = feriadosDesdeDb.Select(h => DateOnly.FromDateTime(h.Fecha)).ToHashSet();
_cache.Set(cacheKey, holidays, TimeSpan.FromHours(24));
}
catch (Exception ex)
{
_logger.LogError(ex, "No se pudo obtener la lista de feriados para {MarketCode} desde la DB.", marketCode);
return false; // Asumimos que no es feriado si la DB falla
}
}
return holidays?.Contains(dateOnly) ?? false;
}
}
}

View File

@@ -0,0 +1,16 @@
namespace Mercados.Infrastructure.Services
{
/// <summary>
/// Define un servicio para consultar si una fecha es feriado para un mercado.
/// </summary>
public interface IHolidayService
{
/// <summary>
/// Comprueba si la fecha dada es un feriado bursátil para el mercado especificado.
/// </summary>
/// <param name="marketCode">El código del mercado (ej. "BA" para Buenos Aires, "US" para EEUU).</param>
/// <param name="date">La fecha a comprobar.</param>
/// <returns>True si es feriado, false si no lo es.</returns>
Task<bool> IsMarketHolidayAsync(string marketCode, DateTime date);
}
}

View File

@@ -15,21 +15,22 @@ namespace Mercados.Worker
private readonly IServiceProvider _serviceProvider; private readonly IServiceProvider _serviceProvider;
private readonly TimeZoneInfo _argentinaTimeZone; private readonly TimeZoneInfo _argentinaTimeZone;
// Almacenamos las expresiones Cron parseadas para no tener que hacerlo en cada ciclo. // Expresiones Cron
private readonly CronExpression _agroSchedule; private readonly CronExpression _agroSchedule;
private readonly CronExpression _bcrSchedule; private readonly CronExpression _bcrSchedule;
private readonly CronExpression _bolsasSchedule; private readonly CronExpression _bolsasSchedule;
private readonly CronExpression _holidaysSchedule;
// Almacenamos la próxima ejecución calculada para cada tarea. // Próximas ejecuciones
private DateTime? _nextAgroRun; private DateTime? _nextAgroRun;
private DateTime? _nextBcrRun; private DateTime? _nextBcrRun;
private DateTime? _nextBolsasRun; private DateTime? _nextBolsasRun;
private DateTime? _nextHolidaysRun;
// Diccionario para rastrear la hora de la última alerta ENVIADA por cada tarea.
private readonly Dictionary<string, DateTime> _lastAlertSent = new(); private readonly Dictionary<string, DateTime> _lastAlertSent = new();
// Definimos el período de "silencio" para las alertas (ej. 4 horas).
private readonly TimeSpan _alertSilencePeriod = TimeSpan.FromHours(4); private readonly TimeSpan _alertSilencePeriod = TimeSpan.FromHours(4);
// Eliminamos IHolidayService del constructor
public DataFetchingService( public DataFetchingService(
ILogger<DataFetchingService> logger, ILogger<DataFetchingService> logger,
IServiceProvider serviceProvider, IServiceProvider serviceProvider,
@@ -55,6 +56,7 @@ namespace Mercados.Worker
_agroSchedule = CronExpression.Parse(configuration["Schedules:MercadoAgroganadero"]!); _agroSchedule = CronExpression.Parse(configuration["Schedules:MercadoAgroganadero"]!);
_bcrSchedule = CronExpression.Parse(configuration["Schedules:BCR"]!); _bcrSchedule = CronExpression.Parse(configuration["Schedules:BCR"]!);
_bolsasSchedule = CronExpression.Parse(configuration["Schedules:Bolsas"]!); _bolsasSchedule = CronExpression.Parse(configuration["Schedules:Bolsas"]!);
_holidaysSchedule = CronExpression.Parse(configuration["Schedules:Holidays"]!);
} }
/// <summary> /// <summary>
@@ -64,7 +66,7 @@ namespace Mercados.Worker
{ {
_logger.LogInformation("🚀 Servicio de Fetching iniciado a las: {time}", DateTimeOffset.Now); _logger.LogInformation("🚀 Servicio de Fetching iniciado a las: {time}", DateTimeOffset.Now);
// Ejecutamos una vez al inicio para tener datos frescos inmediatamente. // La ejecución inicial sigue comentada
// await RunAllFetchersAsync(stoppingToken); // await RunAllFetchersAsync(stoppingToken);
// Calculamos las primeras ejecuciones programadas al arrancar. // Calculamos las primeras ejecuciones programadas al arrancar.
@@ -72,36 +74,78 @@ namespace Mercados.Worker
_nextAgroRun = _agroSchedule.GetNextOccurrence(utcNow, _argentinaTimeZone); _nextAgroRun = _agroSchedule.GetNextOccurrence(utcNow, _argentinaTimeZone);
_nextBcrRun = _bcrSchedule.GetNextOccurrence(utcNow, _argentinaTimeZone); _nextBcrRun = _bcrSchedule.GetNextOccurrence(utcNow, _argentinaTimeZone);
_nextBolsasRun = _bolsasSchedule.GetNextOccurrence(utcNow, _argentinaTimeZone); _nextBolsasRun = _bolsasSchedule.GetNextOccurrence(utcNow, _argentinaTimeZone);
_nextHolidaysRun = _holidaysSchedule.GetNextOccurrence(utcNow, _argentinaTimeZone);
// Usamos un PeriodicTimer que "despierta" cada 30 segundos para revisar si hay tareas pendientes. // Usamos un PeriodicTimer que "despierta" cada 30 segundos.
// Un intervalo más corto aumenta la precisión del disparo de las tareas.
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(30)); using var timer = new PeriodicTimer(TimeSpan.FromSeconds(30));
while (!stoppingToken.IsCancellationRequested && await timer.WaitForNextTickAsync(stoppingToken)) while (!stoppingToken.IsCancellationRequested && await timer.WaitForNextTickAsync(stoppingToken))
{ {
utcNow = DateTime.UtcNow; utcNow = DateTime.UtcNow;
var nowInArgentina = TimeZoneInfo.ConvertTimeFromUtc(utcNow, _argentinaTimeZone);
// Comprobamos si ha llegado el momento de la próxima ejecución para cada tarea. // Tarea de actualización de Feriados (semanal)
if (_nextHolidaysRun.HasValue && utcNow >= _nextHolidaysRun.Value)
{
_logger.LogInformation("Ejecutando tarea semanal de actualización de feriados.");
await RunFetcherByNameAsync("Holidays", stoppingToken);
_nextHolidaysRun = _holidaysSchedule.GetNextOccurrence(utcNow, _argentinaTimeZone);
}
// Tarea de Mercado Agroganadero (diaria)
if (_nextAgroRun.HasValue && utcNow >= _nextAgroRun.Value) if (_nextAgroRun.HasValue && utcNow >= _nextAgroRun.Value)
{
// Comprueba si NO es feriado en Argentina para ejecutar
if (!await IsMarketHolidayAsync("BA", nowInArgentina))
{ {
await RunFetcherByNameAsync("MercadoAgroganadero", stoppingToken); await RunFetcherByNameAsync("MercadoAgroganadero", stoppingToken);
// Inmediatamente después de ejecutar, calculamos la SIGUIENTE ocurrencia. }
else { _logger.LogInformation("Ejecución de MercadoAgroganadero omitida por ser feriado."); }
// Recalcula la próxima ejecución sin importar si corrió o fue feriado
_nextAgroRun = _agroSchedule.GetNextOccurrence(utcNow, _argentinaTimeZone); _nextAgroRun = _agroSchedule.GetNextOccurrence(utcNow, _argentinaTimeZone);
} }
// Tarea de Granos BCR (diaria)
if (_nextBcrRun.HasValue && utcNow >= _nextBcrRun.Value) if (_nextBcrRun.HasValue && utcNow >= _nextBcrRun.Value)
{
if (!await IsMarketHolidayAsync("BA", nowInArgentina))
{ {
await RunFetcherByNameAsync("BCR", stoppingToken); await RunFetcherByNameAsync("BCR", stoppingToken);
}
else { _logger.LogInformation("Ejecución de BCR omitida por ser feriado."); }
_nextBcrRun = _bcrSchedule.GetNextOccurrence(utcNow, _argentinaTimeZone); _nextBcrRun = _bcrSchedule.GetNextOccurrence(utcNow, _argentinaTimeZone);
} }
// Tarea de Bolsas (recurrente)
if (_nextBolsasRun.HasValue && utcNow >= _nextBolsasRun.Value) if (_nextBolsasRun.HasValue && utcNow >= _nextBolsasRun.Value)
{ {
_logger.LogInformation("Ventana de ejecución para Bolsas. Iniciando en paralelo..."); _logger.LogInformation("Ventana de ejecución para Bolsas detectada.");
await Task.WhenAll(
RunFetcherByNameAsync("YahooFinance", stoppingToken), var bolsaTasks = new List<Task>();
RunFetcherByNameAsync("Finnhub", stoppingToken)
); // Comprueba el mercado local (Argentina)
if (!await IsMarketHolidayAsync("BA", nowInArgentina))
{
bolsaTasks.Add(RunFetcherByNameAsync("YahooFinance", stoppingToken));
}
else { _logger.LogInformation("Ejecución de YahooFinance (Mercado Local) omitida por ser feriado."); }
// Comprueba el mercado de EEUU
if (!await IsMarketHolidayAsync("US", nowInArgentina))
{
bolsaTasks.Add(RunFetcherByNameAsync("Finnhub", stoppingToken));
}
else { _logger.LogInformation("Ejecución de Finnhub (Mercado EEUU) omitida por ser feriado."); }
// Si hay alguna tarea para ejecutar, las lanza en paralelo
if (bolsaTasks.Any())
{
_logger.LogInformation("Iniciando {Count} fetcher(s) de bolsa en paralelo...", bolsaTasks.Count);
await Task.WhenAll(bolsaTasks);
}
_nextBolsasRun = _bolsasSchedule.GetNextOccurrence(utcNow, _argentinaTimeZone); _nextBolsasRun = _bolsasSchedule.GetNextOccurrence(utcNow, _argentinaTimeZone);
} }
} }
@@ -179,5 +223,14 @@ namespace Mercados.Worker
} }
#endregion #endregion
// Creamos una única función para comprobar feriados que obtiene el servicio
// desde un scope.
private async Task<bool> IsMarketHolidayAsync(string marketCode, DateTime date)
{
using var scope = _serviceProvider.CreateScope();
var holidayService = scope.ServiceProvider.GetRequiredService<IHolidayService>();
return await holidayService.IsMarketHolidayAsync(marketCode, date);
}
} }
} }

View File

@@ -31,6 +31,13 @@ IHost host = Host.CreateDefaultBuilder(args)
services.AddHttpClient("BcrDataFetcher").AddPolicyHandler(GetRetryPolicy()); services.AddHttpClient("BcrDataFetcher").AddPolicyHandler(GetRetryPolicy());
services.AddHttpClient("FinnhubDataFetcher").AddPolicyHandler(GetRetryPolicy()); services.AddHttpClient("FinnhubDataFetcher").AddPolicyHandler(GetRetryPolicy());
// Servicio de caché en memoria de .NET
services.AddMemoryCache();
// Registramos nuestro nuevo servicio de feriados
services.AddScoped<IHolidayService, FinnhubHolidayService>();
services.AddScoped<IDataFetcher, HolidayDataFetcher>();
services.AddScoped<IMercadoFeriadoRepository, MercadoFeriadoRepository>();
services.AddHostedService<DataFetchingService>(); services.AddHostedService<DataFetchingService>();
}) })
.Build(); .Build();

View File

@@ -12,7 +12,8 @@
"Schedules": { "Schedules": {
"MercadoAgroganadero": "0 11 * * 1-5", "MercadoAgroganadero": "0 11 * * 1-5",
"BCR": "30 11 * * 1-5", "BCR": "30 11 * * 1-5",
"Bolsas": "10 11-17 * * 1-5" "Bolsas": "10 11-17 * * 1-5",
"Holidays": "0 2 * * 1"
}, },
"ApiKeys": { "ApiKeys": {
"Finnhub": "", "Finnhub": "",