Enhance metrics calculation scripts with improved progress tracking and cancellation support

This commit is contained in:
2025-01-28 20:54:05 -05:00
parent a1e3803ca3
commit 9c34e24909
12 changed files with 915 additions and 327 deletions

View File

@@ -2,6 +2,7 @@ const mysql = require("mysql2/promise");
const { Client } = require("ssh2");
const dotenv = require("dotenv");
const path = require("path");
const { outputProgress, formatElapsedTime, estimateRemaining, calculateRate } = require('./metrics/utils/progress');
dotenv.config({ path: path.join(__dirname, "../.env") });
@@ -43,52 +44,38 @@ const localDbConfig = {
namedPlaceholders: true,
};
// Helper function to output progress
function outputProgress(data) {
process.stdout.write(JSON.stringify(data) + "\n");
}
// Helper function to format duration
function formatDuration(seconds) {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
seconds = Math.floor(seconds % 60);
const parts = [];
if (hours > 0) parts.push(`${hours}h`);
if (minutes > 0) parts.push(`${minutes}m`);
if (seconds > 0 || parts.length === 0) parts.push(`${seconds}s`);
return parts.join(" ");
}
// Helper function to update progress with time estimate
function updateProgress(current, total, operation, startTime) {
const elapsed = (Date.now() - startTime) / 1000;
const rate = current / elapsed;
const remaining = (total - current) / rate;
outputProgress({
status: "running",
operation,
current,
total,
rate,
elapsed: formatDuration(elapsed),
remaining: formatDuration(remaining),
percentage: ((current / total) * 100).toFixed(1),
});
}
// Constants
const BATCH_SIZE = 1000;
const PROGRESS_INTERVAL = 1000; // Update progress every second
let isImportCancelled = false;
// Add cancel function
function cancelImport() {
isImportCancelled = true;
outputProgress({
status: "cancelled",
operation: "Import cancelled",
});
isImportCancelled = true;
outputProgress({
status: 'cancelled',
operation: 'Import cancelled',
current: 0,
total: 0,
elapsed: null,
remaining: null,
rate: 0
});
}
// Helper function to update progress with time estimate
function updateProgress(current, total, operation, startTime) {
outputProgress({
status: 'running',
operation,
current,
total,
rate: calculateRate(startTime, current),
elapsed: formatElapsedTime(startTime),
remaining: estimateRemaining(startTime, current, total),
percentage: ((current / total) * 100).toFixed(1)
});
}
async function setupSshTunnel() {
@@ -276,7 +263,7 @@ async function importCategories(prodConnection, localConnection) {
operation: "Categories import completed",
current: totalInserted,
total: totalInserted,
duration: formatDuration((Date.now() - startTime) / 1000),
duration: formatElapsedTime((Date.now() - startTime) / 1000),
});
} catch (error) {
console.error("Error importing categories:", error);
@@ -510,7 +497,6 @@ async function importProducts(prodConnection, localConnection) {
const total = rows.length;
// Process products in batches
const BATCH_SIZE = 100;
for (let i = 0; i < rows.length; i += BATCH_SIZE) {
let batch = rows.slice(i, i + BATCH_SIZE);
@@ -641,7 +627,7 @@ async function importProducts(prodConnection, localConnection) {
operation: "Products import completed",
current: total,
total,
duration: formatDuration((Date.now() - startTime) / 1000),
duration: formatElapsedTime((Date.now() - startTime) / 1000),
});
} catch (error) {
console.error("Error importing products:", error);
@@ -1384,7 +1370,7 @@ async function importPurchaseOrders(prodConnection, localConnection) {
timing: {
start_time: new Date(startTime).toISOString(),
end_time: new Date(endTime).toISOString(),
elapsed_time: formatDuration((endTime - startTime) / 1000),
elapsed_time: formatElapsedTime((endTime - startTime) / 1000),
elapsed_seconds: Math.round((endTime - startTime) / 1000)
}
});
@@ -1459,7 +1445,7 @@ async function main() {
timing: {
start_time: new Date(startTime).toISOString(),
end_time: new Date(endTime).toISOString(),
elapsed_time: formatDuration((endTime - startTime) / 1000),
elapsed_time: formatElapsedTime((endTime - startTime) / 1000),
elapsed_seconds: Math.round((endTime - startTime) / 1000)
}
});
@@ -1473,7 +1459,7 @@ async function main() {
timing: {
start_time: new Date(startTime).toISOString(),
end_time: new Date(endTime).toISOString(),
elapsed_time: formatDuration((endTime - startTime) / 1000),
elapsed_time: formatElapsedTime((endTime - startTime) / 1000),
elapsed_seconds: Math.round((endTime - startTime) / 1000)
}
});