performance improvement: one function to get both processes and system stats

This commit is contained in:
Abdenasser 2024-11-04 21:43:01 +01:00
parent ae3d550db6
commit cbcfbf8b10
2 changed files with 14 additions and 28 deletions

View File

@ -32,13 +32,15 @@ struct SystemStats {
} }
#[tauri::command] #[tauri::command]
async fn get_processes(state: State<'_, AppState>) -> Result<Vec<ProcessInfo>, String> { async fn get_processes(state: State<'_, AppState>) -> Result<(Vec<ProcessInfo>, SystemStats), String> {
let mut sys = state.sys.lock().map_err(|_| "Failed to lock system state")?; let mut sys = state.sys.lock().map_err(|_| "Failed to lock system state")?;
sys.refresh_all(); sys.refresh_processes();
sys.refresh_memory();
sys.refresh_cpu();
let users_cache = UsersCache::new(); let users_cache = UsersCache::new();
Ok(sys.processes() let processes = sys.processes()
.iter() .iter()
.map(|(pid, process)| { .map(|(pid, process)| {
let status = match process.status() { let status = match process.status() {
@ -77,22 +79,18 @@ async fn get_processes(state: State<'_, AppState>) -> Result<Vec<ProcessInfo>, S
threads: None, threads: None,
} }
}) })
.collect()) .collect();
}
#[tauri::command]
async fn get_system_stats(state: State<'_, AppState>) -> Result<SystemStats, String> {
let mut sys = state.sys.lock().map_err(|_| "Failed to lock system state")?;
sys.refresh_all();
let load_avg = sys.load_average(); let load_avg = sys.load_average();
Ok(SystemStats { let system_stats = SystemStats {
cpu_usage: sys.cpus().iter().map(|cpu| cpu.cpu_usage()).collect(), cpu_usage: sys.cpus().iter().map(|cpu| cpu.cpu_usage()).collect(),
memory_total: sys.total_memory(), memory_total: sys.total_memory(),
memory_used: sys.used_memory(), memory_used: sys.used_memory(),
uptime: sys.uptime(), uptime: sys.uptime(),
load_avg: [load_avg.one, load_avg.five, load_avg.fifteen], load_avg: [load_avg.one, load_avg.five, load_avg.fifteen],
}) };
Ok((processes, system_stats))
} }
#[tauri::command] #[tauri::command]
@ -112,7 +110,6 @@ fn main() {
}) })
.invoke_handler(tauri::generate_handler![ .invoke_handler(tauri::generate_handler![
get_processes, get_processes,
get_system_stats,
kill_process kill_process
]) ])
.run(tauri::generate_context!()) .run(tauri::generate_context!())

View File

@ -102,7 +102,9 @@
async function getProcesses() { async function getProcesses() {
try { try {
processes = await invoke<Process[]>("get_processes"); const result = await invoke<[Process[], SystemStats]>("get_processes");
processes = result[0];
systemStats = result[1];
error = null; error = null;
} catch (e: unknown) { } catch (e: unknown) {
if (e instanceof Error) { if (e instanceof Error) {
@ -113,18 +115,6 @@
} }
} }
async function getSystemStats() {
try {
systemStats = await invoke<SystemStats>("get_system_stats");
} catch (e: unknown) {
if (e instanceof Error) {
error = e.message;
} else {
error = String(e);
}
}
}
async function killProcess(pid: number) { async function killProcess(pid: number) {
try { try {
const success = await invoke<boolean>("kill_process", { pid }); const success = await invoke<boolean>("kill_process", { pid });
@ -183,14 +173,13 @@
onMount(async () => { onMount(async () => {
try { try {
await Promise.all([getProcesses(), getSystemStats()]); await Promise.all([getProcesses()]);
} finally { } finally {
isLoading = false; isLoading = false;
} }
intervalId = setInterval(() => { intervalId = setInterval(() => {
getProcesses(); getProcesses();
getSystemStats();
}, 2000); }, 2000);
themeStore.init(); themeStore.init();