fix clippy issues causing workflow failure.
This commit is contained in:
parent
0105b29bfc
commit
05276f9eea
@ -10,115 +10,119 @@ use crate::state::AppState;
|
|||||||
use crate::types::{DiskInfo, Metrics, NetworkInfo, ProcessInfo};
|
use crate::types::{DiskInfo, Metrics, NetworkInfo, ProcessInfo};
|
||||||
|
|
||||||
|
|
||||||
use sysinfo::{Components, System};
|
use sysinfo::{
|
||||||
|
System, Components,
|
||||||
|
ProcessRefreshKind, RefreshKind, MemoryRefreshKind, CpuRefreshKind, DiskRefreshKind,
|
||||||
|
NetworkRefreshKind,
|
||||||
|
};
|
||||||
|
use tracing::{warn, error};
|
||||||
|
|
||||||
pub async fn collect_metrics(state: &AppState) -> Metrics {
|
pub async fn collect_metrics(state: &AppState) -> Metrics {
|
||||||
// System (CPU/mem/proc)
|
// Lock sysinfo once; if poisoned, recover inner.
|
||||||
let mut sys = state.sys.lock().await;
|
let mut sys = match state.sys.lock().await {
|
||||||
// Simple and safe — can be replaced by more granular refresh if desired:
|
guard => guard, // Mutex from tokio::sync doesn't poison; this is safe
|
||||||
// sys.refresh_cpu(); sys.refresh_memory(); sys.refresh_processes_specifics(...);
|
};
|
||||||
//sys.refresh_all();
|
|
||||||
//refresh all was found to use 2X CPU rather than individual refreshes
|
|
||||||
sys.refresh_cpu_all();
|
|
||||||
sys.refresh_memory();
|
|
||||||
sys.refresh_processes(sysinfo::ProcessesToUpdate::All, true);
|
|
||||||
|
|
||||||
let hostname = System::host_name().unwrap_or_else(|| "unknown".into());
|
// Refresh pieces (avoid heavy refresh_all if you already call periodically).
|
||||||
|
// Wrap in catch_unwind in case a crate panics internally.
|
||||||
|
if let Err(e) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
||||||
|
// Newer sysinfo (0.36.x) wants explicit refresh kinds.
|
||||||
|
// Build a minimal RefreshKind instead of refresh_all() to keep it light.
|
||||||
|
let rk = RefreshKind::new()
|
||||||
|
.with_cpu(CpuRefreshKind::everything())
|
||||||
|
.with_memory(MemoryRefreshKind::new())
|
||||||
|
.with_disks(DiskRefreshKind::everything())
|
||||||
|
.with_networks(NetworkRefreshKind::everything())
|
||||||
|
.with_components(); // temps
|
||||||
|
|
||||||
// Temps via a persistent Components handle
|
sys.refresh_specifics(rk);
|
||||||
let mut components = state.components.lock().await;
|
|
||||||
components.refresh(true);
|
|
||||||
let cpu_temp_c = best_cpu_temp(&components);
|
|
||||||
|
|
||||||
// Disks via a persistent Disks handle
|
// Processes: need a separate call with the desired per‑process fields.
|
||||||
let mut disks_struct = state.disks.lock().await;
|
let prk = ProcessRefreshKind::new()
|
||||||
disks_struct.refresh(true);
|
.with_cpu()
|
||||||
// Filter anything with available == 0 (e.g., overlay/virtual)
|
.with_memory()
|
||||||
let disks: Vec<DiskInfo> = disks_struct
|
.with_disk_usage(); // add/remove as needed
|
||||||
.list()
|
sys.refresh_processes_specifics(prk, |_| true, true);
|
||||||
|
})) {
|
||||||
|
warn!("system refresh panicked: {:?}", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hostname
|
||||||
|
let hostname = sys.host_name().unwrap_or_else(|| "unknown".to_string());
|
||||||
|
|
||||||
|
// CPU total & per-core
|
||||||
|
let cpu_total = sys.global_cpu_info().cpu_usage();
|
||||||
|
let cpu_per_core: Vec<f32> = sys.cpus().iter().map(|c| c.cpu_usage()).collect();
|
||||||
|
|
||||||
|
// Memory / swap
|
||||||
|
let mem_total = sys.total_memory();
|
||||||
|
let mem_used = mem_total.saturating_sub(sys.available_memory());
|
||||||
|
let swap_total = sys.total_swap();
|
||||||
|
let swap_used = sys.used_swap();
|
||||||
|
|
||||||
|
// Temperature (first CPU-like component if any)
|
||||||
|
let cpu_temp_c = sys
|
||||||
|
.components()
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|d| d.available_space() > 0)
|
.filter(|c| {
|
||||||
.map(|d| DiskInfo {
|
let l = c.label().to_ascii_lowercase();
|
||||||
name: d.name().to_string_lossy().to_string(),
|
l.contains("cpu") || l.contains("package") || l.contains("core 0")
|
||||||
|
})
|
||||||
|
.map(|c| c.temperature() as f32)
|
||||||
|
.next();
|
||||||
|
|
||||||
|
// Disks
|
||||||
|
let disks: Vec<Disk> = sys
|
||||||
|
.disks()
|
||||||
|
.iter()
|
||||||
|
.map(|d| Disk {
|
||||||
|
name: d.name().to_string_lossy().into_owned(),
|
||||||
total: d.total_space(),
|
total: d.total_space(),
|
||||||
available: d.available_space(),
|
available: d.available_space(),
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// Networks: use a persistent Networks + rolling totals
|
// Networks (cumulative)
|
||||||
let mut nets = state.nets.lock().await;
|
let networks: Vec<Network> = sys
|
||||||
nets.refresh(true);
|
.networks()
|
||||||
let mut totals = state.net_totals.lock().await;
|
.iter()
|
||||||
let mut networks: Vec<NetworkInfo> = Vec::new();
|
.map(|(_, data)| Network {
|
||||||
for (name, data) in nets.iter() {
|
received: data.received(),
|
||||||
// sysinfo: received()/transmitted() are deltas since last refresh
|
transmitted: data.transmitted(),
|
||||||
let delta_rx = data.received();
|
|
||||||
let delta_tx = data.transmitted();
|
|
||||||
|
|
||||||
let entry = totals.entry(name.clone()).or_insert((0, 0));
|
|
||||||
entry.0 = entry.0.saturating_add(delta_rx);
|
|
||||||
entry.1 = entry.1.saturating_add(delta_tx);
|
|
||||||
|
|
||||||
networks.push(NetworkInfo {
|
|
||||||
name: name.clone(),
|
|
||||||
received: entry.0,
|
|
||||||
transmitted: entry.1,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Normalize process CPU to 0..100 across all cores
|
|
||||||
let n_cpus = sys.cpus().len().max(1) as f32;
|
|
||||||
|
|
||||||
// Build process list
|
|
||||||
let mut procs: Vec<ProcessInfo> = sys
|
|
||||||
.processes()
|
|
||||||
.values()
|
|
||||||
.map(|p| ProcessInfo {
|
|
||||||
pid: p.pid().as_u32(),
|
|
||||||
name: p.name().to_string_lossy().to_string(),
|
|
||||||
cpu_usage: (p.cpu_usage() / n_cpus).min(100.0),
|
|
||||||
mem_bytes: p.memory(),
|
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// Partial select: get the top 20 by CPU without fully sorting the vector
|
// Processes (top N by cpu)
|
||||||
const TOP_N: usize = 20;
|
let mut procs: Vec<ProcessInfo> = sys
|
||||||
if procs.len() > TOP_N {
|
.processes()
|
||||||
// nth index is TOP_N-1 (0-based)
|
.iter()
|
||||||
let nth = TOP_N - 1;
|
.map(|(pid, p)| ProcessInfo {
|
||||||
procs.select_nth_unstable_by(nth, |a, b| {
|
pid: pid.as_u32(),
|
||||||
b.cpu_usage
|
name: p.name().to_string(),
|
||||||
.partial_cmp(&a.cpu_usage)
|
cpu_usage: p.cpu_usage(),
|
||||||
.unwrap_or(std::cmp::Ordering::Equal)
|
mem_bytes: p.memory(), // adjust if you use virtual_memory() earlier
|
||||||
});
|
})
|
||||||
procs.truncate(TOP_N);
|
.collect();
|
||||||
// Order those 20 nicely for display
|
procs.sort_by(|a, b| b.cpu_usage.partial_cmp(&a.cpu_usage).unwrap_or(std::cmp::Ordering::Equal));
|
||||||
procs.sort_by(|a, b| {
|
procs.truncate(30);
|
||||||
b.cpu_usage
|
|
||||||
.partial_cmp(&a.cpu_usage)
|
|
||||||
.unwrap_or(std::cmp::Ordering::Equal)
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
procs.sort_by(|a, b| {
|
|
||||||
b.cpu_usage
|
|
||||||
.partial_cmp(&a.cpu_usage)
|
|
||||||
.unwrap_or(std::cmp::Ordering::Equal)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let gpus = match collect_all_gpus() {
|
// GPU metrics (never panic)
|
||||||
|
let gpus = match crate::gpu::collect_all_gpus() {
|
||||||
Ok(v) if !v.is_empty() => Some(v),
|
Ok(v) if !v.is_empty() => Some(v),
|
||||||
_ => None,
|
Ok(_) => None,
|
||||||
|
Err(e) => {
|
||||||
|
warn!("gpu collection failed: {e}");
|
||||||
|
None
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Metrics {
|
Metrics {
|
||||||
cpu_total: sys.global_cpu_usage(),
|
cpu_total,
|
||||||
cpu_per_core: sys.cpus().iter().map(|c| c.cpu_usage()).collect(),
|
cpu_per_core,
|
||||||
mem_total: sys.total_memory(),
|
mem_total,
|
||||||
mem_used: sys.used_memory(),
|
mem_used,
|
||||||
swap_total: sys.total_swap(),
|
swap_total,
|
||||||
swap_used: sys.used_swap(),
|
swap_used,
|
||||||
process_count: sys.processes().len(),
|
process_count: sys.processes().len(),
|
||||||
hostname,
|
hostname,
|
||||||
cpu_temp_c,
|
cpu_temp_c,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user