Skip to content

Commit

Permalink
refactor: simplify system status management with SysinfoInstance (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mogyuchi authored Dec 4, 2024
1 parent 498dd01 commit d23832a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 43 deletions.
31 changes: 8 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn test_function() {

mod gpu;
mod status;
mod sysinfo_instance;
mod thread_message;
mod threads;
mod unix_to_date;
Expand All @@ -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,
Expand Down Expand Up @@ -101,17 +101,9 @@ fn update() -> Result<(), Box<dyn (::std::error::Error)>> {
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));

Expand All @@ -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 {
Expand Down Expand Up @@ -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.");
Expand Down
19 changes: 10 additions & 9 deletions src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
Expand All @@ -101,21 +102,21 @@ impl SystemStatus {
}
};

let cpus: Vec<CoreData> = sys.cpus().iter().map(Into::into).collect();
let cpus: Vec<CoreData> = system.cpus().iter().map(Into::into).collect();

let cpu = CpuData {
model: cpu_name.clone(),
cpus,
};

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());
Expand Down
27 changes: 27 additions & 0 deletions src/sysinfo_instance.rs
Original file line number Diff line number Diff line change
@@ -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();
}
}
15 changes: 4 additions & 11 deletions src/threads/mod.rs
Original file line number Diff line number Diff line change
@@ -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<ArcSwap<SystemStatus>>) {
let mut sys = System::new();
let mut disks = Disks::new();
let mut sys = SysinfoInstance::new();

let builder = thread::Builder::new();

Expand All @@ -16,13 +14,8 @@ pub fn spawn_monitor(shared_data: Arc<ArcSwap<SystemStatus>>) {
.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();
Expand Down

0 comments on commit d23832a

Please sign in to comment.