fmt: apply rustfmt
This commit is contained in:
+19
-13
@@ -4,25 +4,26 @@
|
||||
mod metrics;
|
||||
mod sampler;
|
||||
mod state;
|
||||
mod ws;
|
||||
mod types;
|
||||
mod ws;
|
||||
|
||||
use axum::{routing::get, Router};
|
||||
use std::{collections::HashMap, net::SocketAddr, sync::Arc, time::Duration, sync::atomic::AtomicUsize};
|
||||
use sysinfo::{
|
||||
Components, CpuRefreshKind, Disks, MemoryRefreshKind, Networks, ProcessRefreshKind, RefreshKind,
|
||||
System,
|
||||
use std::{
|
||||
collections::HashMap, net::SocketAddr, sync::atomic::AtomicUsize, sync::Arc, time::Duration,
|
||||
};
|
||||
use tokio::sync::{Mutex, RwLock, Notify};
|
||||
use sysinfo::{
|
||||
Components, CpuRefreshKind, Disks, MemoryRefreshKind, Networks, ProcessRefreshKind,
|
||||
RefreshKind, System,
|
||||
};
|
||||
use tokio::sync::{Mutex, Notify, RwLock};
|
||||
use tracing_subscriber::EnvFilter;
|
||||
|
||||
use state::{AppState, SharedTotals};
|
||||
use sampler::spawn_sampler;
|
||||
use state::{AppState, SharedTotals};
|
||||
use ws::ws_handler;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
||||
// Init logging; configure with RUST_LOG (e.g., RUST_LOG=info).
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(EnvFilter::from_default_env())
|
||||
@@ -60,7 +61,9 @@ async fn main() {
|
||||
// new: adaptive sampling controls
|
||||
client_count: Arc::new(AtomicUsize::new(0)),
|
||||
wake_sampler: Arc::new(Notify::new()),
|
||||
auth_token: std::env::var("SOCKTOP_TOKEN").ok().filter(|s| !s.is_empty()),
|
||||
auth_token: std::env::var("SOCKTOP_TOKEN")
|
||||
.ok()
|
||||
.filter(|s| !s.is_empty()),
|
||||
};
|
||||
|
||||
// Start background sampler (adjust cadence as needed)
|
||||
@@ -68,7 +71,9 @@ async fn main() {
|
||||
|
||||
// Web app
|
||||
let port = resolve_port();
|
||||
let app = Router::new().route("/ws", get(ws_handler)).with_state(state);
|
||||
let app = Router::new()
|
||||
.route("/ws", get(ws_handler))
|
||||
.with_state(state);
|
||||
|
||||
let addr = SocketAddr::from(([0, 0, 0, 0], port));
|
||||
|
||||
@@ -82,7 +87,6 @@ async fn main() {
|
||||
|
||||
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
|
||||
}
|
||||
|
||||
// Resolve the listening port from CLI args/env with a 3000 default.
|
||||
@@ -97,7 +101,10 @@ fn resolve_port() -> u16 {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
eprintln!("Warning: invalid SOCKTOP_PORT='{}'; using default {}", s, DEFAULT);
|
||||
eprintln!(
|
||||
"Warning: invalid SOCKTOP_PORT='{}'; using default {}",
|
||||
s, DEFAULT
|
||||
);
|
||||
}
|
||||
|
||||
let mut args = std::env::args().skip(1);
|
||||
@@ -133,4 +140,3 @@ fn resolve_port() -> u16 {
|
||||
|
||||
DEFAULT
|
||||
}
|
||||
|
||||
|
||||
@@ -121,8 +121,11 @@ pub fn best_cpu_temp(components: &Components) -> Option<f32> {
|
||||
.iter()
|
||||
.filter(|c| {
|
||||
let label = c.label().to_lowercase();
|
||||
label.contains("cpu") || label.contains("package") || label.contains("tctl") || label.contains("tdie")
|
||||
label.contains("cpu")
|
||||
|| label.contains("package")
|
||||
|| label.contains("tctl")
|
||||
|| label.contains("tdie")
|
||||
})
|
||||
.filter_map(|c| c.temperature())
|
||||
.max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,13 +5,16 @@ use crate::metrics::collect_metrics;
|
||||
use crate::state::AppState;
|
||||
//use serde_json::to_string;
|
||||
use tokio::task::JoinHandle;
|
||||
use tokio::time::{Duration, interval, MissedTickBehavior};
|
||||
use tokio::time::{interval, Duration, MissedTickBehavior};
|
||||
|
||||
pub fn spawn_sampler(state: AppState, period: Duration) -> JoinHandle<()> {
|
||||
tokio::spawn(async move {
|
||||
let idle_period = Duration::from_secs(10);
|
||||
loop {
|
||||
let active = state.client_count.load(std::sync::atomic::Ordering::Relaxed) > 0;
|
||||
let active = state
|
||||
.client_count
|
||||
.load(std::sync::atomic::Ordering::Relaxed)
|
||||
> 0;
|
||||
let mut ticker = interval(if active { period } else { idle_period });
|
||||
ticker.set_missed_tick_behavior(MissedTickBehavior::Skip);
|
||||
ticker.tick().await;
|
||||
@@ -33,4 +36,4 @@ pub fn spawn_sampler(state: AppState, period: Duration) -> JoinHandle<()> {
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//! Shared agent state: sysinfo handles and hot JSON cache.
|
||||
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
use sysinfo::{Components, Disks, Networks, System};
|
||||
use tokio::sync::{Mutex, RwLock, Notify};
|
||||
use tokio::sync::{Mutex, Notify, RwLock};
|
||||
|
||||
pub type SharedSystem = Arc<Mutex<System>>;
|
||||
pub type SharedNetworks = Arc<Mutex<Networks>>;
|
||||
@@ -27,4 +27,4 @@ pub struct AppState {
|
||||
pub client_count: Arc<AtomicUsize>,
|
||||
pub wake_sampler: Arc<Notify>,
|
||||
pub auth_token: Option<String>,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,4 +40,4 @@ pub struct Metrics {
|
||||
pub disks: Vec<DiskInfo>,
|
||||
pub networks: Vec<NetworkInfo>,
|
||||
pub top_processes: Vec<ProcessInfo>,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,4 +63,4 @@ async fn handle_socket(mut socket: WebSocket, state: AppState) {
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user