Skip to content

Commit

Permalink
Merge pull request #77 from Abdenasser/more-process-stats
Browse files Browse the repository at this point in the history
More process columns, real time process info modal.
  • Loading branch information
Abdenasser authored Nov 9, 2024
2 parents adb99e9 + 7f44f8d commit 7e6e9e8
Show file tree
Hide file tree
Showing 8 changed files with 358 additions and 65 deletions.
1 change: 1 addition & 0 deletions docs/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,7 @@ footer {
font-size: 2.5rem;
margin-bottom: 1rem;
background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
Expand Down
41 changes: 39 additions & 2 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use sysinfo::{
use tauri::State;
use std::sync::Mutex;
use std::collections::HashMap;
use std::time::Instant;
use std::time::{Instant, SystemTime, UNIX_EPOCH};

struct AppState {
sys: Mutex<System>,
Expand Down Expand Up @@ -58,6 +58,13 @@ struct ProcessInfo {
user: String,
command: String,
threads: Option<u32>,
environ: Vec<String>,
root: String,
virtual_memory: u64,
start_time: u64,
run_time: u64,
disk_usage: (u64, u64), // (read_bytes, written_bytes)
session_id: Option<u32>,
}

#[derive(serde::Serialize)]
Expand Down Expand Up @@ -96,6 +103,12 @@ async fn get_processes(state: State<'_, AppState>) -> Result<(Vec<ProcessInfo>,
let processes_data;
let system_stats;

// Get current time once for all calculations
let current_time = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_err(|e| e.to_string())?
.as_secs();

// Scope for system lock
{
let mut sys = state.sys.lock().map_err(|_| "Failed to lock system state")?;
Expand All @@ -109,6 +122,13 @@ async fn get_processes(state: State<'_, AppState>) -> Result<(Vec<ProcessInfo>,
.processes()
.iter()
.map(|(pid, process)| {
let start_time = process.start_time();
let run_time = if start_time > 0 {
current_time.saturating_sub(start_time)
} else {
0
};

(
pid.as_u32(),
process.name().to_string(),
Expand All @@ -118,6 +138,14 @@ async fn get_processes(state: State<'_, AppState>) -> Result<(Vec<ProcessInfo>,
process.memory(),
process.status(),
process.parent().map(|p| p.as_u32()),
process.environ().to_vec(),
process.root().to_string_lossy().into_owned(),
process.virtual_memory(),
start_time,
run_time, // Use calculated run_time
process.disk_usage().read_bytes,
process.disk_usage().written_bytes,
process.session_id().map(|id| id.as_u32()),
)
})
.collect::<Vec<_>>();
Expand Down Expand Up @@ -170,7 +198,9 @@ async fn get_processes(state: State<'_, AppState>) -> Result<(Vec<ProcessInfo>,
// Build the process info list
let processes = processes_data
.into_iter()
.map(|(pid, name, cmd, user_id, cpu_usage, memory, status, ppid)| {
.map(|(pid, name, cmd, user_id, cpu_usage, memory, status, ppid,
environ, root, virtual_memory, start_time, run_time,
disk_read, disk_written, session_id)| {
let static_info = process_cache.entry(pid).or_insert_with(|| {
ProcessStaticInfo {
name: name.clone(),
Expand All @@ -196,6 +226,13 @@ async fn get_processes(state: State<'_, AppState>) -> Result<(Vec<ProcessInfo>,
user: static_info.user.clone(),
command: static_info.command.clone(),
threads: None,
environ,
root,
virtual_memory,
start_time,
run_time,
disk_usage: (disk_read, disk_written),
session_id,
}
})
.collect();
Expand Down
1 change: 1 addition & 0 deletions src/lib/components/AppInfo.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
</button>

{#if showInfo}
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div class="info-panel" on:mouseleave={() => (showInfo = false)}>
<div class="info-content">
<pre class="ascii-art">{ASCII_ART}</pre>
Expand Down
Loading

0 comments on commit 7e6e9e8

Please sign in to comment.