From 27217bfc50a2dc9bbeb73a8b2d512f0d6bfc25fc Mon Sep 17 00:00:00 2001 From: Ward from fusion-voyager-3 Date: Fri, 11 Oct 2024 01:16:05 +0300 Subject: [PATCH] Add speed meter to apt --- src/bin/apt/apt_full_upgrade/main.rs | 3 +++ src/bin/apt/apt_update/main.rs | 2 ++ src/bin/gui/apt_update_page/mod.rs | 28 +++++++++++++++++++++++ src/bin/gui/apt_update_page/process.rs | 28 +++++++++++++++++++++++ src/lib/apt_update_progress_socket/mod.rs | 17 +++++++++++++- 5 files changed, 77 insertions(+), 1 deletion(-) diff --git a/src/bin/apt/apt_full_upgrade/main.rs b/src/bin/apt/apt_full_upgrade/main.rs index 629c0c4..15e4ffd 100644 --- a/src/bin/apt/apt_full_upgrade/main.rs +++ b/src/bin/apt/apt_full_upgrade/main.rs @@ -18,6 +18,8 @@ fn main() { let percent_socket_path = "/tmp/pika_apt_upgrade_percent.sock"; let status_socket_path = "/tmp/pika_apt_upgrade_status.sock"; + let speed_socket_path = "/tmp/pika_apt_upgrade_speed.sock"; + let json_file_path = "/tmp/pika-apt-exclusions.json"; let mut excluded_updates_vec: Vec = Vec::new(); @@ -74,6 +76,7 @@ fn main() { let mut acquire_progress = AcquireProgress::new(AptUpdateProgressSocket::new( percent_socket_path, status_socket_path, + speed_socket_path, &hit_strfmt_trans_str, &fetch_strfmt_trans_str, &done_strfmt_trans_str, diff --git a/src/bin/apt/apt_update/main.rs b/src/bin/apt/apt_update/main.rs index d49f547..c0592ea 100644 --- a/src/bin/apt/apt_update/main.rs +++ b/src/bin/apt/apt_update/main.rs @@ -26,9 +26,11 @@ fn main() { let update_cache = new_cache!().unwrap(); let percent_socket_path = "/tmp/pika_apt_update_percent.sock"; let status_socket_path = "/tmp/pika_apt_update_status.sock"; + let speed_socket_path = "/tmp/pika_apt_update_speed.sock"; match update_cache.update(&mut AcquireProgress::new(AptUpdateProgressSocket::new( percent_socket_path, status_socket_path, + speed_socket_path, &hit_strfmt_trans_str, &fetch_strfmt_trans_str, &done_strfmt_trans_str, diff --git a/src/bin/gui/apt_update_page/mod.rs b/src/bin/gui/apt_update_page/mod.rs index 5f1d767..b523de3 100644 --- a/src/bin/gui/apt_update_page/mod.rs +++ b/src/bin/gui/apt_update_page/mod.rs @@ -39,6 +39,8 @@ pub fn apt_update_page( ) -> gtk::Box { let (update_percent_sender, update_percent_receiver) = async_channel::unbounded::(); let update_percent_sender = update_percent_sender.clone(); + let (update_speed_sender, update_speed_receiver) = async_channel::unbounded::(); + let update_speed_sender = update_speed_sender.clone(); let (update_status_sender, update_status_receiver) = async_channel::unbounded::(); let update_status_sender = update_status_sender.clone(); let update_status_sender_clone0 = update_status_sender.clone(); @@ -56,6 +58,13 @@ pub fn apt_update_page( )); }); + thread::spawn(move || { + Runtime::new().unwrap().block_on(start_socket_server_no_log( + update_speed_sender, + "/tmp/pika_apt_update_speed.sock", + )); + }); + thread::spawn(move || { Runtime::new().unwrap().block_on(start_socket_server( update_status_sender, @@ -170,7 +179,14 @@ pub fn apt_update_page( .width_request(128) .build(); + let apt_speed_label = gtk::Label::builder() + .halign(Align::Center) + .margin_top(10) + .margin_bottom(10) + .build(); + apt_update_dialog_child_box.append(&apt_update_dialog_spinner); + apt_update_dialog_child_box.append(&apt_speed_label); apt_update_dialog_child_box.append(&apt_update_dialog_progress_bar); let apt_update_dialog = adw::MessageDialog::builder() @@ -300,6 +316,18 @@ pub fn apt_update_page( } )); + let update_speed_server_context = MainContext::default(); + // The main loop executes the asynchronous block + update_speed_server_context.spawn_local(clone!( + #[strong] + apt_speed_label, + async move { + while let Ok(state) = update_speed_receiver.recv().await { + apt_speed_label.set_label(&state); + } + } + )); + let update_status_server_context = MainContext::default(); // The main loop executes the asynchronous block update_status_server_context.spawn_local(clone!( diff --git a/src/bin/gui/apt_update_page/process.rs b/src/bin/gui/apt_update_page/process.rs index 46b676f..f88331a 100644 --- a/src/bin/gui/apt_update_page/process.rs +++ b/src/bin/gui/apt_update_page/process.rs @@ -385,6 +385,8 @@ fn apt_full_upgrade_from_socket( ) { let (upgrade_percent_sender, upgrade_percent_receiver) = async_channel::unbounded::(); let upgrade_percent_sender = upgrade_percent_sender.clone(); + let (upgrade_speed_sender, upgrade_speed_receiver) = async_channel::unbounded::(); + let upgrade_speed_sender = upgrade_speed_sender.clone(); let (upgrade_status_sender, upgrade_status_receiver) = async_channel::unbounded::(); let upgrade_status_sender = upgrade_status_sender.clone(); let upgrade_status_sender_clone0 = upgrade_status_sender.clone(); @@ -402,6 +404,13 @@ fn apt_full_upgrade_from_socket( )); }); + thread::spawn(move || { + Runtime::new().unwrap().block_on(start_socket_server_no_log( + upgrade_speed_sender, + "/tmp/pika_apt_upgrade_speed.sock", + )); + }); + thread::spawn(move || { Runtime::new().unwrap().block_on(start_socket_server( upgrade_status_sender, @@ -456,8 +465,15 @@ fn apt_full_upgrade_from_socket( .height_request(128) .width_request(128) .build(); + + let apt_speed_label = gtk::Label::builder() + .halign(Align::Center) + .margin_top(10) + .margin_bottom(10) + .build(); apt_upgrade_dialog_child_box.append(&apt_upgrade_dialog_spinner); + apt_upgrade_dialog_child_box.append(&apt_speed_label); apt_upgrade_dialog_child_box.append(&apt_upgrade_dialog_progress_bar); let apt_upgrade_dialog = adw::MessageDialog::builder() @@ -513,6 +529,18 @@ fn apt_full_upgrade_from_socket( } )); + let upgrade_speed_server_context = MainContext::default(); + // The main loop executes the asynchronous block + upgrade_speed_server_context.spawn_local(clone!( + #[weak] + apt_speed_label, + async move { + while let Ok(state) = upgrade_speed_receiver.recv().await { + apt_speed_label.set_label(&state); + } + } + )); + let upgrade_status_server_context = MainContext::default(); // The main loop executes the asynchronous block upgrade_status_server_context.spawn_local(clone!( diff --git a/src/lib/apt_update_progress_socket/mod.rs b/src/lib/apt_update_progress_socket/mod.rs index f1d70bb..f248238 100644 --- a/src/lib/apt_update_progress_socket/mod.rs +++ b/src/lib/apt_update_progress_socket/mod.rs @@ -7,9 +7,11 @@ use tokio::net::UnixStream; use tokio::runtime::Runtime; pub struct AptUpdateProgressSocket<'a> { + last_pulse_bytes: u64, pulse_interval: usize, percent_socket_path: &'a str, status_socket_path: &'a str, + speed_socket_path: &'a str, hit_strfmt_trans_str: &'a str, fetch_strfmt_trans_str: &'a str, done_strfmt_trans_str: &'a str, @@ -21,15 +23,18 @@ impl<'a> AptUpdateProgressSocket<'a> { pub fn new( percent_socket_path: &'a str, status_socket_path: &'a str, + speed_socket_path: &'a str, hit_strfmt_trans_str: &'a str, fetch_strfmt_trans_str: &'a str, done_strfmt_trans_str: &'a str, fail_strfmt_trans_str: &'a str, ) -> Self { let progress = Self { - pulse_interval: 0, + last_pulse_bytes: 0, + pulse_interval: 1000000, percent_socket_path: percent_socket_path, status_socket_path: status_socket_path, + speed_socket_path: speed_socket_path, hit_strfmt_trans_str: hit_strfmt_trans_str, fetch_strfmt_trans_str: fetch_strfmt_trans_str, done_strfmt_trans_str: done_strfmt_trans_str, @@ -156,10 +161,20 @@ impl<'a> DynAcquireProgress for AptUpdateProgressSocket<'a> { fn pulse(&mut self, status: &AcqTextStatus, _owner: &PkgAcquire) { let progress_percent: f32 = (status.current_bytes() as f32 * 100.0) / status.total_bytes() as f32; + let speed = if self.pulse_interval != 0 { + (status.current_bytes() as f64 - self.last_pulse_bytes as f64) / (self.pulse_interval as f64 / 1000000.0) + } else { + status.current_bytes() as f64 - self.last_pulse_bytes as f64 + }; + self.last_pulse_bytes = status.current_bytes(); Runtime::new().unwrap().block_on(send_progress_percent( progress_percent, self.percent_socket_path, )); + Runtime::new().unwrap().block_on(send_progress_status( + &(pretty_bytes::converter::convert(speed) + "ps").to_lowercase(), + self.speed_socket_path, + )); } }