nerve/cli/commands/
status.rs

1//! Status Command
2//!
3//! Provides system health overview and component status summary.
4
5use clap::Args;
6use crate::cli::commands::CliCommand;
7use crate::cli::{CliConfig, CliError};
8use crate::integration::core::NerveSystem;
9
10/// Status command arguments
11#[derive(Args, Debug)]
12pub struct StatusCommand {
13    /// Show detailed component information
14    #[arg(short, long)]
15    pub detailed: bool,
16
17    /// Refresh interval in seconds (for continuous monitoring)
18    #[arg(short, long)]
19    pub refresh: Option<u64>,
20
21    /// Show only critical issues
22    #[arg(short = 'C', long)]
23    pub critical: bool,
24}
25
26impl CliCommand for StatusCommand {
27    fn execute(&self, config: &CliConfig) -> Result<(), CliError> {
28        if config.verbose {
29            println!("๐Ÿ“Š Executing status command...");
30        }
31
32        // Create a NerveSystem instance
33        let system = NerveSystem::new()
34            .map_err(|e| CliError::SystemError(format!("Failed to create system: {}", e)))?;
35
36        // Get system status
37        let system_status = system.get_system_status();
38        let stats = system.get_statistics();
39
40        // Display status information
41        println!("๐Ÿง  Nerve Framework System Status");
42        println!("================================");
43        println!();
44
45        // Overall health
46        println!("๐Ÿ“ˆ Overall Health: {}", system_status.overall_health);
47        println!();
48
49        // Performance statistics
50        println!("๐Ÿ“Š Performance Statistics:");
51        println!("  Messages Processed: {}", stats.messages_processed);
52        println!("  Errors Encountered: {}", stats.errors_encountered);
53        println!("  Error Rate: {:.2}%", stats.error_rate_percent);
54        println!("  Memory Usage: {} bytes", stats.memory_usage_bytes);
55        println!("  Thread Utilization: {}%", stats.thread_utilization_percent);
56        println!();
57
58        // Component health
59        println!("๐Ÿ”ง Component Health:");
60        println!("  Memory System: {}", system_status.memory_health);
61        println!("  Thread System: {}", system_status.thread_health);
62        println!("  Node System: {}", system_status.node_health);
63        println!("  Communication System: {}", system_status.communication_health);
64        println!();
65
66        if self.detailed {
67            self.show_detailed_status(&system, config)?;
68        }
69
70        if self.critical {
71            self.show_critical_issues(&system_status, &stats);
72        }
73
74        Ok(())
75    }
76}
77
78impl StatusCommand {
79    /// Show detailed component status
80    fn show_detailed_status(&self, system: &NerveSystem, _config: &CliConfig) -> Result<(), CliError> {
81        println!("๐Ÿ” Detailed Component Status:");
82        println!("================================");
83        println!();
84
85        // Memory system details
86        let memory_pool = system.memory_pool();
87        let message_buffer = system.message_buffer();
88
89        println!("๐Ÿ’พ Memory System:");
90        println!("  Memory Pool: {} strong references", std::sync::Arc::strong_count(memory_pool));
91        println!("  Message Buffer: {} strong references", std::sync::Arc::strong_count(message_buffer));
92        println!();
93
94        // Thread system details
95        let thread_coordinator = system.thread_coordinator();
96        let thread_watchdog = system.thread_watchdog();
97
98        println!("๐Ÿงต Thread System:");
99        println!("  Thread Coordinator: {} strong references", std::sync::Arc::strong_count(thread_coordinator));
100        println!("  Thread Watchdog: {} strong references", std::sync::Arc::strong_count(thread_watchdog));
101        println!();
102
103        // Node system details
104        let node_registry = system.node_registry();
105
106        println!("๐Ÿท๏ธ Node System:");
107        println!("  Node Registry: {} strong references", std::sync::Arc::strong_count(node_registry));
108        println!();
109
110        // Communication system details
111        let message_router = system.message_router();
112        let subscription_manager = system.subscription_manager();
113        let publisher = system.publisher();
114
115        println!("๐Ÿ“ก Communication System:");
116        println!("  Message Router: {} strong references", std::sync::Arc::strong_count(message_router));
117        println!("  Subscription Manager: {} strong references", std::sync::Arc::strong_count(subscription_manager));
118        println!("  Publisher: {} strong references", std::sync::Arc::strong_count(publisher));
119        println!();
120
121        Ok(())
122    }
123
124    /// Show critical issues only
125    fn show_critical_issues(&self, system_status: &crate::integration::core::SystemStatus, stats: &crate::integration::performance::SystemStatistics) {
126        let mut has_critical_issues = false;
127
128        println!("๐Ÿšจ Critical Issues:");
129        println!("===================");
130        println!();
131
132        // Check error rate
133        if stats.error_rate_percent > 5.0 {
134            println!("โŒ High error rate: {:.2}%", stats.error_rate_percent);
135            has_critical_issues = true;
136        }
137
138        // Check memory usage
139        if stats.memory_usage_bytes > 100_000_000 { // 100MB threshold
140            println!("โŒ High memory usage: {} bytes", stats.memory_usage_bytes);
141            has_critical_issues = true;
142        }
143
144        // Check component health
145        if system_status.overall_health.contains("Critical") {
146            println!("โŒ System in critical state: {}", system_status.overall_health);
147            has_critical_issues = true;
148        }
149
150        if !has_critical_issues {
151            println!("โœ… No critical issues detected");
152        }
153        println!();
154    }
155}