|
| 1 | +use serde::{Deserialize, Serialize}; |
| 2 | +use std::fs; |
| 3 | +use std::os::unix::fs::MetadataExt; |
| 4 | +use std::path::PathBuf; |
| 5 | +use sysinfo::System; |
| 6 | + |
| 7 | +use crate::registry::model_registry::ModelRegistry; |
| 8 | +use crate::util::file; |
| 9 | +use crate::util::format::format_size; |
| 10 | + |
| 11 | +#[derive(Debug, Serialize, Deserialize)] |
| 12 | +pub struct SystemInfo { |
| 13 | + pub version: String, |
| 14 | + pub os: String, |
| 15 | + pub architecture: String, |
| 16 | + pub cpu_cores: usize, |
| 17 | + pub total_memory: String, |
| 18 | + pub available_memory: String, |
| 19 | + pub cache_dir: String, |
| 20 | + pub cache_size: String, |
| 21 | + pub models_count: usize, |
| 22 | + pub running_models: usize, |
| 23 | +} |
| 24 | + |
| 25 | +impl SystemInfo { |
| 26 | + pub fn collect() -> Self { |
| 27 | + let mut sys = System::new_all(); |
| 28 | + sys.refresh_all(); |
| 29 | + |
| 30 | + let cache_dir = file::cache_dir(); |
| 31 | + let cache_size = Self::calculate_cache_size(&cache_dir); |
| 32 | + |
| 33 | + let registry = ModelRegistry::new(None); |
| 34 | + let models_count = registry.load_models().unwrap_or_default().len(); |
| 35 | + |
| 36 | + SystemInfo { |
| 37 | + version: env!("CARGO_PKG_VERSION").to_string(), |
| 38 | + os: System::name().unwrap_or_else(|| "Unknown".to_string()), |
| 39 | + architecture: System::cpu_arch().unwrap_or_else(|| "Unknown".to_string()), |
| 40 | + cpu_cores: sys.cpus().len(), |
| 41 | + total_memory: format_size(sys.total_memory()), |
| 42 | + available_memory: format_size(sys.available_memory()), |
| 43 | + cache_dir: cache_dir.to_string_lossy().to_string(), |
| 44 | + cache_size: format_size(cache_size), |
| 45 | + models_count, |
| 46 | + running_models: 0, // TODO: implement running models tracking |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + fn calculate_cache_size(cache_dir: &PathBuf) -> u64 { |
| 51 | + if !cache_dir.exists() { |
| 52 | + return 0; |
| 53 | + } |
| 54 | + |
| 55 | + let mut total_size = 0u64; |
| 56 | + |
| 57 | + if let Ok(entries) = fs::read_dir(cache_dir) { |
| 58 | + for entry in entries.flatten() { |
| 59 | + if let Ok(metadata) = entry.metadata() { |
| 60 | + if metadata.is_file() { |
| 61 | + // Use blocks * 512 to get actual disk usage (handles sparse files) |
| 62 | + total_size += metadata.blocks() * 512; |
| 63 | + } else if metadata.is_dir() { |
| 64 | + total_size += Self::dir_size(&entry.path()); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + total_size |
| 71 | + } |
| 72 | + |
| 73 | + fn dir_size(path: &PathBuf) -> u64 { |
| 74 | + let mut total_size = 0u64; |
| 75 | + |
| 76 | + if let Ok(entries) = fs::read_dir(path) { |
| 77 | + for entry in entries.flatten() { |
| 78 | + if let Ok(metadata) = entry.metadata() { |
| 79 | + if metadata.is_file() { |
| 80 | + // Use blocks * 512 to get actual disk usage (handles sparse files) |
| 81 | + total_size += metadata.blocks() * 512; |
| 82 | + } else if metadata.is_dir() { |
| 83 | + total_size += Self::dir_size(&entry.path()); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + total_size |
| 90 | + } |
| 91 | + |
| 92 | + pub fn display(&self) { |
| 93 | + println!("System Information:"); |
| 94 | + println!(" Operating System: {}", self.os); |
| 95 | + println!(" Architecture: {}", self.architecture); |
| 96 | + println!(" CPU Cores: {}", self.cpu_cores); |
| 97 | + println!(" Total Memory: {}", self.total_memory); |
| 98 | + println!(" Available Memory: {}", self.available_memory); |
| 99 | + println!(); |
| 100 | + println!("PUMA Information:"); |
| 101 | + println!(" PUMA Version: {}", self.version); |
| 102 | + println!(" Cache Directory: {}", self.cache_dir); |
| 103 | + println!(" Cache Size: {}", self.cache_size); |
| 104 | + println!(" Models: {}", self.models_count); |
| 105 | + println!(" Running Models: {}", self.running_models); |
| 106 | + } |
| 107 | +} |
0 commit comments