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

@@ -5,7 +5,8 @@ async function calculateBrandMetrics(startTime, totalProducts, processedCount =
const connection = await getConnection();
let success = false;
const BATCH_SIZE = 5000;
let myProcessedProducts = 0; // Not *directly* processing products, tracking brands
try {
// Get last calculation timestamp
const [lastCalc] = await connection.query(`
@@ -26,12 +27,12 @@ async function calculateBrandMetrics(startTime, totalProducts, processedCount =
OR o.id IS NOT NULL
)
`, [lastCalculationTime, lastCalculationTime]);
const totalBrands = brandCount[0].count;
const totalBrands = brandCount[0].count; // Track total *brands*
if (totalBrands === 0) {
console.log('No brands need metric updates');
return {
processedProducts: 0,
processedProducts: 0, // Not directly processing products
processedOrders: 0,
processedPurchaseOrders: 0,
success: true
@@ -42,12 +43,12 @@ async function calculateBrandMetrics(startTime, totalProducts, processedCount =
outputProgress({
status: 'cancelled',
operation: 'Brand metrics calculation cancelled',
current: processedCount,
total: totalBrands,
current: processedCount, // Use passed-in value
total: totalBrands, // Report total *brands*
elapsed: formatElapsedTime(startTime),
remaining: null,
rate: calculateRate(startTime, processedCount),
percentage: ((processedCount / totalBrands) * 100).toFixed(1),
percentage: ((processedCount / totalBrands) * 100).toFixed(1), // Base on brands
timing: {
start_time: new Date(startTime).toISOString(),
end_time: new Date().toISOString(),
@@ -55,7 +56,7 @@ async function calculateBrandMetrics(startTime, totalProducts, processedCount =
}
});
return {
processedProducts: processedCount,
processedProducts: 0, // Not directly processing products
processedOrders: 0,
processedPurchaseOrders: 0,
success
@@ -65,12 +66,12 @@ async function calculateBrandMetrics(startTime, totalProducts, processedCount =
outputProgress({
status: 'running',
operation: 'Starting brand metrics calculation',
current: processedCount,
total: totalBrands,
current: processedCount, // Use passed-in value
total: totalBrands, // Report total *brands*
elapsed: formatElapsedTime(startTime),
remaining: estimateRemaining(startTime, processedCount, totalBrands),
rate: calculateRate(startTime, processedCount),
percentage: ((processedCount / totalBrands) * 100).toFixed(1),
percentage: ((processedCount / totalBrands) * 100).toFixed(1), // Base on brands
timing: {
start_time: new Date(startTime).toISOString(),
end_time: new Date().toISOString(),
@@ -80,6 +81,7 @@ async function calculateBrandMetrics(startTime, totalProducts, processedCount =
// Process in batches
let lastBrand = '';
let processedBrands = 0; // Track processed brands
while (true) {
if (isCancelled) break;
@@ -243,17 +245,17 @@ async function calculateBrandMetrics(startTime, totalProducts, processedCount =
await connection.query('DROP TEMPORARY TABLE IF EXISTS temp_sales_stats');
lastBrand = batch[batch.length - 1].brand;
processedCount += batch.length;
processedBrands += batch.length; // Increment processed *brands*
outputProgress({
status: 'running',
operation: 'Processing brand metrics batch',
current: processedCount,
total: totalBrands,
current: processedCount + processedBrands, // Use cumulative brand count
total: totalBrands, // Report total *brands*
elapsed: formatElapsedTime(startTime),
remaining: estimateRemaining(startTime, processedCount, totalBrands),
rate: calculateRate(startTime, processedCount),
percentage: ((processedCount / totalBrands) * 100).toFixed(1),
remaining: estimateRemaining(startTime, processedCount + processedBrands, totalBrands),
rate: calculateRate(startTime, processedCount + processedBrands),
percentage: (((processedCount + processedBrands) / totalBrands) * 100).toFixed(1), // Base on brands
timing: {
start_time: new Date(startTime).toISOString(),
end_time: new Date().toISOString(),
@@ -264,7 +266,7 @@ async function calculateBrandMetrics(startTime, totalProducts, processedCount =
// If we get here, everything completed successfully
success = true;
// Update calculate_status
await connection.query(`
INSERT INTO calculate_status (module_name, last_calculation_timestamp)
@@ -273,7 +275,7 @@ async function calculateBrandMetrics(startTime, totalProducts, processedCount =
`);
return {
processedProducts: processedCount,
processedProducts: 0, // Not directly processing products
processedOrders: 0,
processedPurchaseOrders: 0,
success