feat: add fetching computer statistics

This commit is contained in:
trafficlunar 2024-11-29 15:36:34 +00:00
parent ce32c1a67e
commit f18ec348cb
4 changed files with 103 additions and 1 deletions

75
Cargo.lock generated
View file

@ -230,6 +230,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"dotenvy", "dotenvy",
"notify-rust", "notify-rust",
"sysinfo",
"tungstenite", "tungstenite",
] ]
@ -242,6 +243,12 @@ dependencies = [
"crossbeam-utils", "crossbeam-utils",
] ]
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]] [[package]]
name = "cpufeatures" name = "cpufeatures"
version = "0.2.16" version = "0.2.16"
@ -251,6 +258,25 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]] [[package]]
name = "crossbeam-utils" name = "crossbeam-utils"
version = "0.8.20" version = "0.8.20"
@ -319,6 +345,12 @@ version = "0.15.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b"
[[package]]
name = "either"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
[[package]] [[package]]
name = "endi" name = "endi"
version = "1.1.0" version = "1.1.0"
@ -611,6 +643,15 @@ dependencies = [
"zbus", "zbus",
] ]
[[package]]
name = "ntapi"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "num-conv" name = "num-conv"
version = "0.1.0" version = "0.1.0"
@ -787,6 +828,26 @@ dependencies = [
"getrandom", "getrandom",
] ]
[[package]]
name = "rayon"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]] [[package]]
name = "redox_users" name = "redox_users"
version = "0.4.6" version = "0.4.6"
@ -894,6 +955,20 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "sysinfo"
version = "0.32.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c33cd241af0f2e9e3b5c32163b873b29956890b5342e6745b917ce9d490f4af"
dependencies = [
"core-foundation-sys",
"libc",
"memchr",
"ntapi",
"rayon",
"windows",
]
[[package]] [[package]]
name = "tauri-winrt-notification" name = "tauri-winrt-notification"
version = "0.2.1" version = "0.2.1"

View file

@ -6,4 +6,5 @@ edition = "2021"
[dependencies] [dependencies]
dotenvy = "0.15.7" dotenvy = "0.15.7"
notify-rust = "4.11.3" notify-rust = "4.11.3"
sysinfo = "0.32.1"
tungstenite = "0.24.0" tungstenite = "0.24.0"

25
src/computer.rs Normal file
View file

@ -0,0 +1,25 @@
use std::{net::TcpStream, thread, time::Duration};
use sysinfo::System;
use tungstenite::{stream::MaybeTlsStream, WebSocket};
use crate::websocket;
pub fn start_sending(socket: &mut WebSocket<MaybeTlsStream<TcpStream>>) {
let mut sys = System::new();
loop {
sys.refresh_cpu_usage();
sys.refresh_memory();
let cpu_usage = sys.global_cpu_usage().floor() as u8;
let total_memory = sys.total_memory();
let used_memory = sys.used_memory();
let memory_usage = ((used_memory as f64) / (total_memory as f64) * 100.0).floor() as u8;
websocket::send(socket, cpu_usage, memory_usage);
thread::sleep(Duration::from_secs(60));
}
}

View file

@ -1,13 +1,14 @@
use std::error::Error; use std::error::Error;
mod websocket; mod websocket;
mod computer;
mod notifications; mod notifications;
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
dotenvy::dotenv()?; dotenvy::dotenv()?;
let mut socket = websocket::connect().unwrap(); let mut socket = websocket::connect().unwrap();
websocket::send(&mut socket, 1, 1); computer::start_sending(&mut socket);
Ok(()) Ok(())
} }