| 74 | } |
| 75 | |
| 76 | fn print_performance_line(stats: &Value) -> anyhow::Result<()> { |
| 77 | let current = stats |
| 78 | .get("current") |
| 79 | .and_then(Value::as_object) |
| 80 | .ok_or_else(|| anyhow::anyhow!("No current performance sample is available."))?; |
| 81 | let pid = current.get("pid").and_then(Value::as_i64).unwrap_or(0); |
| 82 | let process = stats |
| 83 | .get("processes") |
| 84 | .and_then(Value::as_array) |
| 85 | .and_then(|processes| { |
| 86 | processes |
| 87 | .iter() |
| 88 | .find(|process| process.get("pid").and_then(Value::as_i64) == Some(pid)) |
| 89 | }) |
| 90 | .and_then(|process| process.get("process")) |
| 91 | .and_then(Value::as_str) |
| 92 | .unwrap_or("unknown"); |
| 93 | let cpu = current |
| 94 | .get("cpuPercent") |
| 95 | .and_then(Value::as_f64) |
| 96 | .map(|value| format!("{value:.1}%")) |
| 97 | .unwrap_or_else(|| "--".to_owned()); |
| 98 | let memory = current |
| 99 | .get("memoryFootprintBytes") |
| 100 | .or_else(|| current.get("memoryResidentBytes")) |
| 101 | .and_then(Value::as_u64) |
| 102 | .map(format_bytes_cli) |
| 103 | .unwrap_or_else(|| "--".to_owned()); |
| 104 | let disk = current |
| 105 | .get("diskWriteBytesPerSecond") |
| 106 | .and_then(Value::as_f64) |
| 107 | .map(|value| format!("{}/s", format_bytes_cli(value.max(0.0) as u64))) |
| 108 | .unwrap_or_else(|| "--".to_owned()); |
| 109 | let network_in = current |
| 110 | .get("networkReceivedBytesPerSecond") |
| 111 | .and_then(Value::as_f64) |
| 112 | .map(|value| format!("{}/s", format_bytes_cli(value.max(0.0) as u64))) |
| 113 | .unwrap_or_else(|| "--".to_owned()); |
| 114 | let network_out = current |
| 115 | .get("networkSentBytesPerSecond") |
| 116 | .and_then(Value::as_f64) |
| 117 | .map(|value| format!("{}/s", format_bytes_cli(value.max(0.0) as u64))) |
| 118 | .unwrap_or_else(|| "--".to_owned()); |
| 119 | let connections = current |
| 120 | .get("networkConnectionCount") |
| 121 | .and_then(Value::as_u64) |
| 122 | .map(|value| value.to_string()) |
| 123 | .unwrap_or_else(|| "--".to_owned()); |
| 124 | let hang = current |
| 125 | .get("hang") |
| 126 | .and_then(|hang| hang.get("state")) |
| 127 | .and_then(Value::as_str) |
| 128 | .unwrap_or("unknown"); |
| 129 | println!( |
| 130 | "{} pid={} process={} cpu={} memory={} diskWrite={} netIn={} netOut={} connections={} hang={}", |
| 131 | chrono_like_time_label(), |
| 132 | pid, |
| 133 | process, |