REFACTOR external service and improve websocket training

This commit is contained in:
Urtzi Alfaro
2025-10-09 14:11:02 +02:00
parent 7c72f83c51
commit 3c689b4f98
111 changed files with 13289 additions and 2374 deletions

View File

@@ -27,7 +27,9 @@ const ForecastingPage: React.FC = () => {
const startDate = new Date();
startDate.setDate(startDate.getDate() - parseInt(forecastPeriod));
// Fetch existing forecasts
// NOTE: We don't need to fetch forecasts from API because we already have them
// from the multi-day forecast response stored in currentForecastData
// Keeping this disabled to avoid unnecessary API calls
const {
data: forecastsData,
isLoading: forecastsLoading,
@@ -38,7 +40,7 @@ const ForecastingPage: React.FC = () => {
...(selectedProduct && { inventory_product_id: selectedProduct }),
limit: 100
}, {
enabled: !!tenantId && hasGeneratedForecast && !!selectedProduct
enabled: false // Disabled - we use currentForecastData from multi-day API response
});
@@ -72,12 +74,15 @@ const ForecastingPage: React.FC = () => {
// Build products list from ingredients that have trained models
const products = useMemo(() => {
if (!ingredientsData || !modelsData?.models) {
if (!ingredientsData || !modelsData) {
return [];
}
// Handle both array and paginated response formats
const modelsList = Array.isArray(modelsData) ? modelsData : (modelsData.models || modelsData.items || []);
// Get inventory product IDs that have trained models
const modelProductIds = new Set(modelsData.models.map(model => model.inventory_product_id));
const modelProductIds = new Set(modelsList.map((model: any) => model.inventory_product_id));
// Filter ingredients to only those with models
const ingredientsWithModels = ingredientsData.filter(ingredient =>
@@ -130,10 +135,10 @@ const ForecastingPage: React.FC = () => {
}
};
// Use either current forecast data or fetched data
const forecasts = currentForecastData.length > 0 ? currentForecastData : (forecastsData?.forecasts || []);
const isLoading = forecastsLoading || ingredientsLoading || modelsLoading || isGenerating;
const hasError = forecastsError || ingredientsError || modelsError;
// Use current forecast data from multi-day API response
const forecasts = currentForecastData;
const isLoading = ingredientsLoading || modelsLoading || isGenerating;
const hasError = ingredientsError || modelsError;
// Calculate metrics from real data
const totalDemand = forecasts.reduce((sum, f) => sum + f.predicted_demand, 0);