refactor: implement background monitoring thread and fix sysinfo API usage

This commit is contained in:
2026-02-23 18:58:03 +01:00
parent 6b0466a526
commit 969fb6a17d
6 changed files with 201 additions and 438 deletions

View File

@@ -4,85 +4,34 @@
)]
mod models;
mod monitor;
mod commands; // We'll keep this but gut it
mod cli;
mod profiler;
mod commands;
use sysinfo::System;
use tauri::{State, Manager};
use std::sync::Mutex;
use chrono::Utc;
use std::fs;
use clap::Parser;
use std::path::PathBuf;
use std::time::Duration;
use std::collections::HashMap;
use crate::monitor::Monitor;
use crate::models::GlobalStats;
use crate::models::*;
use crate::cli::Cli;
use crate::profiler::*;
use crate::commands::*;
struct AppState {
monitor: Monitor,
}
#[tauri::command]
fn get_latest_stats(state: State<AppState>) -> GlobalStats {
state.monitor.get_latest()
}
fn main() {
let cli = Cli::parse();
let mut initial_report: Option<Report> = None;
if let Some(file_path) = cli.file {
if let Ok(content) = fs::read_to_string(file_path) {
if let Ok(report) = serde_json::from_str(&content) {
initial_report = Some(report);
}
}
}
if cli.headless {
println!("⚡ SysPulse: Starting headless profiling for {}s (interval: {}ms)...", cli.duration, cli.interval);
let mut sys = System::new_all();
let mut pss_cache = HashMap::new();
let start_time = Utc::now();
let mut snapshots = Vec::new();
let self_pid = std::process::id();
for i in 0..(cli.duration * 1000 / cli.interval) {
let syspulse_pids = get_syspulse_pids(self_pid, &sys);
snapshots.push(collect_snapshot(&mut sys, &syspulse_pids, &mut pss_cache, true));
std::thread::sleep(Duration::from_millis(cli.interval));
if (i + 1) % (1000 / cli.interval) == 0 {
println!(" Progress: {}/{}s", (i + 1) * cli.interval / 1000, cli.duration);
}
}
let report = generate_report(start_time, snapshots, ProfilingMode::Global, None);
let json = serde_json::to_string_pretty(&report).unwrap();
let out_path = cli.output.unwrap_or_else(|| PathBuf::from(format!("syspulse_report_{}.json", Utc::now().format("%Y%m%d_%H%M%S"))));
fs::write(&out_path, json).expect("Failed to write report");
println!("✅ Report saved to: {:?}", out_path);
if cli.gui {
initial_report = Some(report);
} else {
return;
}
}
let monitor = Monitor::new();
monitor.start();
tauri::Builder::default()
.manage(AppState {
sys: Mutex::new(System::new_all()),
profiling: Mutex::new(ProfilingSession {
is_active: false,
mode: ProfilingMode::Global,
target_pid: None,
start_time: None,
snapshots: Vec::new()
}),
initial_report: Mutex::new(initial_report),
pss_cache: Mutex::new(HashMap::new())
monitor
})
.invoke_handler(tauri::generate_handler![
get_system_stats,
get_initial_report,
start_profiling,
start_targeted_profiling,
stop_profiling,
run_as_admin,
save_report
get_latest_stats
])
.run(tauri::generate_context!())
.expect("error while running tauri application");