UNPKG

@agility/cli

Version:

Agility CLI for working with your content. (Public Beta)

243 lines 9.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProgressCalculator = void 0; var ProgressCalculator = /** @class */ (function () { function ProgressCalculator(windowSize) { if (windowSize === void 0) { windowSize = 10; } this.progressHistory = []; this.windowSize = 10; // Keep last 10 measurements for rate calculation this.startTime = new Date(); this.windowSize = windowSize; this.startTime = new Date(); } /** * Calculate progress percentage */ ProgressCalculator.calculatePercentage = function (processed, total) { if (total <= 0) return 0; return Math.min(100, Math.max(0, Math.floor((processed / total) * 100))); }; /** * Calculate progress stats with timing information */ ProgressCalculator.prototype.calculateProgress = function (processed, total) { var currentTime = new Date(); var elapsedTime = currentTime.getTime() - this.startTime.getTime(); var percentage = ProgressCalculator.calculatePercentage(processed, total); // Add current measurement to history this.addMeasurement(processed); // Calculate items per second using moving average var itemsPerSecond = this.calculateItemsPerSecond(); // Estimate remaining time var remaining = total - processed; var estimatedRemainingTime = itemsPerSecond > 0 ? (remaining / itemsPerSecond) * 1000 : undefined; var estimatedTotalTime = estimatedRemainingTime ? elapsedTime + estimatedRemainingTime : undefined; return { processed: processed, total: total, percentage: percentage, startTime: this.startTime, currentTime: currentTime, elapsedTime: elapsedTime, estimatedTotalTime: estimatedTotalTime, estimatedRemainingTime: estimatedRemainingTime, itemsPerSecond: itemsPerSecond }; }; /** * Add a measurement to the progress history */ ProgressCalculator.prototype.addMeasurement = function (processed) { var now = Date.now(); this.progressHistory.push({ timestamp: now, processed: processed }); // Keep only the last windowSize measurements if (this.progressHistory.length > this.windowSize) { this.progressHistory.shift(); } }; /** * Calculate items per second using moving average */ ProgressCalculator.prototype.calculateItemsPerSecond = function () { if (this.progressHistory.length < 2) return 0; var latest = this.progressHistory[this.progressHistory.length - 1]; var earliest = this.progressHistory[0]; var timeDiff = (latest.timestamp - earliest.timestamp) / 1000; // seconds var itemsDiff = latest.processed - earliest.processed; return timeDiff > 0 ? itemsDiff / timeDiff : 0; }; /** * Format time duration */ ProgressCalculator.formatDuration = function (milliseconds) { var seconds = Math.floor(milliseconds / 1000); var minutes = Math.floor(seconds / 60); var hours = Math.floor(minutes / 60); if (hours > 0) { return "".concat(hours, "h ").concat(minutes % 60, "m ").concat(seconds % 60, "s"); } else if (minutes > 0) { return "".concat(minutes, "m ").concat(seconds % 60, "s"); } else { return "".concat(seconds, "s"); } }; /** * Format items per second */ ProgressCalculator.formatRate = function (itemsPerSecond) { if (itemsPerSecond > 1000) { return "".concat((itemsPerSecond / 1000).toFixed(1), "k/sec"); } else if (itemsPerSecond > 1) { return "".concat(itemsPerSecond.toFixed(1), "/sec"); } else if (itemsPerSecond > 0) { return "".concat((itemsPerSecond * 60).toFixed(1), "/min"); } else { return '0/sec'; } }; /** * Create a progress summary string */ ProgressCalculator.formatProgressSummary = function (stats) { var parts = []; parts.push("".concat(stats.processed, "/").concat(stats.total, " (").concat(stats.percentage, "%)")); if (stats.itemsPerSecond !== undefined) { parts.push(ProgressCalculator.formatRate(stats.itemsPerSecond)); } if (stats.estimatedRemainingTime !== undefined) { var eta = ProgressCalculator.formatDuration(stats.estimatedRemainingTime); parts.push("ETA: ".concat(eta)); } return parts.join(' - '); }; /** * Calculate completion percentage for multiple steps */ ProgressCalculator.calculateOverallProgress = function (stepProgresses) { if (stepProgresses.length === 0) return 0; var totalProgress = stepProgresses.reduce(function (sum, progress) { return sum + progress; }, 0); return Math.floor(totalProgress / stepProgresses.length); }; /** * Calculate weighted progress for steps with different importance */ ProgressCalculator.calculateWeightedProgress = function (stepProgresses, weights) { if (stepProgresses.length !== weights.length || stepProgresses.length === 0) return 0; var totalWeight = weights.reduce(function (sum, weight) { return sum + weight; }, 0); if (totalWeight === 0) return 0; var weightedSum = stepProgresses.reduce(function (sum, progress, index) { return sum + (progress * weights[index]); }, 0); return Math.floor(weightedSum / totalWeight); }; /** * Estimate time until completion */ ProgressCalculator.prototype.getEstimatedTimeRemaining = function (processed, total) { var stats = this.calculateProgress(processed, total); return stats.estimatedRemainingTime || null; }; /** * Get current processing rate */ ProgressCalculator.prototype.getCurrentRate = function () { return this.calculateItemsPerSecond(); }; /** * Reset calculator for new operation */ ProgressCalculator.prototype.reset = function () { this.progressHistory = []; this.startTime = new Date(); }; /** * Create a throttled progress reporter */ ProgressCalculator.createThrottledReporter = function (reportCallback, intervalMs) { if (intervalMs === void 0) { intervalMs = 500; } var lastReportTime = 0; var calculator = new ProgressCalculator(); return function (processed, total) { var now = Date.now(); // Always report completion (100%) if (processed >= total) { var stats = calculator.calculateProgress(processed, total); reportCallback(stats); return; } // Throttle intermediate updates if (now - lastReportTime > intervalMs) { var stats = calculator.calculateProgress(processed, total); reportCallback(stats); lastReportTime = now; } }; }; /** * Create batch progress calculator for large operations */ ProgressCalculator.createBatchProgressCalculator = function (batchSize) { var calculator = new ProgressCalculator(); return { reportProgress: function (batchIndex, totalBatches, batchProgress) { // Calculate overall progress: completed batches + current batch progress var completedItems = batchIndex * batchSize; var currentBatchItems = Math.floor((batchProgress / 100) * batchSize); var totalProcessed = completedItems + currentBatchItems; var totalItems = totalBatches * batchSize; return calculator.calculateProgress(totalProcessed, totalItems); }, reset: function () { return calculator.reset(); } }; }; /** * Smooth progress updates to prevent UI jitter */ ProgressCalculator.createSmoothProgressReporter = function (updateCallback, smoothingFactor) { if (smoothingFactor === void 0) { smoothingFactor = 0.1; } var lastReportedPercentage = 0; return function (processed, total) { var actualPercentage = ProgressCalculator.calculatePercentage(processed, total); // Use exponential smoothing to reduce jitter var smoothedPercentage = lastReportedPercentage + smoothingFactor * (actualPercentage - lastReportedPercentage); var roundedPercentage = Math.floor(smoothedPercentage); // Only update if there's a meaningful change or completion if (roundedPercentage !== lastReportedPercentage || actualPercentage === 100) { updateCallback(actualPercentage === 100 ? 100 : roundedPercentage); lastReportedPercentage = roundedPercentage; } }; }; /** * Calculate conservative progress for Content Sync SDK operations */ ProgressCalculator.calculateConservativeProgress = function (totalItems, divisor) { if (divisor === void 0) { divisor = 20; } // More conservative progress calculation to prevent overly optimistic estimates return Math.min(95, Math.floor(totalItems / divisor)); }; /** * Get progress statistics for debugging */ ProgressCalculator.prototype.getStats = function () { return { historySize: this.progressHistory.length, currentRate: this.calculateItemsPerSecond(), elapsedTime: Date.now() - this.startTime.getTime() }; }; return ProgressCalculator; }()); exports.ProgressCalculator = ProgressCalculator; //# sourceMappingURL=progress-calculator.js.map