11use crate :: {
22 localization:: { Language , Localizer } ,
3- ssh:: SSHConnection ,
3+ ssh:: { SSHConnection , ServerStats } ,
44} ;
55use eframe:: egui;
66use serde:: { Deserialize , Serialize } ;
@@ -67,6 +67,7 @@ enum Task {
6767 WriteFile ( String , String ) ,
6868 /// Disconnect the active connection
6969 Disconnect ,
70+ FetchStats ,
7071}
7172
7273/// Represents the result of executing a Task.
@@ -95,6 +96,7 @@ enum TaskResult {
9596 WriteFileResult ( Result < ( ) , String > ) ,
9697 /// The result of disconnecting
9798 DisconnectResult ,
99+ FetchStatsResult ( Result < ServerStats , String > ) ,
98100}
99101
100102/// BackgroundWorker handles asynchronous tasks to avoid blocking the UI.
@@ -239,6 +241,16 @@ impl BackgroundWorker {
239241 }
240242 let _ = result_sender. send ( TaskResult :: DisconnectResult ) ;
241243 }
244+
245+ Task :: FetchStats => {
246+ if let Some ( conn) = connection. as_ref ( ) {
247+ let result = conn. fetch_stats ( ) ;
248+ let _ = result_sender. send ( TaskResult :: FetchStatsResult ( result) ) ;
249+ } else {
250+ let _ = result_sender
251+ . send ( TaskResult :: FetchStatsResult ( Err ( "Not connected" . into ( ) ) ) ) ;
252+ }
253+ }
242254 }
243255 }
244256 } ) ;
@@ -299,6 +311,7 @@ pub struct UIState {
299311 pub language : Language ,
300312 /// The localizer that holds translations
301313 pub localizer : Localizer ,
314+ pub server_stats : Option < ServerStats > ,
302315}
303316
304317impl Default for UIState {
@@ -325,6 +338,7 @@ impl Default for UIState {
325338 language : Language :: English ,
326339
327340 localizer : Localizer :: new ( ) ,
341+ server_stats : None ,
328342 }
329343 }
330344}
@@ -460,6 +474,21 @@ pub fn render_ui(ui: &mut egui::Ui, state: &mut UIState, _connection: &mut Optio
460474 ui. colored_label ( egui:: Color32 :: RED , error) ;
461475 }
462476 } else {
477+ ui. collapsing ( "Dashboard" , |ui| {
478+ if ui. button ( "Refresh Stats" ) . clicked ( ) {
479+ state. operation_in_progress = true ;
480+ let worker = state. worker . clone ( ) ;
481+ worker. lock ( ) . unwrap ( ) . send_task ( Task :: FetchStats ) ;
482+ }
483+
484+ if let Some ( stats) = & state. server_stats {
485+ ui. label ( format ! ( "CPU Usage:\n {}" , stats. cpu_usage) ) ;
486+ ui. label ( format ! ( "Memory Usage:\n {}" , stats. memory_usage) ) ;
487+ ui. label ( format ! ( "Disk Usage:\n {}" , stats. disk_usage) ) ;
488+ } else {
489+ ui. label ( "No stats available. Click 'Refresh Stats' to fetch." ) ;
490+ }
491+ } ) ;
463492 ui. heading ( state. localizer . t ( state. language , "ssh_file_manager" ) ) ;
464493
465494 ui. horizontal ( |ui| {
@@ -852,6 +881,16 @@ fn poll_worker(state: &mut UIState) {
852881 state. current_path = "/" . to_string ( ) ;
853882 state. error_message = Some ( "Disconnected" . to_string ( ) ) ;
854883 }
884+ TaskResult :: FetchStatsResult ( res) => match res {
885+ Ok ( stats) => {
886+ state. server_stats = Some ( stats) ;
887+ state. error_message = None ;
888+ }
889+ Err ( e) => {
890+ state. error_message = Some ( e) ;
891+ state. server_stats = None ;
892+ }
893+ } ,
855894 }
856895 }
857896}
0 commit comments