@agility/cli
Version:
Agility CLI for working with your content. (Public Beta)
279 lines • 11.2 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProgressTracker = void 0;
var ProgressTracker = /** @class */ (function () {
function ProgressTracker(operationName) {
if (operationName === void 0) { operationName = 'Operation'; }
this.steps = [];
this.callbacks = {};
this.startTime = new Date();
this.operationName = 'Operation';
this.operationName = operationName;
this.startTime = new Date();
}
/**
* Initialize steps for tracking
*/
ProgressTracker.prototype.initializeSteps = function (stepNames) {
this.steps = stepNames.map(function (name) { return ({
name: name,
status: 'pending',
percentage: 0
}); });
this.startTime = new Date();
};
/**
* Set progress callbacks for different events
*/
ProgressTracker.prototype.setCallbacks = function (callbacks) {
this.callbacks = callbacks;
};
/**
* Start a step
*/
ProgressTracker.prototype.startStep = function (stepIndex) {
var _a, _b;
if (stepIndex < 0 || stepIndex >= this.steps.length)
return;
this.steps[stepIndex].status = 'progress';
this.steps[stepIndex].percentage = 0;
this.steps[stepIndex].startTime = new Date();
this.steps[stepIndex].error = undefined;
(_b = (_a = this.callbacks).onStepStart) === null || _b === void 0 ? void 0 : _b.call(_a, stepIndex, this.steps[stepIndex].name);
};
/**
* Update step progress
*/
ProgressTracker.prototype.updateStepProgress = function (stepIndex, percentage, status) {
var _a, _b, _c, _d;
if (status === void 0) { status = 'progress'; }
if (stepIndex < 0 || stepIndex >= this.steps.length)
return;
this.steps[stepIndex].percentage = Math.min(100, Math.max(0, percentage));
this.steps[stepIndex].status = status;
if (status === 'success' || status === 'error') {
this.steps[stepIndex].endTime = new Date();
this.steps[stepIndex].percentage = 100;
}
(_b = (_a = this.callbacks).onStepProgress) === null || _b === void 0 ? void 0 : _b.call(_a, stepIndex, this.steps[stepIndex].name, this.steps[stepIndex].percentage);
if (status === 'success' || status === 'error') {
(_d = (_c = this.callbacks).onStepComplete) === null || _d === void 0 ? void 0 : _d.call(_c, stepIndex, this.steps[stepIndex].name, status);
}
};
/**
* Mark step as successful
*/
ProgressTracker.prototype.completeStep = function (stepIndex) {
this.updateStepProgress(stepIndex, 100, 'success');
};
/**
* Mark step as failed
*/
ProgressTracker.prototype.failStep = function (stepIndex, error) {
if (stepIndex < 0 || stepIndex >= this.steps.length)
return;
this.steps[stepIndex].error = error;
this.updateStepProgress(stepIndex, this.steps[stepIndex].percentage, 'error');
};
/**
* Create a progress callback for a specific step
*/
ProgressTracker.prototype.createStepProgressCallback = function (stepIndex) {
var _this = this;
return function (processed, total, status) {
if (status === void 0) { status = "progress"; }
var percentage = total > 0 ? Math.floor((processed / total) * 100) : 0;
if (status === "error") {
_this.failStep(stepIndex);
}
else if (status === "success") {
_this.completeStep(stepIndex);
}
else {
_this.updateStepProgress(stepIndex, percentage, 'progress');
}
};
};
/**
* Get current progress summary
*/
ProgressTracker.prototype.getSummary = function () {
var _a, _b;
var totalSteps = this.steps.length;
var successfulSteps = this.steps.filter(function (step) { return step.status === 'success'; }).length;
var errorSteps = this.steps.filter(function (step) { return step.status === 'error'; }).length;
var pendingSteps = this.steps.filter(function (step) { return step.status === 'pending'; }).length;
var overallSuccess = errorSteps === 0 && successfulSteps === totalSteps;
var now = new Date();
var totalDuration = now.getTime() - this.startTime.getTime();
var totalSeconds = Math.floor(totalDuration / 1000);
var minutes = Math.floor(totalSeconds / 60);
var seconds = totalSeconds % 60;
var durationFormatted = minutes > 0 ? "".concat(minutes, "m ").concat(seconds, "s") : "".concat(seconds, "s");
var summary = {
totalSteps: totalSteps,
successfulSteps: successfulSteps,
errorSteps: errorSteps,
pendingSteps: pendingSteps,
overallSuccess: overallSuccess,
totalDuration: totalDuration,
durationFormatted: durationFormatted
};
(_b = (_a = this.callbacks).onOverallProgress) === null || _b === void 0 ? void 0 : _b.call(_a, summary);
return summary;
};
/**
* Get step status by index
*/
ProgressTracker.prototype.getStep = function (stepIndex) {
if (stepIndex < 0 || stepIndex >= this.steps.length)
return null;
return __assign({}, this.steps[stepIndex]);
};
/**
* Get step status by name
*/
ProgressTracker.prototype.getStepByName = function (stepName) {
var step = this.steps.find(function (s) { return s.name === stepName; });
return step ? __assign({}, step) : null;
};
/**
* Get all steps
*/
ProgressTracker.prototype.getAllSteps = function () {
return this.steps.map(function (step) { return (__assign({}, step)); });
};
/**
* Get step index by name
*/
ProgressTracker.prototype.getStepIndex = function (stepName) {
return this.steps.findIndex(function (s) { return s.name === stepName; });
};
/**
* Check if all steps are complete
*/
ProgressTracker.prototype.isComplete = function () {
return this.steps.every(function (step) { return step.status === 'success' || step.status === 'error'; });
};
/**
* Check if any steps have errors
*/
ProgressTracker.prototype.hasErrors = function () {
return this.steps.some(function (step) { return step.status === 'error'; });
};
/**
* Get steps with errors
*/
ProgressTracker.prototype.getFailedSteps = function () {
return this.steps.filter(function (step) { return step.status === 'error'; }).map(function (step) { return (__assign({}, step)); });
};
/**
* Get completed steps
*/
ProgressTracker.prototype.getCompletedSteps = function () {
return this.steps.filter(function (step) { return step.status === 'success'; }).map(function (step) { return (__assign({}, step)); });
};
/**
* Get pending steps
*/
ProgressTracker.prototype.getPendingSteps = function () {
return this.steps.filter(function (step) { return step.status === 'pending'; }).map(function (step) { return (__assign({}, step)); });
};
/**
* Get overall progress percentage (0-100)
*/
ProgressTracker.prototype.getOverallProgress = function () {
if (this.steps.length === 0)
return 0;
var totalProgress = this.steps.reduce(function (sum, step) { return sum + step.percentage; }, 0);
return Math.floor(totalProgress / this.steps.length);
};
/**
* Reset all steps to pending
*/
ProgressTracker.prototype.reset = function () {
this.steps = this.steps.map(function (step) { return (__assign(__assign({}, step), { status: 'pending', percentage: 0, startTime: undefined, endTime: undefined, error: undefined })); });
this.startTime = new Date();
};
/**
* Format summary for logging
*/
ProgressTracker.prototype.formatSummary = function (includeDetails) {
if (includeDetails === void 0) { includeDetails = false; }
var summary = this.getSummary();
var lines = [];
lines.push("".concat(this.operationName, " completed: ").concat(summary.successfulSteps, "/").concat(summary.totalSteps, " steps successful, ").concat(summary.errorSteps, " errors, ").concat(summary.durationFormatted));
if (includeDetails) {
if (summary.errorSteps > 0) {
lines.push('Failed steps:');
this.getFailedSteps().forEach(function (step) {
lines.push(" \u2717 ".concat(step.name).concat(step.error ? ": ".concat(step.error) : ''));
});
}
if (summary.successfulSteps > 0) {
lines.push('Successful steps:');
this.getCompletedSteps().forEach(function (step) {
var duration = step.startTime && step.endTime
? "(".concat(Math.floor((step.endTime.getTime() - step.startTime.getTime()) / 1000), "s)")
: '';
lines.push(" \u2713 ".concat(step.name, " ").concat(duration));
});
}
}
return lines;
};
/**
* Create a throttled progress callback for memory optimization
*/
ProgressTracker.prototype.createThrottledProgressCallback = function (stepIndex, updateInterval) {
var _this = this;
if (updateInterval === void 0) { updateInterval = 500; }
var lastUpdate = 0;
return function (processed, total, status) {
if (status === void 0) { status = "progress"; }
var now = Date.now();
// Always process success/error status immediately
if (status === "success" || status === "error") {
_this.createStepProgressCallback(stepIndex)(processed, total, status);
return;
}
// Throttle progress updates
if (now - lastUpdate > updateInterval) {
_this.createStepProgressCallback(stepIndex)(processed, total, status);
lastUpdate = now;
}
};
};
/**
* Get operation name
*/
ProgressTracker.prototype.getOperationName = function () {
return this.operationName;
};
/**
* Set operation name
*/
ProgressTracker.prototype.setOperationName = function (name) {
this.operationName = name;
};
/**
* Get start time
*/
ProgressTracker.prototype.getStartTime = function () {
return new Date(this.startTime);
};
return ProgressTracker;
}());
exports.ProgressTracker = ProgressTracker;
//# sourceMappingURL=progress-tracker.js.map