UNPKG

@sailboat-computer/health-monitoring

Version:

Comprehensive health monitoring system for sailboat computer v3 with marine-specific health checks

217 lines 7.24 kB
"use strict"; /** * Recovery action types and interfaces for self-healing mechanisms */ Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseRecoveryAction = exports.RecoveryStatus = exports.RecoveryPriority = exports.RecoveryActionType = void 0; /** * Recovery action types */ var RecoveryActionType; (function (RecoveryActionType) { /** * Restart a component or service */ RecoveryActionType["RESTART"] = "restart"; /** * Switch to a backup system */ RecoveryActionType["FAILOVER"] = "failover"; /** * Reset a component to default settings */ RecoveryActionType["RESET"] = "reset"; /** * Recalibrate a sensor or component */ RecoveryActionType["RECALIBRATE"] = "recalibrate"; /** * Adjust configuration parameters */ RecoveryActionType["ADJUST_CONFIG"] = "adjust_config"; /** * Reduce load on a system */ RecoveryActionType["REDUCE_LOAD"] = "reduce_load"; /** * Isolate a component from the system */ RecoveryActionType["ISOLATE"] = "isolate"; /** * Notify crew for manual intervention */ RecoveryActionType["NOTIFY"] = "notify"; })(RecoveryActionType || (exports.RecoveryActionType = RecoveryActionType = {})); /** * Recovery action priority */ var RecoveryPriority; (function (RecoveryPriority) { /** * Low priority, can be delayed */ RecoveryPriority["LOW"] = "low"; /** * Normal priority */ RecoveryPriority["NORMAL"] = "normal"; /** * High priority, execute soon */ RecoveryPriority["HIGH"] = "high"; /** * Critical priority, execute immediately */ RecoveryPriority["CRITICAL"] = "critical"; })(RecoveryPriority || (exports.RecoveryPriority = RecoveryPriority = {})); /** * Recovery action status */ var RecoveryStatus; (function (RecoveryStatus) { /** * Action is pending execution */ RecoveryStatus["PENDING"] = "pending"; /** * Action is currently executing */ RecoveryStatus["EXECUTING"] = "executing"; /** * Action completed successfully */ RecoveryStatus["SUCCEEDED"] = "succeeded"; /** * Action failed */ RecoveryStatus["FAILED"] = "failed"; /** * Action was cancelled */ RecoveryStatus["CANCELLED"] = "cancelled"; /** * Action timed out */ RecoveryStatus["TIMEOUT"] = "timeout"; })(RecoveryStatus || (exports.RecoveryStatus = RecoveryStatus = {})); /** * Abstract base class for recovery actions */ class BaseRecoveryAction { constructor(config) { this.retryCount = 0; this.config = config; } /** * Execute the recovery action */ async execute(triggeringCheck, operationalContext, marineEnvironment) { const startTime = new Date(); try { // Check if action is allowed in current context if (!this.isAllowedInContext(operationalContext, marineEnvironment)) { return this.createFailedResult(startTime, new Date(), 'Action not allowed in current operational context', operationalContext, marineEnvironment, triggeringCheck); } // Execute the actual recovery action const result = await this.performRecovery(triggeringCheck, operationalContext, marineEnvironment); const endTime = new Date(); if (result.success) { this.retryCount = 0; this.lastResult = result; return { ...result, startTime, endTime, duration: endTime.getTime() - startTime.getTime(), retryCount: this.retryCount, triggeringCheck: { checkId: triggeringCheck.checkId, status: triggeringCheck.status, score: triggeringCheck.score } }; } else { // Handle retry logic if (this.retryCount < this.config.maxRetries) { this.retryCount++; await new Promise(resolve => setTimeout(resolve, this.config.retryDelay)); return this.execute(triggeringCheck, operationalContext, marineEnvironment); } else { // Max retries reached const failedResult = this.createFailedResult(startTime, new Date(), `Recovery action failed after ${this.retryCount} retries: ${result.message}`, operationalContext, marineEnvironment, triggeringCheck); this.lastResult = failedResult; return failedResult; } } } catch (error) { const failedResult = this.createFailedResult(startTime, new Date(), `Recovery action error: ${error.message}`, operationalContext, marineEnvironment, triggeringCheck); this.lastResult = failedResult; return failedResult; } } /** * Get the last execution result */ getLastResult() { return this.lastResult; } /** * Get action configuration */ getConfig() { return { ...this.config }; } /** * Check if action is allowed in current context */ isAllowedInContext(operationalContext, marineEnvironment) { // Check operational context if (!this.config.marineSettings.allowedContexts.includes(operationalContext)) { return false; } // Check sea conditions if (marineEnvironment?.seaState === 'rough' || marineEnvironment?.seaState === 'very_rough') { if (!this.config.marineSettings.allowInRoughSeas) { return false; } } // Check power status if (this.config.marineSettings.powerAware && marineEnvironment?.powerStatus === 'critical') { // Only allow safety-critical actions when power is critical return this.config.marineSettings.safetyImpact; } return true; } /** * Create a failed result */ createFailedResult(startTime, endTime, message, operationalContext, marineEnvironment, triggeringCheck) { return { actionId: this.config.actionId, name: this.config.name, type: this.config.type, status: RecoveryStatus.FAILED, message, startTime, endTime, duration: endTime.getTime() - startTime.getTime(), retryCount: this.retryCount, success: false, error: message, marineContext: { operationalContext, environmentalConditions: marineEnvironment ? { ...marineEnvironment } : undefined }, triggeringCheck: triggeringCheck ? { checkId: triggeringCheck.checkId, status: triggeringCheck.status, score: triggeringCheck.score } : undefined }; } } exports.BaseRecoveryAction = BaseRecoveryAction; //# sourceMappingURL=RecoveryAction.js.map