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:
@@ -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,91 +33,155 @@ const Variacion = ({ value }: { value: number }) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sub-componente que renderiza la tabla de acciones detalladas.
|
||||||
|
* 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');
|
||||||
|
|
||||||
|
if (panelPrincipal.length === 0) {
|
||||||
|
return <Alert severity="info">No hay acciones líderes para mostrar en este momento.</Alert>;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<TableContainer component={Paper}>
|
||||||
|
<Box sx={{ p: 1, m: 0 }}>
|
||||||
|
<Typography variant="caption" sx={{ fontStyle: 'italic', color: 'text.secondary' }}>
|
||||||
|
Última actualización de acciones: {formatFullDateTime(panelPrincipal[0].fechaRegistro)}
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
<Table size="small" aria-label="panel principal merval">
|
||||||
|
<TableHead>
|
||||||
|
<TableRow>
|
||||||
|
<TableCell>Símbolo</TableCell>
|
||||||
|
<TableCell align="right">Precio Actual</TableCell>
|
||||||
|
<TableCell align="right" sx={{ display: { xs: 'none', md: 'table-cell' } }}>Apertura</TableCell>
|
||||||
|
<TableCell align="right" sx={{ display: { xs: 'none', sm: 'table-cell' } }}>Cierre Anterior</TableCell>
|
||||||
|
<TableCell align="center">% Cambio</TableCell>
|
||||||
|
<TableCell align="center">Historial</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
</TableHead>
|
||||||
|
<TableBody>
|
||||||
|
{panelPrincipal.map((row) => (
|
||||||
|
<TableRow key={row.ticker} hover>
|
||||||
|
<TableCell component="th" scope="row"><Typography variant="body2" sx={{ fontWeight: 'bold' }}>{row.ticker}</Typography></TableCell>
|
||||||
|
<TableCell align="right">${formatCurrency(row.precioActual)}</TableCell>
|
||||||
|
<TableCell align="right" sx={{ display: { xs: 'none', md: 'table-cell' } }}>${formatCurrency(row.apertura)}</TableCell>
|
||||||
|
<TableCell align="right" sx={{ display: { xs: 'none', sm: 'table-cell' } }}>${formatCurrency(row.cierreAnterior)}</TableCell>
|
||||||
|
<TableCell align="center"><Variacion value={row.porcentajeCambio} /></TableCell>
|
||||||
|
<TableCell align="center">
|
||||||
|
<IconButton
|
||||||
|
aria-label={`ver historial de ${row.ticker}`}
|
||||||
|
size="small"
|
||||||
|
onClick={(event) => handleOpenModal(row.ticker, event)}
|
||||||
|
sx={{
|
||||||
|
boxShadow: '0 1px 3px rgba(0,0,0,0.1)',
|
||||||
|
transition: 'all 0.2s ease-in-out',
|
||||||
|
'&:hover': { transform: 'scale(1.1)', boxShadow: '0 2px 6px rgba(0,0,0,0.2)' }
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<PiChartLineUpBold size="18" />
|
||||||
|
</IconButton>
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
))}
|
||||||
|
</TableBody>
|
||||||
|
</Table>
|
||||||
|
</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 = () => {
|
export const BolsaLocalWidget = () => {
|
||||||
const { data, loading, error } = useApiData<CotizacionBolsa[]>('/mercados/bolsa/local');
|
// 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 [selectedTicker, setSelectedTicker] = useState<string | null>(null);
|
||||||
|
const triggerButtonRef = useRef<HTMLButtonElement | null>(null);
|
||||||
|
|
||||||
const handleRowClick = (ticker: string) => setSelectedTicker(ticker);
|
const handleOpenModal = (ticker: string, event: React.MouseEvent<HTMLButtonElement>) => {
|
||||||
const handleCloseDialog = () => setSelectedTicker(null);
|
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;
|
||||||
|
|
||||||
const panelPrincipal = data?.filter(d => d.ticker !== '^MERV') || [];
|
if (isLoading) {
|
||||||
|
|
||||||
if (loading) {
|
|
||||||
return <Box sx={{ display: 'flex', justifyContent: 'center', p: 4 }}><CircularProgress /></Box>;
|
return <Box sx={{ display: 'flex', justifyContent: 'center', p: 4 }}><CircularProgress /></Box>;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error) {
|
if (dataError) {
|
||||||
return <Alert severity="error">{error}</Alert>;
|
return <Alert severity="error">{dataError}</Alert>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Si no hay ningún dato en absoluto, mostramos un mensaje final.
|
||||||
if (!data || data.length === 0) {
|
if (!data || data.length === 0) {
|
||||||
return <Alert severity="info">No hay datos disponibles para el mercado local.</Alert>;
|
// 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 (
|
return (
|
||||||
<>
|
<>
|
||||||
|
{/* Si es feriado, mostramos la alerta informativa en la parte superior. */}
|
||||||
{panelPrincipal.length > 0 && (
|
{isHoliday && (
|
||||||
<TableContainer component={Paper}>
|
<Box sx={{ mb: 2 }}>
|
||||||
<Box sx={{ p: 1, m: 0 }}>
|
<HolidayAlert />
|
||||||
<Typography variant="caption" sx={{ fontStyle: 'italic', color: 'text.secondary' }}>
|
</Box>
|
||||||
Última actualización de acciones: {formatFullDateTime(panelPrincipal[0].fechaRegistro)}
|
|
||||||
</Typography>
|
|
||||||
</Box>
|
|
||||||
<Table size="small" aria-label="panel principal merval">
|
|
||||||
<TableHead>
|
|
||||||
<TableRow>
|
|
||||||
<TableCell>Símbolo</TableCell>
|
|
||||||
<TableCell align="right">Precio Actual</TableCell>
|
|
||||||
<TableCell align="right" sx={{ display: { xs: 'none', md: 'table-cell' } }}>Apertura</TableCell>
|
|
||||||
<TableCell align="right" sx={{ display: { xs: 'none', sm: 'table-cell' } }}>Cierre Anterior</TableCell>
|
|
||||||
<TableCell align="center">% Cambio</TableCell>
|
|
||||||
<TableCell align="center">Historial</TableCell>
|
|
||||||
</TableRow>
|
|
||||||
</TableHead>
|
|
||||||
<TableBody>
|
|
||||||
{panelPrincipal.map((row) => (
|
|
||||||
<TableRow key={row.ticker} hover>
|
|
||||||
<TableCell component="th" scope="row"><Typography variant="body2" sx={{ fontWeight: 'bold' }}>{row.ticker}</Typography></TableCell>
|
|
||||||
<TableCell align="right">${formatCurrency(row.precioActual)}</TableCell>
|
|
||||||
<TableCell align="right" sx={{ display: { xs: 'none', md: 'table-cell' } }}>${formatCurrency(row.apertura)}</TableCell>
|
|
||||||
<TableCell align="right" sx={{ display: { xs: 'none', sm: 'table-cell' } }}>${formatCurrency(row.cierreAnterior)}</TableCell>
|
|
||||||
<TableCell align="center"><Variacion value={row.porcentajeCambio} /></TableCell>
|
|
||||||
<TableCell align="center">
|
|
||||||
<IconButton
|
|
||||||
aria-label={`ver historial de ${row.ticker}`}
|
|
||||||
size="small"
|
|
||||||
onClick={() => handleRowClick(row.ticker)}
|
|
||||||
sx={{
|
|
||||||
boxShadow: '0 1px 3px rgba(0,0,0,0.1)',
|
|
||||||
transition: 'all 0.2s ease-in-out',
|
|
||||||
'&:hover': { transform: 'scale(1.1)', boxShadow: '0 2px 6px rgba(0,0,0,0.2)' }
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<PiChartLineUpBold size="18" />
|
|
||||||
</IconButton>
|
|
||||||
</TableCell>
|
|
||||||
</TableRow>
|
|
||||||
))}
|
|
||||||
</TableBody>
|
|
||||||
</Table>
|
|
||||||
</TableContainer>
|
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{/* 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>
|
||||||
|
|||||||
@@ -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" />;
|
||||||
@@ -68,7 +71,7 @@ const GranoCard = ({ grano, onChartClick }: { grano: CotizacionGrano, onChartCli
|
|||||||
<Box sx={{ display: 'flex', alignItems: 'center', mb: 1, pr: 5 }}>
|
<Box sx={{ display: 'flex', alignItems: 'center', mb: 1, pr: 5 }}>
|
||||||
{getGrainIcon(grano.nombre)}
|
{getGrainIcon(grano.nombre)}
|
||||||
<Typography variant="h6" component="h3" sx={{ fontWeight: 'bold', ml: 1 }}>
|
<Typography variant="h6" component="h3" sx={{ fontWeight: 'bold', ml: 1 }}>
|
||||||
{grano.nombre}
|
{grano.nombre}
|
||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
@@ -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);
|
||||||
|
|
||||||
@@ -107,11 +111,12 @@ export const GranosCardWidget = () => {
|
|||||||
const handleCloseDialog = () => {
|
const handleCloseDialog = () => {
|
||||||
setSelectedGrano(null);
|
setSelectedGrano(null);
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
triggerButtonRef.current?.focus();
|
triggerButtonRef.current?.focus();
|
||||||
}, 0);
|
}, 0);
|
||||||
};
|
};
|
||||||
|
|
||||||
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,41 +130,47 @@ export const GranosCardWidget = () => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Box
|
{/* Si es feriado (y la comprobación ha terminado), mostramos la alerta encima */}
|
||||||
sx={{
|
{isHoliday === true && (
|
||||||
display: 'flex',
|
<Box sx={{ mb: 2 }}> {/* Añadimos un margen inferior a la alerta */}
|
||||||
flexWrap: 'wrap',
|
<HolidayAlert />
|
||||||
// Usamos el objeto para definir gaps responsivos
|
</Box>
|
||||||
gap: {
|
)}
|
||||||
xs: 4, // 16px de gap en pantallas extra pequeñas (afecta el espaciado vertical)
|
<Box
|
||||||
sm: 4, // 16px en pantallas pequeñas
|
sx={{
|
||||||
md: 3, // 24px en pantallas medianas (afecta el espaciado horizontal)
|
display: 'flex',
|
||||||
},
|
flexWrap: 'wrap',
|
||||||
justifyContent: 'center'
|
// Usamos el objeto para definir gaps responsivos
|
||||||
}}
|
gap: {
|
||||||
>
|
xs: 4, // 16px de gap en pantallas extra pequeñas (afecta el espaciado vertical)
|
||||||
{data.map((grano) => (
|
sm: 4, // 16px en pantallas pequeñas
|
||||||
|
md: 3, // 24px en pantallas medianas (afecta el espaciado horizontal)
|
||||||
|
},
|
||||||
|
justifyContent: 'center'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{data.map((grano) => (
|
||||||
<GranoCard key={grano.nombre} grano={grano} onChartClick={(event) => handleChartClick(grano.nombre, event)} />
|
<GranoCard key={grano.nombre} grano={grano} onChartClick={(event) => handleChartClick(grano.nombre, event)} />
|
||||||
))}
|
))}
|
||||||
</Box>
|
</Box>
|
||||||
<Dialog open={Boolean(selectedGrano)} onClose={handleCloseDialog} maxWidth="md" fullWidth sx={{ '& .MuiDialog-paper': { overflow: 'visible' } }}>
|
<Dialog open={Boolean(selectedGrano)} onClose={handleCloseDialog} maxWidth="md" fullWidth sx={{ '& .MuiDialog-paper': { overflow: 'visible' } }}>
|
||||||
<IconButton
|
<IconButton
|
||||||
aria-label="close"
|
aria-label="close"
|
||||||
onClick={handleCloseDialog}
|
onClick={handleCloseDialog}
|
||||||
sx={{
|
sx={{
|
||||||
position: 'absolute',
|
position: 'absolute',
|
||||||
top: -15, // Mueve el botón hacia arriba, fuera del Dialog
|
top: -15, // Mueve el botón hacia arriba, fuera del Dialog
|
||||||
right: -15, // Mueve el botón hacia la derecha, fuera del Dialog
|
right: -15, // Mueve el botón hacia la derecha, fuera del Dialog
|
||||||
color: (theme) => theme.palette.grey[500],
|
color: (theme) => theme.palette.grey[500],
|
||||||
backgroundColor: 'white',
|
backgroundColor: 'white',
|
||||||
boxShadow: 3, // Añade una sombra para que destaque
|
boxShadow: 3, // Añade una sombra para que destaque
|
||||||
'&:hover': {
|
'&:hover': {
|
||||||
backgroundColor: 'grey.100', // Un leve cambio de color al pasar el mouse
|
backgroundColor: 'grey.100', // Un leve cambio de color al pasar el mouse
|
||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<CloseIcon />
|
<CloseIcon />
|
||||||
</IconButton>
|
</IconButton>
|
||||||
<DialogTitle sx={{ m: 0, p: 2 }}>Mensual de {selectedGrano}</DialogTitle>
|
<DialogTitle sx={{ m: 0, p: 2 }}>Mensual de {selectedGrano}</DialogTitle>
|
||||||
<DialogContent dividers>
|
<DialogContent dividers>
|
||||||
{selectedGrano && <GrainsHistoricalChartWidget nombre={selectedGrano} />}
|
{selectedGrano && <GrainsHistoricalChartWidget nombre={selectedGrano} />}
|
||||||
|
|||||||
@@ -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} />
|
||||||
|
|||||||
@@ -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} />
|
||||||
|
|||||||
10
frontend/src/components/common/HolidayAlert.tsx
Normal file
10
frontend/src/components/common/HolidayAlert.tsx
Normal 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>
|
||||||
|
);
|
||||||
|
};
|
||||||
24
frontend/src/hooks/useIsHoliday.ts
Normal file
24
frontend/src/hooks/useIsHoliday.ts
Normal 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;
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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
|
||||||
|
|||||||
10
src/Mercados.Core/Entities/MercadoFeriado.cs
Normal file
10
src/Mercados.Core/Entities/MercadoFeriado.cs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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()
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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" />
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
16
src/Mercados.Infrastructure/Services/IHolidayService.cs
Normal file
16
src/Mercados.Infrastructure/Services/IHolidayService.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,22 +14,23 @@ namespace Mercados.Worker
|
|||||||
private readonly ILogger<DataFetchingService> _logger;
|
private readonly ILogger<DataFetchingService> _logger;
|
||||||
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,44 +66,86 @@ 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.
|
||||||
var utcNow = DateTime.UtcNow;
|
var utcNow = DateTime.UtcNow;
|
||||||
_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)
|
||||||
{
|
{
|
||||||
await RunFetcherByNameAsync("MercadoAgroganadero", stoppingToken);
|
// Comprueba si NO es feriado en Argentina para ejecutar
|
||||||
// Inmediatamente después de ejecutar, calculamos la SIGUIENTE ocurrencia.
|
if (!await IsMarketHolidayAsync("BA", nowInArgentina))
|
||||||
|
{
|
||||||
|
await RunFetcherByNameAsync("MercadoAgroganadero", stoppingToken);
|
||||||
|
}
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
await RunFetcherByNameAsync("BCR", stoppingToken);
|
if (!await IsMarketHolidayAsync("BA", nowInArgentina))
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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();
|
||||||
|
|||||||
@@ -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": "",
|
||||||
|
|||||||
Reference in New Issue
Block a user