@@ -12,7 +12,7 @@ use anyhow::Result; |
| 12 | 12 | use gartk_core::{InputEvent, Key, Point, Rect}; |
| 13 | 13 | use gartk_render::{Renderer, TextStyle}; |
| 14 | 14 | use gartk_x11::{Connection, EventLoop, EventLoopConfig, Window, WindowConfig}; |
| 15 | | -use gartop_ipc::{Command, CpuStats, MemoryStats, ProcessInfo, Response, SortField, StatusInfo}; |
| 15 | +use gartop_ipc::{Command, CpuStats, DiskStats, MemoryStats, NetworkStats, ProcessInfo, Response, SortField, StatusInfo}; |
| 16 | 16 | use std::io::{BufRead, BufReader, Write}; |
| 17 | 17 | use std::os::unix::net::UnixStream; |
| 18 | 18 | use std::time::Instant; |
@@ -49,8 +49,12 @@ pub struct App { |
| 49 | 49 | status: Option<StatusInfo>, |
| 50 | 50 | cpu_stats: Option<CpuStats>, |
| 51 | 51 | memory_stats: Option<MemoryStats>, |
| 52 | + network_stats: Vec<NetworkStats>, |
| 53 | + disk_stats: Vec<DiskStats>, |
| 52 | 54 | cpu_history: Vec<CpuStats>, |
| 53 | 55 | memory_history: Vec<MemoryStats>, |
| 56 | + network_history: Vec<Vec<NetworkStats>>, |
| 57 | + disk_history: Vec<Vec<DiskStats>>, |
| 54 | 58 | processes: Vec<ProcessInfo>, |
| 55 | 59 | } |
| 56 | 60 | |
@@ -117,8 +121,12 @@ impl App { |
| 117 | 121 | status: None, |
| 118 | 122 | cpu_stats: None, |
| 119 | 123 | memory_stats: None, |
| 124 | + network_stats: Vec::new(), |
| 125 | + disk_stats: Vec::new(), |
| 120 | 126 | cpu_history: Vec::new(), |
| 121 | 127 | memory_history: Vec::new(), |
| 128 | + network_history: Vec::new(), |
| 129 | + disk_history: Vec::new(), |
| 122 | 130 | processes: Vec::new(), |
| 123 | 131 | }) |
| 124 | 132 | } |
@@ -205,10 +213,39 @@ impl App { |
| 205 | 213 | } |
| 206 | 214 | } |
| 207 | 215 | |
| 216 | + // Get network stats |
| 217 | + if let Some(resp) = self.send_command(&Command::GetNetwork) { |
| 218 | + if resp.success { |
| 219 | + self.network_stats = resp.data.and_then(|d| serde_json::from_value(d).ok()).unwrap_or_default(); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + // Get network history |
| 224 | + if let Some(resp) = self.send_command(&Command::GetNetworkHistory { count: Some(60) }) { |
| 225 | + if resp.success { |
| 226 | + self.network_history = resp.data.and_then(|d| serde_json::from_value(d).ok()).unwrap_or_default(); |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + // Get disk stats |
| 231 | + if let Some(resp) = self.send_command(&Command::GetDisk) { |
| 232 | + if resp.success { |
| 233 | + self.disk_stats = resp.data.and_then(|d| serde_json::from_value(d).ok()).unwrap_or_default(); |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + // Get disk history |
| 238 | + if let Some(resp) = self.send_command(&Command::GetDiskHistory { count: Some(60) }) { |
| 239 | + if resp.success { |
| 240 | + self.disk_history = resp.data.and_then(|d| serde_json::from_value(d).ok()).unwrap_or_default(); |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 208 | 244 | // Get processes sorted by current tab's resource |
| 209 | 245 | let sort_field = match self.tab_bar.active() { |
| 210 | 246 | Tab::Cpu => SortField::Cpu, |
| 211 | 247 | Tab::Memory => SortField::Memory, |
| 248 | + Tab::Network | Tab::Disk => SortField::Cpu, // Default for I/O tabs |
| 212 | 249 | }; |
| 213 | 250 | if let Some(resp) = self.send_command(&Command::GetProcesses { |
| 214 | 251 | sort_by: Some(sort_field), |
@@ -398,6 +435,140 @@ impl App { |
| 398 | 435 | }).collect()); |
| 399 | 436 | graph.render(&ctx, graph_rect, &[mem_series, swap_series], &self.theme); |
| 400 | 437 | } |
| 438 | + |
| 439 | + Tab::Network => { |
| 440 | + // Network label - show total rx/tx rates across all interfaces |
| 441 | + let (total_rx, total_tx) = self.network_stats.iter().fold((0.0, 0.0), |acc, s| { |
| 442 | + (acc.0 + s.rx_rate, acc.1 + s.tx_rate) |
| 443 | + }); |
| 444 | + let net_label = format!( |
| 445 | + "Network: {} \u{2193} / {} \u{2191}", |
| 446 | + format_rate(total_rx), |
| 447 | + format_rate(total_tx) |
| 448 | + ); |
| 449 | + self.renderer.text( |
| 450 | + &net_label, |
| 451 | + CONTENT_PADDING as f64, |
| 452 | + y as f64 + 14.0, |
| 453 | + &TextStyle { |
| 454 | + font_family: "monospace".to_string(), |
| 455 | + font_size: 12.0, |
| 456 | + color: self.theme.network_color, |
| 457 | + ..Default::default() |
| 458 | + }, |
| 459 | + )?; |
| 460 | + |
| 461 | + // Interface count on right |
| 462 | + let iface_info = format!("{} interfaces", self.network_stats.len()); |
| 463 | + self.renderer.text( |
| 464 | + &iface_info, |
| 465 | + (self.width - 100) as f64, |
| 466 | + y as f64 + 14.0, |
| 467 | + &TextStyle { |
| 468 | + font_family: "monospace".to_string(), |
| 469 | + font_size: 10.0, |
| 470 | + color: self.theme.text_secondary, |
| 471 | + ..Default::default() |
| 472 | + }, |
| 473 | + )?; |
| 474 | + y += 20; |
| 475 | + |
| 476 | + // Network Graph - show rx/tx rates over time |
| 477 | + let graph_rect = Rect::new(CONTENT_PADDING as i32, y, graph_width, GRAPH_HEIGHT); |
| 478 | + let mut rx_series = DataSeries::new("Download", self.theme.network_color); |
| 479 | + let mut tx_series = DataSeries::new("Upload", self.theme.swap_color); |
| 480 | + |
| 481 | + // Calculate max rate for scaling |
| 482 | + let max_rate = self.network_history.iter() |
| 483 | + .flat_map(|stats| stats.iter().map(|s| s.rx_rate.max(s.tx_rate))) |
| 484 | + .fold(1.0, f64::max); |
| 485 | + |
| 486 | + // Convert rates to percentage of max for graph |
| 487 | + rx_series.set_values( |
| 488 | + self.network_history.iter() |
| 489 | + .map(|stats| { |
| 490 | + let total: f64 = stats.iter().map(|s| s.rx_rate).sum(); |
| 491 | + (total / max_rate) * 100.0 |
| 492 | + }) |
| 493 | + .collect() |
| 494 | + ); |
| 495 | + tx_series.set_values( |
| 496 | + self.network_history.iter() |
| 497 | + .map(|stats| { |
| 498 | + let total: f64 = stats.iter().map(|s| s.tx_rate).sum(); |
| 499 | + (total / max_rate) * 100.0 |
| 500 | + }) |
| 501 | + .collect() |
| 502 | + ); |
| 503 | + graph.render(&ctx, graph_rect, &[rx_series, tx_series], &self.theme); |
| 504 | + } |
| 505 | + |
| 506 | + Tab::Disk => { |
| 507 | + // Disk label - show total read/write rates |
| 508 | + let (total_read, total_write) = self.disk_stats.iter().fold((0.0, 0.0), |acc, s| { |
| 509 | + (acc.0 + s.read_rate, acc.1 + s.write_rate) |
| 510 | + }); |
| 511 | + let disk_label = format!( |
| 512 | + "Disk: {} read / {} write", |
| 513 | + format_rate(total_read), |
| 514 | + format_rate(total_write) |
| 515 | + ); |
| 516 | + self.renderer.text( |
| 517 | + &disk_label, |
| 518 | + CONTENT_PADDING as f64, |
| 519 | + y as f64 + 14.0, |
| 520 | + &TextStyle { |
| 521 | + font_family: "monospace".to_string(), |
| 522 | + font_size: 12.0, |
| 523 | + color: self.theme.disk_color, |
| 524 | + ..Default::default() |
| 525 | + }, |
| 526 | + )?; |
| 527 | + |
| 528 | + // Device count on right |
| 529 | + let dev_info = format!("{} devices", self.disk_stats.len()); |
| 530 | + self.renderer.text( |
| 531 | + &dev_info, |
| 532 | + (self.width - 100) as f64, |
| 533 | + y as f64 + 14.0, |
| 534 | + &TextStyle { |
| 535 | + font_family: "monospace".to_string(), |
| 536 | + font_size: 10.0, |
| 537 | + color: self.theme.text_secondary, |
| 538 | + ..Default::default() |
| 539 | + }, |
| 540 | + )?; |
| 541 | + y += 20; |
| 542 | + |
| 543 | + // Disk Graph - show read/write rates over time |
| 544 | + let graph_rect = Rect::new(CONTENT_PADDING as i32, y, graph_width, GRAPH_HEIGHT); |
| 545 | + let mut read_series = DataSeries::new("Read", self.theme.disk_color); |
| 546 | + let mut write_series = DataSeries::new("Write", self.theme.swap_color); |
| 547 | + |
| 548 | + // Calculate max rate for scaling |
| 549 | + let max_rate = self.disk_history.iter() |
| 550 | + .flat_map(|stats| stats.iter().map(|s| s.read_rate.max(s.write_rate))) |
| 551 | + .fold(1.0, f64::max); |
| 552 | + |
| 553 | + // Convert rates to percentage of max for graph |
| 554 | + read_series.set_values( |
| 555 | + self.disk_history.iter() |
| 556 | + .map(|stats| { |
| 557 | + let total: f64 = stats.iter().map(|s| s.read_rate).sum(); |
| 558 | + (total / max_rate) * 100.0 |
| 559 | + }) |
| 560 | + .collect() |
| 561 | + ); |
| 562 | + write_series.set_values( |
| 563 | + self.disk_history.iter() |
| 564 | + .map(|stats| { |
| 565 | + let total: f64 = stats.iter().map(|s| s.write_rate).sum(); |
| 566 | + (total / max_rate) * 100.0 |
| 567 | + }) |
| 568 | + .collect() |
| 569 | + ); |
| 570 | + graph.render(&ctx, graph_rect, &[read_series, write_series], &self.theme); |
| 571 | + } |
| 401 | 572 | } |
| 402 | 573 | |
| 403 | 574 | // Render process list |
@@ -463,6 +634,7 @@ impl App { |
| 463 | 634 | let sort_field = match tab { |
| 464 | 635 | Tab::Cpu => SortField::Cpu, |
| 465 | 636 | Tab::Memory => SortField::Memory, |
| 637 | + Tab::Network | Tab::Disk => SortField::Cpu, |
| 466 | 638 | }; |
| 467 | 639 | self.process_list.set_sort(sort_field); |
| 468 | 640 | // Force refresh to get re-sorted processes |
@@ -547,15 +719,28 @@ impl App { |
| 547 | 719 | self.last_refresh = Instant::now() - std::time::Duration::from_secs(10); |
| 548 | 720 | ev_loop.request_redraw(); |
| 549 | 721 | } |
| 722 | + Key::Char('3') => { |
| 723 | + self.tab_bar.set_active(Tab::Network); |
| 724 | + self.last_refresh = Instant::now() - std::time::Duration::from_secs(10); |
| 725 | + ev_loop.request_redraw(); |
| 726 | + } |
| 727 | + Key::Char('4') => { |
| 728 | + self.tab_bar.set_active(Tab::Disk); |
| 729 | + self.last_refresh = Instant::now() - std::time::Duration::from_secs(10); |
| 730 | + ev_loop.request_redraw(); |
| 731 | + } |
| 550 | 732 | Key::Tab => { |
| 551 | 733 | let new_tab = match self.tab_bar.active() { |
| 552 | 734 | Tab::Cpu => Tab::Memory, |
| 553 | | - Tab::Memory => Tab::Cpu, |
| 735 | + Tab::Memory => Tab::Network, |
| 736 | + Tab::Network => Tab::Disk, |
| 737 | + Tab::Disk => Tab::Cpu, |
| 554 | 738 | }; |
| 555 | 739 | self.tab_bar.set_active(new_tab); |
| 556 | 740 | let sort_field = match new_tab { |
| 557 | 741 | Tab::Cpu => SortField::Cpu, |
| 558 | 742 | Tab::Memory => SortField::Memory, |
| 743 | + Tab::Network | Tab::Disk => SortField::Cpu, |
| 559 | 744 | }; |
| 560 | 745 | self.process_list.set_sort(sort_field); |
| 561 | 746 | self.last_refresh = Instant::now() - std::time::Duration::from_secs(10); |
@@ -627,3 +812,20 @@ fn format_bytes(bytes: u64) -> String { |
| 627 | 812 | format!("{} B", bytes) |
| 628 | 813 | } |
| 629 | 814 | } |
| 815 | + |
| 816 | +/// Format rate (bytes/second) to human-readable string. |
| 817 | +fn format_rate(bytes_per_sec: f64) -> String { |
| 818 | + const KIB: f64 = 1024.0; |
| 819 | + const MIB: f64 = KIB * 1024.0; |
| 820 | + const GIB: f64 = MIB * 1024.0; |
| 821 | + |
| 822 | + if bytes_per_sec >= GIB { |
| 823 | + format!("{:.1} GiB/s", bytes_per_sec / GIB) |
| 824 | + } else if bytes_per_sec >= MIB { |
| 825 | + format!("{:.1} MiB/s", bytes_per_sec / MIB) |
| 826 | + } else if bytes_per_sec >= KIB { |
| 827 | + format!("{:.1} KiB/s", bytes_per_sec / KIB) |
| 828 | + } else { |
| 829 | + format!("{:.0} B/s", bytes_per_sec) |
| 830 | + } |
| 831 | +} |