mirror of
https://github.com/trafficlunar/computer-statistics.git
synced 2026-06-27 22:24:09 +00:00
feat: add fetching computer statistics
This commit is contained in:
parent
ce32c1a67e
commit
f18ec348cb
4 changed files with 103 additions and 1 deletions
25
src/computer.rs
Normal file
25
src/computer.rs
Normal 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));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +1,14 @@
|
|||
use std::error::Error;
|
||||
|
||||
mod websocket;
|
||||
mod computer;
|
||||
mod notifications;
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
dotenvy::dotenv()?;
|
||||
|
||||
let mut socket = websocket::connect().unwrap();
|
||||
websocket::send(&mut socket, 1, 1);
|
||||
computer::start_sending(&mut socket);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue