aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFuwn <[email protected]>2021-05-02 17:20:29 +0000
committerFuwn <[email protected]>2021-05-02 17:20:29 +0000
commit6f839a24e989353571c31870cae91c6aeabcb75b (patch)
tree7030b6b145e2d75d98222d14a59f491c622cfa01 /src
parentfeat(global): begin implementing api (diff)
downloadwhirl-6f839a24e989353571c31870cae91c6aeabcb75b.tar.xz
whirl-6f839a24e989353571c31870cae91c6aeabcb75b.zip
feat(api): flush out statistics endpoint
Diffstat (limited to 'src')
-rw-r--r--src/api/routes/stats/mod.rs25
-rw-r--r--src/api/routes/stats/structures.rs16
-rw-r--r--src/utils/mod.rs1
-rw-r--r--src/utils/system.rs38
4 files changed, 77 insertions, 3 deletions
diff --git a/src/api/routes/stats/mod.rs b/src/api/routes/stats/mod.rs
index a58e4b3..f7af6e8 100644
--- a/src/api/routes/stats/mod.rs
+++ b/src/api/routes/stats/mod.rs
@@ -4,12 +4,33 @@
pub mod structures;
use rocket_contrib::json::Json;
+use sysinfo::{get_current_pid, ProcessExt, System, SystemExt};
-use crate::api::routes::stats::structures::Statistics;
+use crate::{
+ api::routes::stats::structures::{Statistics, StatisticsProcess, StatisticsSystem},
+ utils::system::seconds_to_hrtime,
+};
+// This is mostly for developmental testing, it consumes more CPU than it's
+// worth.
#[get("/statistics")]
pub fn statistics() -> Json<Statistics> {
+ let mut sys = System::new_all();
+ sys.refresh_all();
+
+ let process = sys.get_process(get_current_pid().unwrap()).unwrap();
+
Json(Statistics {
- message: "todo".to_string(),
+ system: StatisticsSystem {
+ os_type: sys.get_name().unwrap(),
+ release: sys.get_kernel_version().unwrap(),
+ uptime: seconds_to_hrtime(sysinfo::System::new().get_uptime() as usize),
+ },
+ process: StatisticsProcess {
+ memory_usage: (process.memory() / 1000).to_string(),
+ // (process.cpu_usage() * 100.0).round() / 100.0
+ cpu_usage: (process.cpu_usage() / sys.get_processors().len() as f32).to_string(),
+ // uptime: seconds_to_hrtime((sys.get_uptime() - process.start_time()) as usize),
+ },
})
}
diff --git a/src/api/routes/stats/structures.rs b/src/api/routes/stats/structures.rs
index 9a4fe23..a141ce6 100644
--- a/src/api/routes/stats/structures.rs
+++ b/src/api/routes/stats/structures.rs
@@ -5,5 +5,19 @@ use serde_derive::Serialize;
#[derive(Serialize)]
pub struct Statistics {
- pub message: String,
+ pub system: StatisticsSystem,
+ pub process: StatisticsProcess,
+}
+#[derive(Serialize)]
+pub struct StatisticsSystem {
+ #[serde(rename = "type")]
+ pub os_type: String,
+ pub release: String,
+ pub uptime: String,
+}
+#[derive(Serialize)]
+pub struct StatisticsProcess {
+ pub memory_usage: String,
+ pub cpu_usage: String,
+ // pub uptime: String,
}
diff --git a/src/utils/mod.rs b/src/utils/mod.rs
index 98990d4..ce264a8 100644
--- a/src/utils/mod.rs
+++ b/src/utils/mod.rs
@@ -2,3 +2,4 @@
// SPDX-License-Identifier: GPL-3.0-only
pub mod db;
+pub mod system;
diff --git a/src/utils/system.rs b/src/utils/system.rs
new file mode 100644
index 0000000..7a823a0
--- /dev/null
+++ b/src/utils/system.rs
@@ -0,0 +1,38 @@
+// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective
+// SPDX-License-Identifier: GPL-3.0-only
+
+const WEEK: usize = 60 * 60 * 60 * 60;
+const DAY: usize = 60 * 60 * 60;
+const HOUR: usize = 60 * 60;
+const MIN: usize = 60;
+
+fn make_parts(t: usize, steps: &[usize], mut accum: Vec<usize>) -> Vec<usize> {
+ match steps.split_first() {
+ None => accum,
+ Some((s, steps)) => {
+ accum.push(t / *s);
+ make_parts(t % *s, steps, accum)
+ }
+ }
+}
+
+pub fn seconds_to_hrtime(seconds: usize) -> String {
+ let word = ["week", "day", "hour", "min", "sec"];
+
+ make_parts(seconds, &[WEEK, DAY, HOUR, MIN, 1], Vec::new())
+ .iter()
+ .enumerate()
+ .filter_map(|(i, s)| {
+ if s > &0 {
+ if s > &1 {
+ Some(format!("{} {}s", s, word[i]))
+ } else {
+ Some(format!("{} {}", s, word[i]))
+ }
+ } else {
+ None
+ }
+ })
+ .collect::<Vec<String>>()
+ .join(", ")
+}