From 7bf2ff0b4deee962cf4290b52c081a62bfdcb0d1 Mon Sep 17 00:00:00 2001 From: toast Date: Tue, 12 Mar 2024 14:24:44 +1100 Subject: [PATCH] Add utils and memory --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/commands/uptime.rs | 43 ++++++++++++++++++++---------------------- src/main.rs | 1 + src/utils.rs | 37 ++++++++++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 25 deletions(-) create mode 100644 src/utils.rs diff --git a/Cargo.lock b/Cargo.lock index fbeb000..f913f39 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -833,7 +833,7 @@ dependencies = [ [[package]] name = "kon" -version = "0.1.16" +version = "0.1.18" dependencies = [ "cargo_toml", "gamedig", diff --git a/Cargo.toml b/Cargo.toml index 360266a..9792704 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kon" -version = "0.1.16" +version = "0.1.18" rust-version = "1.74" edition = "2021" diff --git a/src/commands/uptime.rs b/src/commands/uptime.rs index a321bd0..3773455 100644 --- a/src/commands/uptime.rs +++ b/src/commands/uptime.rs @@ -1,4 +1,11 @@ -use crate::Error; +use crate::{ + Error, + utils::{ + format_duration, + format_memory, + concat_message + } +}; use sysinfo::System; use uptime_lib::get; @@ -14,6 +21,11 @@ pub async fn uptime(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> { let mut sys = System::new_all(); sys.refresh_all(); + // Fetch system's memory usage + let memory_used = System::used_memory(&sys); + let memory_free = System::free_memory(&sys); + let memory_total = System::total_memory(&sys); + // Fetch system's uptime let sys_uptime = get().unwrap().as_secs(); @@ -26,27 +38,12 @@ pub async fn uptime(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> { proc_uptime = now.duration_since(time_started).unwrap().as_secs(); } - ctx.reply(format!("System: `{}`\nProcess: `{}`", format_duration(sys_uptime), format_duration(proc_uptime))).await?; + let stat_msg = vec![ + format!("System: `{}`", format_duration(sys_uptime)), + format!("Process: `{}`", format_duration(proc_uptime)), + format!("Memory: `{} / {} / {}`", format_memory(memory_free), format_memory(memory_used), format_memory(memory_total)) + ]; + ctx.reply(concat_message(stat_msg)).await?; + Ok(()) } - -fn format_duration(secs: u64) -> String { - let days = secs / 86400; - let hours = (secs % 86400) / 3600; - let minutes = (secs % 3600) / 60; - let seconds = secs % 60; - - let mut formatted_string = String::new(); - if days > 0 { - formatted_string.push_str(&format!("{}d, ", days)); - } - if hours > 0 || days > 0 { - formatted_string.push_str(&format!("{}h, ", hours)); - } - if minutes > 0 || hours > 0 { - formatted_string.push_str(&format!("{}m, ", minutes)); - } - formatted_string.push_str(&format!("{}s", seconds)); - - formatted_string -} diff --git a/src/main.rs b/src/main.rs index f7b64f4..14b42a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod commands; mod controllers; mod models; +mod utils; use poise::serenity_prelude::{self as serenity}; use std::{ diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..6dfb32e --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,37 @@ +pub fn concat_message(messages: Vec) -> String { + messages.join("\n") +} + +pub fn format_duration(secs: u64) -> String { + let days = secs / 86400; + let hours = (secs % 86400) / 3600; + let minutes = (secs % 3600) / 60; + let seconds = secs % 60; + + let mut formatted_string = String::new(); + if days > 0 { + formatted_string.push_str(&format!("{}d, ", days)); + } + if hours > 0 || days > 0 { + formatted_string.push_str(&format!("{}h, ", hours)); + } + if minutes > 0 || hours > 0 { + formatted_string.push_str(&format!("{}m, ", minutes)); + } + formatted_string.push_str(&format!("{}s", seconds)); + + formatted_string +} + +pub fn format_memory(bytes: u64) -> String { + let kb = 1024; + let mb = 1024 * 1024; + let gb = 1024 * 1024 * 1024; + + match bytes { + b if b >= gb => format!("{:.0} GB", (b as f64 / (1024.0 * 1024.0 * 1024.0)).ceil()), + b if b >= mb => format!("{:.0} MB", (b as f64 / (1024.0 * 1024.0)).ceil()), + b if b >= kb => format!("{:.0} KB", (b as f64 / 1024.0).ceil()), + _ => format!("{:.0} B", bytes), + } +}