diff --git a/inventory-server/scripts/calculate-metrics.js b/inventory-server/scripts/calculate-metrics.js index 2edf071..38120b1 100644 --- a/inventory-server/scripts/calculate-metrics.js +++ b/inventory-server/scripts/calculate-metrics.js @@ -399,14 +399,14 @@ async function calculateMetrics() { `); let processedCount = processedProducts; - outputProgress({ + global.outputProgress({ status: 'running', operation: 'Creating revenue rankings', current: processedCount, total: totalProducts, - elapsed: formatElapsedTime(startTime), - remaining: estimateRemaining(startTime, processedCount, totalProducts), - rate: calculateRate(startTime, processedCount), + elapsed: global.formatElapsedTime(startTime), + remaining: global.estimateRemaining(startTime, processedCount, totalProducts), + rate: global.calculateRate(startTime, processedCount), percentage: ((processedCount / totalProducts) * 100).toFixed(1), timing: { start_time: new Date(startTime).toISOString(), @@ -453,7 +453,7 @@ async function calculateMetrics() { // const max_rank = totalCount; // Store max_rank for use in classification // ABC classification progress tracking - let abcProcessedCount = 0; + // let abcProcessedCount = 0; // No longer needed - use processedProducts directly const batchSize = 5000; let lastProgressUpdate = Date.now(); const progressUpdateInterval = 1000; // Update every second @@ -501,11 +501,11 @@ async function calculateMetrics() { WHERE pm.pid IN (?) `, [abcThresholds.a_threshold, abcThresholds.b_threshold, pids.map(row => row.pid)]); - abcProcessedCount += pids.length; // Use pids.length, more accurate + // abcProcessedCount += pids.length; // No longer needed processedProducts += pids.length; // Add to the main processedProducts // Calculate progress ensuring valid numbers - const currentProgress = Math.floor(totalProducts * (0.99 + (abcProcessedCount / (totalProducts || 1)) * 0.01)); + const currentProgress = Math.floor(totalProducts * (0.99 + (processedProducts / (totalProducts || 1)) * 0.01)); processedProducts = Number(currentProgress) || processedProducts || 0; // Only update progress at most once per second @@ -513,14 +513,14 @@ async function calculateMetrics() { if (now - lastProgressUpdate >= progressUpdateInterval) { const progress = ensureValidProgress(processedProducts, totalProducts); - outputProgress({ + global.outputProgress({ status: 'running', operation: 'ABC classification progress', current: progress.current, total: progress.total, - elapsed: formatElapsedTime(startTime), - remaining: estimateRemaining(startTime, progress.current, progress.total), - rate: calculateRate(startTime, progress.current), + elapsed: global.formatElapsedTime(startTime), + remaining: global.estimateRemaining(startTime, progress.current, progress.total), + rate: global.calculateRate(startTime, progress.current), percentage: progress.percentage, timing: { start_time: new Date(startTime).toISOString(), @@ -556,14 +556,14 @@ async function calculateMetrics() { const finalProgress = ensureValidProgress(totalProducts, totalProducts); // Final success message - outputProgress({ + global.outputProgress({ status: 'complete', operation: 'Metrics calculation complete', current: finalProgress.current, total: finalProgress.total, - elapsed: formatElapsedTime(startTime), + elapsed: global.formatElapsedTime(startTime), remaining: '0s', - rate: calculateRate(startTime, finalProgress.current), + rate: global.calculateRate(startTime, finalProgress.current), percentage: '100', timing: { start_time: new Date(startTime).toISOString(), diff --git a/inventory-server/scripts/metrics/product-metrics.js b/inventory-server/scripts/metrics/product-metrics.js index f38ca1e..f364cf5 100644 --- a/inventory-server/scripts/metrics/product-metrics.js +++ b/inventory-server/scripts/metrics/product-metrics.js @@ -40,7 +40,7 @@ async function calculateProductMetrics(startTime, totalProducts, processedCount const SKIP_PRODUCT_TIME_AGGREGATES = 0; if (isCancelled) { - outputProgress({ + global.outputProgress({ status: 'cancelled', operation: 'Product metrics calculation cancelled', current: processedCount, @@ -276,18 +276,17 @@ async function calculateProductMetrics(startTime, totalProducts, processedCount ]); lastPid = batch[batch.length - 1].pid; - processedCount += batch.length; myProcessedProducts += batch.length; // Increment the *module's* count outputProgress({ status: 'running', operation: 'Processing base metrics batch', - current: processedCount, + current: processedCount + myProcessedProducts, // Show cumulative progress total: totalProducts, elapsed: formatElapsedTime(startTime), - remaining: estimateRemaining(startTime, processedCount, totalProducts), - rate: calculateRate(startTime, processedCount), - percentage: ((processedCount / totalProducts) * 100).toFixed(1), + remaining: estimateRemaining(startTime, processedCount + myProcessedProducts, totalProducts), + rate: calculateRate(startTime, processedCount + myProcessedProducts), + percentage: (((processedCount + myProcessedProducts) / totalProducts) * 100).toFixed(1), timing: { start_time: new Date(startTime).toISOString(), end_time: new Date().toISOString(), diff --git a/inventory-server/scripts/metrics/sales-forecasts.js b/inventory-server/scripts/metrics/sales-forecasts.js index 16c78e8..999e8c9 100644 --- a/inventory-server/scripts/metrics/sales-forecasts.js +++ b/inventory-server/scripts/metrics/sales-forecasts.js @@ -48,7 +48,7 @@ async function calculateSalesForecasts(startTime, totalProducts, processedCount elapsed: formatElapsedTime(startTime), remaining: null, rate: calculateRate(startTime, processedCount), - percentage: ((processedCount / totalProductsToUpdate) * 100).toFixed(1), + percentage: ((processedCount / totalProducts) * 100).toFixed(1), timing: { start_time: new Date(startTime).toISOString(), end_time: new Date().toISOString(), @@ -261,12 +261,12 @@ async function calculateSalesForecasts(startTime, totalProducts, processedCount outputProgress({ status: 'running', operation: 'Processing sales forecast batch', - current: processedCount, + current: processedCount + myProcessedProducts, total: totalProducts, elapsed: formatElapsedTime(startTime), - remaining: estimateRemaining(startTime, processedCount, totalProducts), - rate: calculateRate(startTime, processedCount), - percentage: ((processedCount / totalProductsToUpdate) * 100).toFixed(1), + remaining: estimateRemaining(startTime, processedCount + myProcessedProducts, totalProducts), + rate: calculateRate(startTime, processedCount + myProcessedProducts), + percentage: (((processedCount + myProcessedProducts) / totalProducts) * 100).toFixed(1), timing: { start_time: new Date(startTime).toISOString(), end_time: new Date().toISOString(),