Apply gemini suggested calculate improvements

This commit is contained in:
2025-02-10 15:20:22 -05:00
parent 09f7103472
commit 90379386d6
8 changed files with 211 additions and 250 deletions

View File

@@ -4,6 +4,7 @@ const { getConnection } = require('./utils/db');
async function calculateSalesForecasts(startTime, totalProducts, processedCount = 0, isCancelled = false) {
const connection = await getConnection();
let success = false;
let myProcessedProducts = 0; // Track products processed *within this module*
const BATCH_SIZE = 5000;
try {
@@ -43,7 +44,7 @@ async function calculateSalesForecasts(startTime, totalProducts, processedCount
status: 'cancelled',
operation: 'Sales forecast calculation cancelled',
current: processedCount,
total: totalProductsToUpdate,
total: totalProducts,
elapsed: formatElapsedTime(startTime),
remaining: null,
rate: calculateRate(startTime, processedCount),
@@ -55,7 +56,7 @@ async function calculateSalesForecasts(startTime, totalProducts, processedCount
}
});
return {
processedProducts: processedCount,
processedProducts: myProcessedProducts,
processedOrders: 0,
processedPurchaseOrders: 0,
success
@@ -66,9 +67,9 @@ async function calculateSalesForecasts(startTime, totalProducts, processedCount
status: 'running',
operation: 'Starting sales forecast calculation',
current: processedCount,
total: totalProductsToUpdate,
total: totalProducts,
elapsed: formatElapsedTime(startTime),
remaining: estimateRemaining(startTime, processedCount, totalProductsToUpdate),
remaining: estimateRemaining(startTime, processedCount, totalProducts),
rate: calculateRate(startTime, processedCount),
percentage: ((processedCount / totalProductsToUpdate) * 100).toFixed(1),
timing: {
@@ -255,15 +256,15 @@ async function calculateSalesForecasts(startTime, totalProducts, processedCount
await connection.query('DROP TEMPORARY TABLE IF EXISTS temp_confidence_calc');
lastPid = batch[batch.length - 1].pid;
processedCount += batch.length;
myProcessedProducts += batch.length;
outputProgress({
status: 'running',
operation: 'Processing sales forecast batch',
current: processedCount,
total: totalProductsToUpdate,
total: totalProducts,
elapsed: formatElapsedTime(startTime),
remaining: estimateRemaining(startTime, processedCount, totalProductsToUpdate),
remaining: estimateRemaining(startTime, processedCount, totalProducts),
rate: calculateRate(startTime, processedCount),
percentage: ((processedCount / totalProductsToUpdate) * 100).toFixed(1),
timing: {
@@ -285,7 +286,7 @@ async function calculateSalesForecasts(startTime, totalProducts, processedCount
`);
return {
processedProducts: processedCount,
processedProducts: myProcessedProducts,
processedOrders: 0,
processedPurchaseOrders: 0,
success