aboutsummaryrefslogtreecommitdiff
path: root/crates/whirl_api/src/routes/stats
diff options
context:
space:
mode:
authorFuwn <[email protected]>2021-05-20 17:05:59 +0000
committerFuwn <[email protected]>2021-05-20 17:05:59 +0000
commit9dbc613765de8ab7dfa8e1374cf6661dcfd56bc8 (patch)
tree8cfff6a23bb72db2660e68c63a8cf9d0539a061f /crates/whirl_api/src/routes/stats
parentfeat(readme): add sqlfluff as a dev dep (diff)
downloadwhirl-9dbc613765de8ab7dfa8e1374cf6661dcfd56bc8.tar.xz
whirl-9dbc613765de8ab7dfa8e1374cf6661dcfd56bc8.zip
refactor(global): move crates around, stricter module isolation
Diffstat (limited to 'crates/whirl_api/src/routes/stats')
-rw-r--r--crates/whirl_api/src/routes/stats/mod.rs33
-rw-r--r--crates/whirl_api/src/routes/stats/structures.rs21
2 files changed, 54 insertions, 0 deletions
diff --git a/crates/whirl_api/src/routes/stats/mod.rs b/crates/whirl_api/src/routes/stats/mod.rs
new file mode 100644
index 0000000..d5d0937
--- /dev/null
+++ b/crates/whirl_api/src/routes/stats/mod.rs
@@ -0,0 +1,33 @@
+// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective
+// SPDX-License-Identifier: GPL-3.0-only
+
+pub mod structures;
+
+use actix_web::HttpResponse;
+use sysinfo::{get_current_pid, ProcessExt, System, SystemExt};
+use whirl_common::system::seconds_to_hrtime;
+
+use crate::routes::stats::structures::{Statistics, StatisticsProcess, StatisticsSystem};
+
+// This is mostly for developmental testing, it consumes more CPU than it's
+// worth.
+pub fn statistics() -> HttpResponse {
+ let mut sys = System::new_all();
+ sys.refresh_all();
+
+ let process = sys.get_process(get_current_pid().unwrap()).unwrap();
+
+ HttpResponse::Ok().json(Statistics {
+ 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 {
+ // (process.cpu_usage() * 100.0).round() / 100.0
+ memory_usage: (process.memory() / 1000).to_string(),
+ 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/crates/whirl_api/src/routes/stats/structures.rs b/crates/whirl_api/src/routes/stats/structures.rs
new file mode 100644
index 0000000..88fc852
--- /dev/null
+++ b/crates/whirl_api/src/routes/stats/structures.rs
@@ -0,0 +1,21 @@
+// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective
+// SPDX-License-Identifier: GPL-3.0-only
+
+#[derive(Serialize)]
+pub struct Statistics {
+ 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,
+}