use crate::error::AppResult; use crate::models::{ApplicationEntry, DrivePartition, TreemapItem}; use sysinfo::Disks; pub struct TreemapService; impl TreemapService { pub fn get_disk_partitions() -> AppResult> { let disks = Disks::new_with_refreshed_list(); let mut partitions = Vec::new(); for disk in disks.list() { let total = disk.total_space(); let free = disk.available_space(); let used = total.saturating_sub(free); partitions.push(DrivePartition { name: disk.name().to_string_lossy().to_string(), mount_point: disk.mount_point().to_string_lossy().to_string(), total_bytes: total, free_bytes: free, used_bytes: used, file_system: disk.file_system().to_string_lossy().to_string(), }); } Ok(partitions) } pub fn generate_apps_treemap(apps: &[ApplicationEntry]) -> AppResult> { let mut items = Vec::new(); let colors = [ "#38bdf8", "#818cf8", "#c084fc", "#f43f5e", "#fb923c", "#facc15", "#4ade80", "#2dd4bf", ]; let mut sorted_apps: Vec<&ApplicationEntry> = apps.iter().filter(|a| a.estimated_size_kb > 0).collect(); sorted_apps.sort_unstable_by_key(|a| std::cmp::Reverse(a.estimated_size_kb)); for (idx, app) in sorted_apps.into_iter().enumerate() { let size_bytes = app.estimated_size_kb * 1024; let color = colors[idx % colors.len()]; items.push(TreemapItem { id: app.id.clone(), name: app.display_name.clone(), value: size_bytes, formatted_size: Self::format_size(size_bytes), color: color.to_string(), children: Vec::new(), }); } Ok(items) } fn format_size(bytes: u64) -> String { const KB: u64 = 1024; const MB: u64 = KB * 1024; const GB: u64 = MB * 1024; if bytes >= GB { format!("{:.2} GB", bytes as f64 / GB as f64) } else if bytes >= MB { format!("{:.2} MB", bytes as f64 / MB as f64) } else if bytes >= KB { format!("{:.2} KB", bytes as f64 / KB as f64) } else { format!("{} B", bytes) } } }