Skip to main content

sysd_manager_comcontroler/
analyze.rs

1use super::SystemdErrors;
2
3#[derive(Clone, Debug)]
4pub struct Analyze {
5    pub time: u32,
6    pub service: String,
7}
8
9/// Returns the results of `systemd-analyze blame`
10pub async fn blame() -> Result<Vec<Analyze>, SystemdErrors> {
11    let cmd = ["systemd-analyze", "blame"];
12    let command_output = super::commander_output(&cmd, None)?.stdout;
13
14    let collection = String::from_utf8(command_output)
15        .expect("from_utf8 failed")
16        .lines()
17        .rev()
18        .map(|x| {
19            let mut iterator = x.split_whitespace();
20            Analyze {
21                time: parse_time(iterator.next().unwrap()),
22                service: String::from(iterator.next().unwrap()),
23            }
24        })
25        .collect::<Vec<Analyze>>();
26
27    Ok(collection)
28}
29
30fn parse_time(input: &str) -> u32 {
31    if input.ends_with("ms") {
32        input[0..input.len() - 2].parse::<u32>().unwrap_or(0)
33    } else if input.ends_with('s') {
34        (input[0..input.len() - 1].parse::<f32>().unwrap_or(0f32) * 1000f32) as u32
35    } else if input.ends_with("min") {
36        input[0..input.len() - 3].parse::<u32>().unwrap_or(0) * 3600000
37    } else {
38        0u32
39    }
40}