Apply gemini suggested calculate improvements
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -5,7 +5,8 @@ async function calculateCategoryMetrics(startTime, totalProducts, processedCount
|
||||
const connection = await getConnection();
|
||||
let success = false;
|
||||
const BATCH_SIZE = 5000;
|
||||
|
||||
let myProcessedProducts = 0; // Not *directly* processing products, but tracking categories
|
||||
|
||||
try {
|
||||
// Get last calculation timestamp
|
||||
const [lastCalc] = await connection.query(`
|
||||
@@ -28,12 +29,12 @@ async function calculateCategoryMetrics(startTime, totalProducts, processedCount
|
||||
OR o.id IS NOT NULL
|
||||
)
|
||||
`, [lastCalculationTime, lastCalculationTime]);
|
||||
const totalCategories = categoryCount[0].count;
|
||||
const totalCategories = categoryCount[0].count; // Track total *categories*
|
||||
|
||||
if (totalCategories === 0) {
|
||||
console.log('No categories need metric updates');
|
||||
return {
|
||||
processedProducts: 0,
|
||||
processedProducts: 0, // Not directly processing products
|
||||
processedOrders: 0,
|
||||
processedPurchaseOrders: 0,
|
||||
success: true
|
||||
@@ -44,12 +45,12 @@ async function calculateCategoryMetrics(startTime, totalProducts, processedCount
|
||||
outputProgress({
|
||||
status: 'cancelled',
|
||||
operation: 'Category metrics calculation cancelled',
|
||||
current: processedCount,
|
||||
total: totalCategories,
|
||||
current: processedCount, // Use passed-in value
|
||||
total: totalCategories, // Report total *categories*
|
||||
elapsed: formatElapsedTime(startTime),
|
||||
remaining: null,
|
||||
rate: calculateRate(startTime, processedCount),
|
||||
percentage: ((processedCount / totalCategories) * 100).toFixed(1),
|
||||
percentage: ((processedCount / totalCategories) * 100).toFixed(1), // Base on categories
|
||||
timing: {
|
||||
start_time: new Date(startTime).toISOString(),
|
||||
end_time: new Date().toISOString(),
|
||||
@@ -57,7 +58,7 @@ async function calculateCategoryMetrics(startTime, totalProducts, processedCount
|
||||
}
|
||||
});
|
||||
return {
|
||||
processedProducts: processedCount,
|
||||
processedProducts: 0, // Not directly processing products
|
||||
processedOrders: 0,
|
||||
processedPurchaseOrders: 0,
|
||||
success
|
||||
@@ -67,12 +68,12 @@ async function calculateCategoryMetrics(startTime, totalProducts, processedCount
|
||||
outputProgress({
|
||||
status: 'running',
|
||||
operation: 'Starting category metrics calculation',
|
||||
current: processedCount,
|
||||
total: totalCategories,
|
||||
current: processedCount, // Use passed-in value
|
||||
total: totalCategories, // Report total *categories*
|
||||
elapsed: formatElapsedTime(startTime),
|
||||
remaining: estimateRemaining(startTime, processedCount, totalCategories),
|
||||
rate: calculateRate(startTime, processedCount),
|
||||
percentage: ((processedCount / totalCategories) * 100).toFixed(1),
|
||||
percentage: ((processedCount / totalCategories) * 100).toFixed(1), // Base on categories
|
||||
timing: {
|
||||
start_time: new Date(startTime).toISOString(),
|
||||
end_time: new Date().toISOString(),
|
||||
@@ -82,6 +83,7 @@ async function calculateCategoryMetrics(startTime, totalProducts, processedCount
|
||||
|
||||
// Process in batches
|
||||
let lastCatId = 0;
|
||||
let processedCategories = 0; // Track processed categories
|
||||
while (true) {
|
||||
if (isCancelled) break;
|
||||
|
||||
@@ -247,17 +249,17 @@ async function calculateCategoryMetrics(startTime, totalProducts, processedCount
|
||||
await connection.query('DROP TEMPORARY TABLE IF EXISTS temp_sales_stats');
|
||||
|
||||
lastCatId = batch[batch.length - 1].cat_id;
|
||||
processedCount += batch.length;
|
||||
processedCategories += batch.length; // Increment processed *categories*
|
||||
|
||||
outputProgress({
|
||||
status: 'running',
|
||||
operation: 'Processing category metrics batch',
|
||||
current: processedCount,
|
||||
total: totalCategories,
|
||||
current: processedCount + processedCategories, // Use cumulative category count
|
||||
total: totalCategories, // Report total *categories*
|
||||
elapsed: formatElapsedTime(startTime),
|
||||
remaining: estimateRemaining(startTime, processedCount, totalCategories),
|
||||
rate: calculateRate(startTime, processedCount),
|
||||
percentage: ((processedCount / totalCategories) * 100).toFixed(1),
|
||||
remaining: estimateRemaining(startTime, processedCount + processedCategories, totalCategories),
|
||||
rate: calculateRate(startTime, processedCount + processedCategories),
|
||||
percentage: (((processedCount + processedCategories) / totalCategories) * 100).toFixed(1), // Base on categories
|
||||
timing: {
|
||||
start_time: new Date(startTime).toISOString(),
|
||||
end_time: new Date().toISOString(),
|
||||
@@ -268,7 +270,7 @@ async function calculateCategoryMetrics(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)
|
||||
@@ -277,7 +279,7 @@ async function calculateCategoryMetrics(startTime, totalProducts, processedCount
|
||||
`);
|
||||
|
||||
return {
|
||||
processedProducts: processedCount,
|
||||
processedProducts: 0, // Not directly processing products
|
||||
processedOrders: 0,
|
||||
processedPurchaseOrders: 0,
|
||||
success
|
||||
|
||||
@@ -5,6 +5,7 @@ async function calculateFinancialMetrics(startTime, totalProducts, processedCoun
|
||||
const connection = await getConnection();
|
||||
let success = false;
|
||||
const BATCH_SIZE = 5000;
|
||||
let myProcessedProducts = 0; // Track products processed *within this module*
|
||||
|
||||
try {
|
||||
// Get last calculation timestamp
|
||||
@@ -54,7 +55,7 @@ async function calculateFinancialMetrics(startTime, totalProducts, processedCoun
|
||||
}
|
||||
});
|
||||
return {
|
||||
processedProducts: processedCount,
|
||||
processedProducts: myProcessedProducts,
|
||||
processedOrders: 0,
|
||||
processedPurchaseOrders: 0,
|
||||
success
|
||||
@@ -133,17 +134,17 @@ async function calculateFinancialMetrics(startTime, totalProducts, processedCoun
|
||||
`, [batch.map(row => row.pid)]);
|
||||
|
||||
lastPid = batch[batch.length - 1].pid;
|
||||
processedCount += batch.length;
|
||||
myProcessedProducts += batch.length;
|
||||
|
||||
outputProgress({
|
||||
status: 'running',
|
||||
operation: 'Processing financial metrics batch',
|
||||
current: processedCount,
|
||||
current: processedCount + myProcessedProducts,
|
||||
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(),
|
||||
@@ -163,7 +164,7 @@ async function calculateFinancialMetrics(startTime, totalProducts, processedCoun
|
||||
`);
|
||||
|
||||
return {
|
||||
processedProducts: processedCount,
|
||||
processedProducts: myProcessedProducts,
|
||||
processedOrders: 0,
|
||||
processedPurchaseOrders: 0,
|
||||
success
|
||||
|
||||
@@ -13,6 +13,7 @@ async function calculateProductMetrics(startTime, totalProducts, processedCount
|
||||
const connection = await getConnection();
|
||||
let success = false;
|
||||
let processedOrders = 0;
|
||||
let myProcessedProducts = 0; // Track products processed *within this module*
|
||||
const BATCH_SIZE = 5000;
|
||||
|
||||
try {
|
||||
@@ -24,24 +25,10 @@ async function calculateProductMetrics(startTime, totalProducts, processedCount
|
||||
`);
|
||||
const lastCalculationTime = lastCalc[0]?.last_calculation_timestamp || '1970-01-01';
|
||||
|
||||
// Get total product count if not provided
|
||||
if (!totalProducts) {
|
||||
const [productCount] = await connection.query(`
|
||||
SELECT COUNT(DISTINCT p.pid) as count
|
||||
FROM products p
|
||||
LEFT JOIN orders o ON p.pid = o.pid AND o.updated > ?
|
||||
LEFT JOIN purchase_orders po ON p.pid = po.pid AND po.updated > ?
|
||||
WHERE p.updated > ?
|
||||
OR o.pid IS NOT NULL
|
||||
OR po.pid IS NOT NULL
|
||||
`, [lastCalculationTime, lastCalculationTime, lastCalculationTime]);
|
||||
totalProducts = productCount[0].count;
|
||||
}
|
||||
|
||||
if (totalProducts === 0) {
|
||||
console.log('No products need updating');
|
||||
return {
|
||||
processedProducts: 0,
|
||||
processedProducts: myProcessedProducts,
|
||||
processedOrders: 0,
|
||||
processedPurchaseOrders: 0,
|
||||
success: true
|
||||
@@ -69,7 +56,7 @@ async function calculateProductMetrics(startTime, totalProducts, processedCount
|
||||
}
|
||||
});
|
||||
return {
|
||||
processedProducts: processedCount,
|
||||
processedProducts: myProcessedProducts,
|
||||
processedOrders,
|
||||
processedPurchaseOrders: 0,
|
||||
success
|
||||
@@ -290,6 +277,7 @@ 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',
|
||||
@@ -361,12 +349,12 @@ async function calculateProductMetrics(startTime, totalProducts, processedCount
|
||||
outputProgress({
|
||||
status: 'running',
|
||||
operation: 'Starting product time aggregates calculation',
|
||||
current: processedCount || 0,
|
||||
total: totalProducts || 0,
|
||||
current: processedCount,
|
||||
total: totalProducts,
|
||||
elapsed: formatElapsedTime(startTime),
|
||||
remaining: estimateRemaining(startTime, processedCount || 0, totalProducts || 0),
|
||||
rate: calculateRate(startTime, processedCount || 0),
|
||||
percentage: (((processedCount || 0) / (totalProducts || 1)) * 100).toFixed(1),
|
||||
remaining: estimateRemaining(startTime, processedCount, totalProducts),
|
||||
rate: calculateRate(startTime, processedCount),
|
||||
percentage: (((processedCount) / (totalProducts || 1)) * 100).toFixed(1),
|
||||
timing: {
|
||||
start_time: new Date(startTime).toISOString(),
|
||||
end_time: new Date().toISOString(),
|
||||
@@ -428,12 +416,12 @@ async function calculateProductMetrics(startTime, totalProducts, processedCount
|
||||
outputProgress({
|
||||
status: 'running',
|
||||
operation: 'Product time aggregates calculated',
|
||||
current: processedCount || 0,
|
||||
total: totalProducts || 0,
|
||||
current: processedCount,
|
||||
total: totalProducts,
|
||||
elapsed: formatElapsedTime(startTime),
|
||||
remaining: estimateRemaining(startTime, processedCount || 0, totalProducts || 0),
|
||||
rate: calculateRate(startTime, processedCount || 0),
|
||||
percentage: (((processedCount || 0) / (totalProducts || 1)) * 100).toFixed(1),
|
||||
remaining: estimateRemaining(startTime, processedCount, totalProducts),
|
||||
rate: calculateRate(startTime, processedCount),
|
||||
percentage: (((processedCount) / (totalProducts || 1)) * 100).toFixed(1),
|
||||
timing: {
|
||||
start_time: new Date(startTime).toISOString(),
|
||||
end_time: new Date().toISOString(),
|
||||
@@ -445,12 +433,12 @@ async function calculateProductMetrics(startTime, totalProducts, processedCount
|
||||
outputProgress({
|
||||
status: 'running',
|
||||
operation: 'Skipping product time aggregates calculation',
|
||||
current: processedCount || 0,
|
||||
total: totalProducts || 0,
|
||||
current: processedCount,
|
||||
total: totalProducts,
|
||||
elapsed: formatElapsedTime(startTime),
|
||||
remaining: estimateRemaining(startTime, processedCount || 0, totalProducts || 0),
|
||||
rate: calculateRate(startTime, processedCount || 0),
|
||||
percentage: (((processedCount || 0) / (totalProducts || 1)) * 100).toFixed(1),
|
||||
remaining: estimateRemaining(startTime, processedCount, totalProducts),
|
||||
rate: calculateRate(startTime, processedCount),
|
||||
percentage: (((processedCount) / (totalProducts || 1)) * 100).toFixed(1),
|
||||
timing: {
|
||||
start_time: new Date(startTime).toISOString(),
|
||||
end_time: new Date().toISOString(),
|
||||
@@ -479,7 +467,7 @@ async function calculateProductMetrics(startTime, totalProducts, processedCount
|
||||
if (isCancelled) return {
|
||||
processedProducts: processedCount,
|
||||
processedOrders,
|
||||
processedPurchaseOrders: 0, // This module doesn't process POs
|
||||
processedPurchaseOrders: 0,
|
||||
success
|
||||
};
|
||||
|
||||
@@ -540,7 +528,7 @@ async function calculateProductMetrics(startTime, totalProducts, processedCount
|
||||
if (isCancelled) return {
|
||||
processedProducts: processedCount,
|
||||
processedOrders,
|
||||
processedPurchaseOrders: 0, // This module doesn't process POs
|
||||
processedPurchaseOrders: 0,
|
||||
success
|
||||
};
|
||||
|
||||
@@ -620,9 +608,9 @@ async function calculateProductMetrics(startTime, totalProducts, processedCount
|
||||
`);
|
||||
|
||||
return {
|
||||
processedProducts: processedCount || 0,
|
||||
processedProducts: processedCount,
|
||||
processedOrders: processedOrders || 0,
|
||||
processedPurchaseOrders: 0, // This module doesn't process POs
|
||||
processedPurchaseOrders: 0,
|
||||
success
|
||||
};
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -5,7 +5,8 @@ async function calculateTimeAggregates(startTime, totalProducts, processedCount
|
||||
const connection = await getConnection();
|
||||
let success = false;
|
||||
const BATCH_SIZE = 5000;
|
||||
|
||||
let myProcessedProducts = 0; // Track products processed *within this module*
|
||||
|
||||
try {
|
||||
// Get last calculation timestamp
|
||||
const [lastCalc] = await connection.query(`
|
||||
@@ -15,17 +16,7 @@ async function calculateTimeAggregates(startTime, totalProducts, processedCount
|
||||
`);
|
||||
const lastCalculationTime = lastCalc[0]?.last_calculation_timestamp || '1970-01-01';
|
||||
|
||||
// Get total count of products needing updates
|
||||
if (!totalProducts) {
|
||||
const [productCount] = await connection.query(`
|
||||
SELECT COUNT(DISTINCT p.pid) as count
|
||||
FROM products p
|
||||
LEFT JOIN orders o ON p.pid = o.pid AND o.updated > ?
|
||||
WHERE p.updated > ?
|
||||
OR o.pid IS NOT NULL
|
||||
`, [lastCalculationTime, lastCalculationTime]);
|
||||
totalProducts = productCount[0].count;
|
||||
}
|
||||
// We now receive totalProducts as an argument, so we don't need to query for it here.
|
||||
|
||||
if (totalProducts === 0) {
|
||||
console.log('No products need time aggregate updates');
|
||||
@@ -41,7 +32,7 @@ async function calculateTimeAggregates(startTime, totalProducts, processedCount
|
||||
outputProgress({
|
||||
status: 'cancelled',
|
||||
operation: 'Time aggregates calculation cancelled',
|
||||
current: processedCount,
|
||||
current: processedCount, // Use passed-in value
|
||||
total: totalProducts,
|
||||
elapsed: formatElapsedTime(startTime),
|
||||
remaining: null,
|
||||
@@ -54,7 +45,7 @@ async function calculateTimeAggregates(startTime, totalProducts, processedCount
|
||||
}
|
||||
});
|
||||
return {
|
||||
processedProducts: processedCount,
|
||||
processedProducts: myProcessedProducts, // Return only what *this* module processed
|
||||
processedOrders: 0,
|
||||
processedPurchaseOrders: 0,
|
||||
success
|
||||
@@ -64,7 +55,7 @@ async function calculateTimeAggregates(startTime, totalProducts, processedCount
|
||||
outputProgress({
|
||||
status: 'running',
|
||||
operation: 'Starting time aggregates calculation',
|
||||
current: processedCount,
|
||||
current: processedCount, // Use passed-in value
|
||||
total: totalProducts,
|
||||
elapsed: formatElapsedTime(startTime),
|
||||
remaining: estimateRemaining(startTime, processedCount, totalProducts),
|
||||
@@ -253,17 +244,17 @@ async function calculateTimeAggregates(startTime, totalProducts, processedCount
|
||||
await connection.query('DROP TEMPORARY TABLE IF EXISTS temp_time_aggregates');
|
||||
|
||||
lastPid = batch[batch.length - 1].pid;
|
||||
processedCount += batch.length;
|
||||
myProcessedProducts += batch.length; // Increment *this module's* count
|
||||
|
||||
outputProgress({
|
||||
status: 'running',
|
||||
operation: 'Processing time aggregates 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(),
|
||||
@@ -274,7 +265,7 @@ async function calculateTimeAggregates(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 +274,7 @@ async function calculateTimeAggregates(startTime, totalProducts, processedCount
|
||||
`);
|
||||
|
||||
return {
|
||||
processedProducts: processedCount,
|
||||
processedProducts: myProcessedProducts, // Return only what *this* module processed
|
||||
processedOrders: 0,
|
||||
processedPurchaseOrders: 0,
|
||||
success
|
||||
|
||||
@@ -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