getting data programmatically (#56)
* getting loadavg programmatically * added missing uptime in rsblock.yml * using clock_gettime syscall to get uptime
This commit is contained in:
parent
674f6811fe
commit
b1d81bf8c9
13
rsblocks.yml
13
rsblocks.yml
@ -11,6 +11,13 @@ time:
|
||||
icon: ''
|
||||
|
||||
|
||||
# Time of the system has been running
|
||||
uptime:
|
||||
delay: 15.0
|
||||
enabled: false
|
||||
icon: ''
|
||||
|
||||
|
||||
# public ip address
|
||||
pub_ip:
|
||||
delay: 120.0
|
||||
@ -18,7 +25,7 @@ pub_ip:
|
||||
icon: ''
|
||||
|
||||
|
||||
# ethernet/wifi bandwith
|
||||
# ethernet/wifi bandwith (no delay for this since it will calculate the bandwith every second)
|
||||
netspeed:
|
||||
enabled: false
|
||||
interface: 'wlan0'
|
||||
@ -50,9 +57,9 @@ cpu_temperature:
|
||||
icon: ''
|
||||
|
||||
|
||||
# cpu load average
|
||||
# cpu load average from the last minute
|
||||
loadavg:
|
||||
delay: 2.0
|
||||
delay: 15.0
|
||||
enabled: false
|
||||
icon: ''
|
||||
|
||||
|
@ -1,12 +1,16 @@
|
||||
use crate::config::CONFIG;
|
||||
use crate::types::ThreadsData;
|
||||
use nix::sys::sysinfo;
|
||||
|
||||
use nix::libc::{c_double, c_int, getloadavg};
|
||||
|
||||
pub fn get_load_avg() -> ThreadsData {
|
||||
let load = sysinfo::sysinfo().unwrap().load_average().0;
|
||||
let mut data: [c_double; 3] = [0f64; 3];
|
||||
unsafe { getloadavg(data.as_mut_ptr(), data.len() as c_int) };
|
||||
let [load, _, _] = data;
|
||||
let data = format!(
|
||||
" {} {:.2} {}",
|
||||
CONFIG.loadavg.icon, load, CONFIG.seperator
|
||||
);
|
||||
|
||||
ThreadsData::LoadAvg(data)
|
||||
}
|
||||
|
@ -1,21 +1,33 @@
|
||||
use crate::config::CONFIG;
|
||||
use crate::types::ThreadsData;
|
||||
use chrono::Duration;
|
||||
use nix::sys::sysinfo;
|
||||
use nix::time::clock_gettime;
|
||||
|
||||
pub fn get_uptime() -> ThreadsData {
|
||||
let duration = sysinfo::sysinfo().unwrap().uptime();
|
||||
let uptime_sec = Duration::from_std(duration).unwrap().num_seconds();
|
||||
|
||||
let hour = uptime_sec / 3600;
|
||||
let rem = uptime_sec - hour * 3600;
|
||||
let minutes = rem / 60;
|
||||
|
||||
let (_, hour, minutes, seconds) = get_uptime_data();
|
||||
let uptime = if hour > 0 {
|
||||
format!("{}:{}", hour, minutes)
|
||||
format!("{}:{}:{}", hour, minutes, seconds)
|
||||
} else {
|
||||
format!("{} min", minutes)
|
||||
format!("{} min, {} sec", minutes, seconds)
|
||||
};
|
||||
let result = format!(" {} {} {}", CONFIG.uptime.icon, uptime, CONFIG.seperator);
|
||||
ThreadsData::Uptime(result)
|
||||
}
|
||||
|
||||
// This helper function will use the system call clock_gettime
|
||||
// it will return a tuple of (days, hours, minutes, seconds)
|
||||
fn get_uptime_data() -> (i64, i64, i64, i64) {
|
||||
let mut uptime = clock_gettime(nix::time::ClockId::CLOCK_MONOTONIC)
|
||||
.unwrap()
|
||||
.tv_sec();
|
||||
|
||||
if uptime > 60 {
|
||||
uptime += 30;
|
||||
}
|
||||
let days = uptime / 86400;
|
||||
uptime %= 86400;
|
||||
let hours = uptime / 3600;
|
||||
uptime %= 3600;
|
||||
let minutes = uptime / 60;
|
||||
let seconds = uptime % 60;
|
||||
(days, hours, minutes, seconds)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user