summaryrefslogtreecommitdiffstats
path: root/src/app/data_collection/disks.rs
blob: ad897aed6de0bcba9e5ddf1208636252a7519c9b (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
78
79
80
81
82
83
84
use heim_common::prelude::StreamExt;
use std::time::Instant;

#[derive(Clone, Default)]
pub struct DiskData {
	pub name : Box<str>,
	pub mount_point : Box<str>,
	pub free_space : u64,
	pub used_space : u64,
	pub total_space : u64,
}

#[derive(Clone)]
pub struct IOData {
	pub mount_point : Box<str>,
	pub read_bytes : u64,
	pub write_bytes : u64,
}

#[derive(Clone)]
pub struct IOPackage {
	pub io_list : Vec<IOData>,
	pub instant : Instant,
}

pub async fn get_io_usage_list(get_physical : bool) -> Result<IOPackage, heim::Error> {
	let mut io_list : Vec<IOData> = Vec::new();
	if get_physical {
		let mut physical_counter_stream = heim::disk::io_counters_physical();
		while let Some(io) = physical_counter_stream.next().await {
			let io = io?;
			io_list.push(IOData {
				mount_point : Box::from(io.device_name().to_str().unwrap_or("Name Unavailable")),
				read_bytes : io.read_bytes().get::<heim_common::units::information::megabyte>(),
				write_bytes : io.write_bytes().get::<heim_common::units::information::megabyte>(),
			})
		}
	}
	else {
		let mut counter_stream = heim::disk::io_counters();
		while let Some(io) = counter_stream.next().await {
			let io = io?;
			io_list.push(IOData {
				mount_point : Box::from(io.device_name().to_str().unwrap_or("Name Unavailable")),
				read_bytes : io.read_bytes().get::<heim_common::units::information::megabyte>(),
				write_bytes : io.write_bytes().get::<heim_common::units::information::megabyte>(),
			})
		}
	}

	Ok(IOPackage { io_list, instant : Instant::now() })
}

pub async fn get_disk_usage_list() -> Result<Vec<DiskData>, heim::Error> {
	let mut vec_disks : Vec<DiskData> = Vec::new();
	let mut partitions_stream = heim::disk::partitions_physical();

	while let Some(part) = partitions_stream.next().await {
		let partition = part?; // TODO: Change this?  We don't want to error out immediately...
		let usage = heim::disk::usage(partition.mount_point().to_path_buf()).await?;

		vec_disks.push(DiskData {
			free_space : usage.free().get::<heim_common::units::information::megabyte>(),
			used_space : usage.used().get::<heim_common::units::information::megabyte>(),
			total_space : usage.total().get::<heim_common::units::information::megabyte>(),
			mount_point : Box::from(partition.mount_point().to_str().unwrap_or("Name Unavailable")),
			name : Box::from(partition.device().unwrap_or_else(|| std::ffi::OsStr::new("Name Unavailable")).to_str().unwrap_or("Name Unavailable")),
		});
	}

	vec_disks.sort_by(|a, b| {
		if a.name < b.name {
			std::cmp::Ordering::Less
		}
		else if a.name > b.name {
			std::cmp::Ordering::Greater
		}
		else {
			std::cmp::Ordering::Equal
		}
	});

	Ok(vec_disks)
}