Optimize and fix calculate scripts

This commit is contained in:
2025-01-27 13:16:21 -05:00
parent 5781b45f37
commit 8323ae7703
10 changed files with 748 additions and 962 deletions

View File

@@ -1,8 +1,20 @@
const { outputProgress, formatElapsedTime, estimateRemaining, calculateRate } = require('./utils/progress');
const { getConnection } = require('./utils/db');
async function calculateTimeAggregates(startTime, totalProducts, processedCount) {
const connection = await getConnection();
try {
outputProgress({
status: 'running',
operation: 'Calculating time aggregates',
current: Math.floor(totalProducts * 0.95),
total: totalProducts,
elapsed: formatElapsedTime(startTime),
remaining: estimateRemaining(startTime, Math.floor(totalProducts * 0.95), totalProducts),
rate: calculateRate(startTime, Math.floor(totalProducts * 0.95)),
percentage: '95'
});
// Initial insert of time-based aggregates
await connection.query(`
INSERT INTO product_time_aggregates (
@@ -47,7 +59,7 @@ async function calculateTimeAggregates(startTime, totalProducts, processedCount)
SUM(received) as stock_received,
SUM(ordered) as stock_ordered
FROM purchase_orders
WHERE receiving_status >= 30 -- Partial or fully received
WHERE status = 50
GROUP BY pid, YEAR(date), MONTH(date)
)
SELECT
@@ -126,7 +138,9 @@ async function calculateTimeAggregates(startTime, totalProducts, processedCount)
return Math.floor(totalProducts * 0.65);
} finally {
connection.release();
if (connection) {
connection.release();
}
}
}