aboutsummaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/api')
-rw-r--r--src/api/routes/stats/mod.rs25
-rw-r--r--src/api/routes/stats/structures.rs16
2 files changed, 38 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,
}