summaryrefslogtreecommitdiffstats
path: root/src/data_collection/disks/windows.rs
blob: ae7bb025ce3b60901349a0b4dc568238988c0bfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! Disk stats via sysinfo.

use itertools::Itertools;
use sysinfo::{DiskExt, SystemExt};

use super::{keep_disk_entry, DiskHarvest};

use crate::data_collection::{disks::IoCounters, DataCollector};

mod bindings;
use bindings::*;

/// Returns I/O stats.
pub(crate) fn io_stats() -> anyhow::Result<Vec<IoCounters>> {
    let volume_io = all_volume_io()?;

    Ok(volume_io
        .into_iter()
        .map_ok(|(performance, volume_name)| {
            let name = volume_name;
            let read_bytes = performance.BytesRead as u64;
            let write_bytes = performance.BytesWritten as u64;

            IoCounters::new(name, read_bytes, write_bytes)
        })
        .flatten()
        .collect::<Vec<_>>())
}

pub(crate) fn get_disk_usage(collector: &DataCollector) -> anyhow::Result<Vec<DiskHarvest>> {
    let disks = collector.sys.disks();
    let disk_filter = &collector.filters.disk_filter;
    let mount_filter = &collector.filters.mount_filter;

    Ok(disks
        .iter()
        .filter_map(|disk| {
            let name = {
                let name = disk.name();

                if name.is_empty() {
                    "No Name".to_string()
                } else {
                    name.to_os_string()
                        .into_string()
                        .unwrap_or_else(|_| "Name Unavailable".to_string())
                }
            };

            let mount_point = disk
                .mount_point()
                .as_os_str()
                .to_os_string()
                .into_string()
                .unwrap_or_else(|_| "Mount Unavailable".to_string());

            let volume_name = volume_name_from_mount(&mount_point).ok();

            if keep_disk_entry(&name, &mount_point, disk_filter, mount_filter) {
                let free_space = disk.available_space();
                let total_space = disk.total_space();
                let used_space = total_space - free_space;

                Some(DiskHarvest {
                    name,
                    mount_point,
                    volume_name,
                    free_space: Some(free_space),
                    used_space: Some(used_space),
                    total_space: Some(total_space),
                })
            } else {
                None
            }
        })
        .collect())
}