summaryrefslogtreecommitdiffstats
path: root/src/convert_data.rs
blob: d3bb3147b6b79eac21d364c12e7bb9aec19207a0 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use crate::{app::data_collection, constants};
use constants::*;

pub fn update_temp_row(app_data : &data_collection::Data, temp_type : &data_collection::temperature::TemperatureType) -> Vec<Vec<String>> {
	let mut sensor_vector : Vec<Vec<String>> = Vec::new();

	if (&app_data.list_of_temperature_sensor).is_empty() {
		sensor_vector.push(vec!["None Found".to_string(), "".to_string()])
	}
	else {
		for sensor in &app_data.list_of_temperature_sensor {
			sensor_vector.push(vec![
				sensor.component_name.to_string(),
				(sensor.temperature.ceil() as u64).to_string()
					+ match temp_type {
						data_collection::temperature::TemperatureType::Celsius => "C",
						data_collection::temperature::TemperatureType::Kelvin => "K",
						data_collection::temperature::TemperatureType::Fahrenheit => "F",
					},
			]);
		}
	}

	sensor_vector
}

pub fn update_disk_row(app_data : &data_collection::Data) -> Vec<Vec<String>> {
	let mut disk_vector : Vec<Vec<String>> = Vec::new();
	for disk in &app_data.list_of_disks {
		let io_activity = if app_data.list_of_io.len() > 2 {
			let io_package = &app_data.list_of_io.last().unwrap();
			let prev_io_package = &app_data.list_of_io[app_data.list_of_io.len() - 2];

			let io_hashmap = &io_package.io_hash;
			let prev_io_hashmap = &prev_io_package.io_hash;
			let trimmed_mount = &disk.name.to_string().split('/').last().unwrap().to_string();
			let time_difference = io_package.instant.duration_since(prev_io_package.instant).as_secs_f64();
			if io_hashmap.contains_key(trimmed_mount) && prev_io_hashmap.contains_key(trimmed_mount) {
				// Ideally change this...
				let ele = &io_hashmap[trimmed_mount];
				let prev = &prev_io_hashmap[trimmed_mount];
				let read_bytes_per_sec = ((ele.read_bytes - prev.read_bytes) as f64 / time_difference) as u64;
				let write_bytes_per_sec = ((ele.write_bytes - prev.write_bytes) as f64 / time_difference) as u64;
				(
					if read_bytes_per_sec < 1024 {
						format!("{}B", read_bytes_per_sec)
					}
					else if read_bytes_per_sec < 1024 * 1024 {
						format!("{}KB", read_bytes_per_sec / 1024)
					}
					else {
						format!("{}MB", read_bytes_per_sec / 1024 / 1024)
					},
					if write_bytes_per_sec < 1024 {
						format!("{}B", write_bytes_per_sec)
					}
					else if write_bytes_per_sec < 1024 * 1024 {
						format!("{}KB", write_bytes_per_sec / 1024)
					}
					else {
						format!("{}MB", write_bytes_per_sec / 1024 / 1024)
					},
				)
			}
			else {
				("0B".to_string(), "0B".to_string())
			}
		}
		else {
			("0B".to_string(), "0B".to_string())
		};
		disk_vector.push(vec![
			disk.name.to_string(),
			disk.mount_point.to_string(),
			format!("{:.0}%", disk.used_space as f64 / disk.total_space as f64 * 100_f64),
			if disk.free_space < 1024 {
				disk.free_space.to_string() + "MB"
			}
			else {
				(disk.free_space / 1024).to_string() + "GB"
			},
			if disk.total_space < 1024 {
				disk.total_space.to_string() + "MB"
			}
			else {
				(disk.total_space / 1024).to_string() + "GB"
			},
			io_activity.0,
			io_activity.1,
		]);
	}

	disk_vector
}

pub fn update_process_row(app_data : &data_collection::Data) -> Vec<Vec<String>> {
	let mut process_vector : Vec<Vec<String>> = Vec::new();

	for process in &app_data.list_of_processes {
		process_vector.push(vec![
			process.pid.to_string(),
			process.command.to_string(),
			format!("{:.1}%", process.cpu_usage_percent),
			format!(
				"{:.1}%",
				if let Some(mem_usage) = process.mem_usage_percent {
					mem_usage
				}
				else if let Some(mem_usage_kb) = process.mem_usage_kb {
					if let Some(mem_data) = app_data.memory.last() {
						(mem_usage_kb / 1024) as f64 / mem_data.mem_total_in_mb as f64 * 100_f64
					}
					else {
						0_f64
					}
				}
				else {
					0_f64
				}
			),
		]);
	}

	process_vector
}

pub fn update_cpu_data_points(show_avg_cpu : bool, app_data : &data_collection::Data) -> Vec<(String, Vec<(f64, f64)>)> {
	let mut cpu_data_vector : Vec<(String, Vec<(f64, f64)>)> = Vec::new();
	let mut cpu_collection : Vec<Vec<(f64, f64)>> = Vec::new();

	if !app_data.list_of_cpu_packages.is_empty() {
		// I'm sorry for the if statement but I couldn't be bothered here...
		for cpu_num in (if show_avg_cpu { 0 } else { 1 })..app_data.list_of_cpu_packages.last().unwrap().cpu_vec.len() {
			let mut this_cpu_data : Vec<(f64, f64)> = Vec::new();

			for data in &app_data.list_of_cpu_packages {
				let current_time = std::time::Instant::now();
				let current_cpu_usage = data.cpu_vec[cpu_num].cpu_usage;

				let new_entry = (
					((STALE_MAX_MILLISECONDS as f64 - current_time.duration_since(data.instant).as_millis() as f64) * 10_f64).floor(),
					current_cpu_usage,
				);

				// Now, inject our joining points...
				if !this_cpu_data.is_empty() {
					let previous_element_data = *(this_cpu_data.last().unwrap());
					for idx in 0..50 {
						this_cpu_data.push((
							previous_element_data.0 + ((new_entry.0 - previous_element_data.0) / 50.0 * f64::from(idx)),
							previous_element_data.1 + ((new_entry.1 - previous_element_data.1) / 50.0 * f64::from(idx)),
						));
					}
				}

				this_cpu_data.push(new_entry);
			}

			cpu_collection.push(this_cpu_data);
		}

		// Finally, add it all onto the end
		for (i, data) in cpu_collection.iter().enumerate() {
			cpu_data_vector.push((
				// + 1 to skip total CPU if show_avg_cpu is false
				format!(
					"{:4}: ",
					&*(app_data.list_of_cpu_packages.last().unwrap().cpu_vec[i + if show_avg_cpu { 0 } else { 1 }].cpu_name)
				)
				.to_uppercase() + &format!("{:3}%", (data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64)),
				data.clone(),
			))
		}
	}

	cpu_data_vector
}

pub fn update_mem_data_points(app_data : &data_collection::Data) -> Vec<(f64, f64)> {
	convert_mem_data(&app_data.memory)
}

pub fn update_swap_data_points(app_data : &data_collection::Data) -> Vec<(f64, f64)> {
	convert_mem_data(&app_data.swap)
}

pub fn convert_mem_data(mem_data : &[data_collection::mem::MemData]) -> Vec<(f64, f64)> {
	let mut result : Vec<(f64, f64)> = Vec::new();

	for data in mem_data {
		let current_time = std::time::Instant::now();
		let new_entry = (
			((STALE_MAX_MILLISECONDS