From d23832a4903cbe5399d94cd31f41d843c5be37cb Mon Sep 17 00:00:00 2001 From: Mogyuchi Date: Wed, 4 Dec 2024 07:37:20 +0000 Subject: [PATCH] refactor: simplify system status management with SysinfoInstance (#525) --- src/lib.rs | 31 ++++++++----------------------- src/status.rs | 19 ++++++++++--------- src/sysinfo_instance.rs | 27 +++++++++++++++++++++++++++ src/threads/mod.rs | 15 ++++----------- 4 files changed, 49 insertions(+), 43 deletions(-) create mode 100644 src/sysinfo_instance.rs diff --git a/src/lib.rs b/src/lib.rs index 4979b0ad..910773b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ fn test_function() { mod gpu; mod status; +mod sysinfo_instance; mod thread_message; mod threads; mod unix_to_date; @@ -26,9 +27,8 @@ use std::{ thread, time::Duration, }; -use sysinfo::{CpuRefreshKind, Disks, MemoryRefreshKind, RefreshKind, System}; -use crate::status::SystemStatus; +use crate::{status::SystemStatus, sysinfo_instance::SysinfoInstance}; struct App { pub finish: bool, @@ -101,17 +101,9 @@ fn update() -> Result<(), Box> { pub fn start() { let _ = update(); - let mut system = System::new_with_specifics( - RefreshKind::new() - .with_cpu(CpuRefreshKind::new().with_cpu_usage()) - .with_memory(MemoryRefreshKind::everything()), - ); - let mut disks = Disks::new_with_refreshed_list(); + let mut sys = SysinfoInstance::new(); - let shared_data = Arc::new(ArcSwap::from_pointee(SystemStatus::get( - &mut system, - &mut disks, - ))); + let shared_data = Arc::new(ArcSwap::from_pointee(SystemStatus::get(&mut sys))); threads::spawn_monitor(Arc::clone(&shared_data)); @@ -127,13 +119,13 @@ pub fn start() { .reconnect_on_disconnect(true) .on(Event::Connect, |_, _| println!("Connected")) .on(Event::Close, |_, _| println!("Disconnected")) - .on("hi", |payload, socket| { + .on("hi", move |payload, socket| { match payload { Payload::Text(values) => println!("Received: {}", values[0]), Payload::Binary(bin_data) => println!("Received bytes: {:#?}", bin_data), _ => (), }; - init(socket); + init(&mut sys, socket); }) .on("sync", move |payload, socket| { match payload { @@ -161,18 +153,11 @@ pub fn start() { } } -fn init(socket: RawClient) { +fn init(sys: &mut SysinfoInstance, socket: RawClient) { print!("hi from server"); - let mut sys = System::new_with_specifics( - RefreshKind::new() - .with_cpu(CpuRefreshKind::new().with_cpu_usage()) - .with_memory(MemoryRefreshKind::everything()), - ); - let mut disks = Disks::new_with_refreshed_list(); - let pass = env::var("PASS").unwrap_or_default(); - let status = SystemStatus::get(&mut sys, &mut disks); + let status = SystemStatus::get(sys); socket .emit("hi", json!(status.with_pass(pass))) .expect("Failed to emit."); diff --git a/src/status.rs b/src/status.rs index 73672995..1d09d69b 100644 --- a/src/status.rs +++ b/src/status.rs @@ -2,9 +2,9 @@ use std::env; use cfg_if::cfg_if; use serde::{Deserialize, Serialize}; -use sysinfo::{Cpu, Disks, System}; +use sysinfo::{Cpu, System}; -use crate::{gpu, unix_to_date}; +use crate::{gpu, sysinfo_instance::SysinfoInstance, unix_to_date}; #[derive(Serialize, Deserialize, Clone, Debug)] pub struct CoreData { @@ -82,8 +82,9 @@ pub struct StatusDataWithPass { const GIT_DESCRIBE: &'static str = env!("GIT_DESCRIBE"); impl SystemStatus { - pub fn get(sys: &mut System, disks: &mut Disks) -> Self { - let cpu_name = sys.cpus()[0].brand().to_string(); + pub fn get(sys: &SysinfoInstance) -> Self { + let SysinfoInstance { system, disks } = sys; + let cpu_name = system.cpus()[0].brand().to_string(); let os_name = System::name().expect("Failed to get os name"); let os_version = System::os_version() .or(System::kernel_version()) @@ -101,7 +102,7 @@ impl SystemStatus { } }; - let cpus: Vec = sys.cpus().iter().map(Into::into).collect(); + let cpus: Vec = system.cpus().iter().map(Into::into).collect(); let cpu = CpuData { model: cpu_name.clone(), @@ -109,13 +110,13 @@ impl SystemStatus { }; let ram = RamData { - free: sys.available_memory(), - total: sys.total_memory(), + free: system.available_memory(), + total: system.total_memory(), }; let swap = SwapData { - free: sys.free_swap(), - total: sys.total_swap(), + free: system.free_swap(), + total: system.total_swap(), }; let uptime = unix_to_date::new(System::uptime()); diff --git a/src/sysinfo_instance.rs b/src/sysinfo_instance.rs new file mode 100644 index 00000000..63420219 --- /dev/null +++ b/src/sysinfo_instance.rs @@ -0,0 +1,27 @@ +use sysinfo::{CpuRefreshKind, Disks, MemoryRefreshKind, RefreshKind, System}; + +pub struct SysinfoInstance { + pub system: System, + pub disks: Disks, +} + +impl SysinfoInstance { + pub fn new() -> SysinfoInstance { + let system = System::new_with_specifics( + RefreshKind::new() + .with_cpu(CpuRefreshKind::new().with_cpu_usage()) + .with_memory(MemoryRefreshKind::everything()), + ); + let disks = Disks::new_with_refreshed_list(); + + SysinfoInstance { system, disks } + } + pub fn refresh(&mut self) { + self.system.refresh_specifics( + RefreshKind::new() + .with_cpu(CpuRefreshKind::new().with_cpu_usage()) + .with_memory(MemoryRefreshKind::everything()), + ); + self.disks.refresh_list(); + } +} diff --git a/src/threads/mod.rs b/src/threads/mod.rs index a95f8baa..0aa2ea38 100644 --- a/src/threads/mod.rs +++ b/src/threads/mod.rs @@ -1,13 +1,11 @@ use arc_swap::ArcSwap; -use sysinfo::{CpuRefreshKind, Disks, MemoryRefreshKind, RefreshKind, System}; use std::{sync::Arc, thread, time::Duration}; -use crate::status::SystemStatus; +use crate::{status::SystemStatus, sysinfo_instance::SysinfoInstance}; pub fn spawn_monitor(shared_data: Arc>) { - let mut sys = System::new(); - let mut disks = Disks::new(); + let mut sys = SysinfoInstance::new(); let builder = thread::Builder::new(); @@ -16,13 +14,8 @@ pub fn spawn_monitor(shared_data: Arc>) { .spawn(move || loop { thread::sleep(Duration::from_secs(1)); - sys.refresh_specifics( - RefreshKind::new() - .with_cpu(CpuRefreshKind::new().with_cpu_usage()) - .with_memory(MemoryRefreshKind::everything()), - ); - disks.refresh_list(); - let current_status = SystemStatus::get(&mut sys, &mut disks); + sys.refresh(); + let current_status = SystemStatus::get(&mut sys); shared_data.store(Arc::new(current_status)); }) .unwrap();