Add utils and memory
All checks were successful
Build and push container image / build (push) Successful in 4m23s

This commit is contained in:
toast 2024-03-12 14:24:44 +11:00
parent fbca39ed5d
commit 7bf2ff0b4d
5 changed files with 60 additions and 25 deletions

2
Cargo.lock generated
View File

@ -833,7 +833,7 @@ dependencies = [
[[package]] [[package]]
name = "kon" name = "kon"
version = "0.1.16" version = "0.1.18"
dependencies = [ dependencies = [
"cargo_toml", "cargo_toml",
"gamedig", "gamedig",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "kon" name = "kon"
version = "0.1.16" version = "0.1.18"
rust-version = "1.74" rust-version = "1.74"
edition = "2021" edition = "2021"

View File

@ -1,4 +1,11 @@
use crate::Error; use crate::{
Error,
utils::{
format_duration,
format_memory,
concat_message
}
};
use sysinfo::System; use sysinfo::System;
use uptime_lib::get; use uptime_lib::get;
@ -14,6 +21,11 @@ pub async fn uptime(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
let mut sys = System::new_all(); let mut sys = System::new_all();
sys.refresh_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 // Fetch system's uptime
let sys_uptime = get().unwrap().as_secs(); 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(); 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(()) 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
}

View File

@ -1,6 +1,7 @@
mod commands; mod commands;
mod controllers; mod controllers;
mod models; mod models;
mod utils;
use poise::serenity_prelude::{self as serenity}; use poise::serenity_prelude::{self as serenity};
use std::{ use std::{

37
src/utils.rs Normal file
View File

@ -0,0 +1,37 @@
pub fn concat_message(messages: Vec<String>) -> 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),
}
}