Apply gemini suggested calculate improvements
This commit is contained in:
@@ -5,7 +5,8 @@ async function calculateVendorMetrics(startTime, totalProducts, processedCount =
|
||||
const connection = await getConnection();
|
||||
let success = false;
|
||||
const BATCH_SIZE = 5000;
|
||||
|
||||
let myProcessedProducts = 0; // Not directly processing products, but we'll track vendors
|
||||
|
||||
try {
|
||||
// Get last calculation timestamp
|
||||
const [lastCalc] = await connection.query(`
|
||||
@@ -33,12 +34,12 @@ async function calculateVendorMetrics(startTime, totalProducts, processedCount =
|
||||
)
|
||||
)
|
||||
`, [lastCalculationTime, lastCalculationTime]);
|
||||
const totalVendors = vendorCount[0].count;
|
||||
const totalVendors = vendorCount[0].count; // Track total *vendors*
|
||||
|
||||
if (totalVendors === 0) {
|
||||
console.log('No vendors need metric updates');
|
||||
return {
|
||||
processedProducts: 0,
|
||||
processedProducts: 0, // No products directly processed
|
||||
processedOrders: 0,
|
||||
processedPurchaseOrders: 0,
|
||||
success: true
|
||||
@@ -49,12 +50,12 @@ async function calculateVendorMetrics(startTime, totalProducts, processedCount =
|
||||
outputProgress({
|
||||
status: 'cancelled',
|
||||
operation: 'Vendor metrics calculation cancelled',
|
||||
current: processedCount,
|
||||
total: totalVendors,
|
||||
current: processedCount, // Use passed-in value (for consistency)
|
||||
total: totalVendors, // Report total *vendors*
|
||||
elapsed: formatElapsedTime(startTime),
|
||||
remaining: null,
|
||||
rate: calculateRate(startTime, processedCount),
|
||||
percentage: ((processedCount / totalVendors) * 100).toFixed(1),
|
||||
percentage: ((processedCount / totalVendors) * 100).toFixed(1), // Base on vendors
|
||||
timing: {
|
||||
start_time: new Date(startTime).toISOString(),
|
||||
end_time: new Date().toISOString(),
|
||||
@@ -62,7 +63,7 @@ async function calculateVendorMetrics(startTime, totalProducts, processedCount =
|
||||
}
|
||||
});
|
||||
return {
|
||||
processedProducts: processedCount,
|
||||
processedProducts: 0, // No products directly processed
|
||||
processedOrders: 0,
|
||||
processedPurchaseOrders: 0,
|
||||
success
|
||||
@@ -72,12 +73,12 @@ async function calculateVendorMetrics(startTime, totalProducts, processedCount =
|
||||
outputProgress({
|
||||
status: 'running',
|
||||
operation: 'Starting vendor metrics calculation',
|
||||
current: processedCount,
|
||||
total: totalVendors,
|
||||
current: processedCount, // Use passed-in value
|
||||
total: totalVendors, // Report total *vendors*
|
||||
elapsed: formatElapsedTime(startTime),
|
||||
remaining: estimateRemaining(startTime, processedCount, totalVendors),
|
||||
rate: calculateRate(startTime, processedCount),
|
||||
percentage: ((processedCount / totalVendors) * 100).toFixed(1),
|
||||
percentage: ((processedCount / totalVendors) * 100).toFixed(1), // Base on vendors
|
||||
timing: {
|
||||
start_time: new Date(startTime).toISOString(),
|
||||
end_time: new Date().toISOString(),
|
||||
@@ -87,6 +88,7 @@ async function calculateVendorMetrics(startTime, totalProducts, processedCount =
|
||||
|
||||
// Process in batches
|
||||
let lastVendor = '';
|
||||
let processedVendors = 0; // Track processed vendors
|
||||
while (true) {
|
||||
if (isCancelled) break;
|
||||
|
||||
@@ -119,7 +121,7 @@ async function calculateVendorMetrics(startTime, totalProducts, processedCount =
|
||||
// Create temporary tables with optimized structure and indexes
|
||||
await connection.query('DROP TEMPORARY TABLE IF EXISTS temp_purchase_stats');
|
||||
await connection.query('DROP TEMPORARY TABLE IF EXISTS temp_product_stats');
|
||||
|
||||
|
||||
await connection.query(`
|
||||
CREATE TEMPORARY TABLE temp_purchase_stats (
|
||||
vendor VARCHAR(100) NOT NULL,
|
||||
@@ -253,17 +255,17 @@ async function calculateVendorMetrics(startTime, totalProducts, processedCount =
|
||||
await connection.query('DROP TEMPORARY TABLE IF EXISTS temp_product_stats');
|
||||
|
||||
lastVendor = batch[batch.length - 1].vendor;
|
||||
processedCount += batch.length;
|
||||
processedVendors += batch.length; // Increment processed *vendors*
|
||||
|
||||
outputProgress({
|
||||
status: 'running',
|
||||
operation: 'Processing vendor metrics batch',
|
||||
current: processedCount,
|
||||
total: totalVendors,
|
||||
current: processedCount + processedVendors, // Use cumulative vendor count
|
||||
total: totalVendors, // Report total *vendors*
|
||||
elapsed: formatElapsedTime(startTime),
|
||||
remaining: estimateRemaining(startTime, processedCount, totalVendors),
|
||||
rate: calculateRate(startTime, processedCount),
|
||||
percentage: ((processedCount / totalVendors) * 100).toFixed(1),
|
||||
remaining: estimateRemaining(startTime, processedCount + processedVendors, totalVendors),
|
||||
rate: calculateRate(startTime, processedCount + processedVendors),
|
||||
percentage: (((processedCount + processedVendors) / totalVendors) * 100).toFixed(1), // Base on vendors
|
||||
timing: {
|
||||
start_time: new Date(startTime).toISOString(),
|
||||
end_time: new Date().toISOString(),
|
||||
@@ -274,7 +276,7 @@ async function calculateVendorMetrics(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)
|
||||
@@ -283,7 +285,7 @@ async function calculateVendorMetrics(startTime, totalProducts, processedCount =
|
||||
`);
|
||||
|
||||
return {
|
||||
processedProducts: processedCount,
|
||||
processedProducts: 0, // No products directly processed
|
||||
processedOrders: 0,
|
||||
processedPurchaseOrders: 0,
|
||||
success
|
||||
|
||||
Reference in New Issue
Block a user