rrd: simplify code
This commit is contained in:
parent
a4a3f7ca5e
commit
803ab12ad4
@ -27,7 +27,7 @@ fn get_node_stats(
|
|||||||
_param: Value,
|
_param: Value,
|
||||||
) -> Result<Value, Error> {
|
) -> Result<Value, Error> {
|
||||||
|
|
||||||
crate::rrd::extract_data_list(
|
crate::rrd::extract_data(
|
||||||
"host",
|
"host",
|
||||||
&["cpu", "iowait", "memtotal", "memused"],
|
&["cpu", "iowait", "memtotal", "memused"],
|
||||||
timeframe,
|
timeframe,
|
||||||
|
@ -5,7 +5,7 @@ use std::sync::{RwLock};
|
|||||||
|
|
||||||
use anyhow::{format_err, Error};
|
use anyhow::{format_err, Error};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use serde_json::Value;
|
use serde_json::{json, Value};
|
||||||
|
|
||||||
use proxmox::tools::fs::{create_path, CreateOptions};
|
use proxmox::tools::fs::{create_path, CreateOptions};
|
||||||
|
|
||||||
@ -67,24 +67,6 @@ pub fn update_value(rel_path: &str, value: f64) -> Result<(), Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn extract_data(
|
pub fn extract_data(
|
||||||
rel_path: &str,
|
|
||||||
timeframe: RRDTimeFrameResolution,
|
|
||||||
mode: RRDMode,
|
|
||||||
) -> Result<Value, Error> {
|
|
||||||
|
|
||||||
let now = now()?;
|
|
||||||
|
|
||||||
let map = RRD_CACHE.read().unwrap();
|
|
||||||
|
|
||||||
if let Some(rrd) = map.get(rel_path) {
|
|
||||||
Ok(rrd.extract_data(now, timeframe, mode))
|
|
||||||
} else {
|
|
||||||
Ok(RRD::new().extract_data(now, timeframe, mode))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn extract_data_list(
|
|
||||||
base: &str,
|
base: &str,
|
||||||
items: &[&str],
|
items: &[&str],
|
||||||
timeframe: RRDTimeFrameResolution,
|
timeframe: RRDTimeFrameResolution,
|
||||||
@ -95,17 +77,29 @@ pub fn extract_data_list(
|
|||||||
|
|
||||||
let map = RRD_CACHE.read().unwrap();
|
let map = RRD_CACHE.read().unwrap();
|
||||||
|
|
||||||
let mut list: Vec<(&str, &RRD)> = Vec::new();
|
|
||||||
|
|
||||||
let empty_rrd = RRD::new();
|
let empty_rrd = RRD::new();
|
||||||
|
|
||||||
|
let mut result = Vec::new();
|
||||||
|
|
||||||
for name in items.iter() {
|
for name in items.iter() {
|
||||||
if let Some(rrd) = map.get(&format!("{}/{}", base, name)) {
|
let rrd = map.get(&format!("{}/{}", base, name)).unwrap_or(&empty_rrd);
|
||||||
list.push((name, rrd));
|
let (start, reso, list) = rrd.extract_data(now, timeframe, mode);
|
||||||
} else {
|
let mut t = start;
|
||||||
list.push((name, &empty_rrd));
|
for index in 0..RRD_DATA_ENTRIES {
|
||||||
|
if result.len() <= index {
|
||||||
|
if let Some(value) = list[index] {
|
||||||
|
result.push(json!({ "time": t, *name: value }));
|
||||||
|
} else {
|
||||||
|
result.push(json!({ "time": t }));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if let Some(value) = list[index] {
|
||||||
|
result[index][name] = value.into();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t += reso;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(extract_rrd_data(&list, now, timeframe, mode))
|
Ok(result.into())
|
||||||
}
|
}
|
||||||
|
@ -45,11 +45,11 @@ impl RRD {
|
|||||||
epoch: u64,
|
epoch: u64,
|
||||||
timeframe: RRDTimeFrameResolution,
|
timeframe: RRDTimeFrameResolution,
|
||||||
mode: RRDMode,
|
mode: RRDMode,
|
||||||
) -> Value {
|
) -> (u64, u64, Vec<Option<f64>>) {
|
||||||
|
|
||||||
let reso = timeframe as u64;
|
let reso = timeframe as u64;
|
||||||
|
|
||||||
let end = reso*(epoch/reso);
|
let end = reso*((epoch + reso -1)/reso);
|
||||||
let start = end - reso*(RRD_DATA_ENTRIES as u64);
|
let start = end - reso*(RRD_DATA_ENTRIES as u64);
|
||||||
|
|
||||||
let rrd_end = reso*(self.last_update/reso);
|
let rrd_end = reso*(self.last_update/reso);
|
||||||
@ -69,23 +69,23 @@ impl RRD {
|
|||||||
let mut index = ((t/reso) % (RRD_DATA_ENTRIES as u64)) as usize;
|
let mut index = ((t/reso) % (RRD_DATA_ENTRIES as u64)) as usize;
|
||||||
for _ in 0..RRD_DATA_ENTRIES {
|
for _ in 0..RRD_DATA_ENTRIES {
|
||||||
if t < rrd_start || t > rrd_end {
|
if t < rrd_start || t > rrd_end {
|
||||||
list.push(json!({ "time": t }));
|
list.push(None);
|
||||||
} else {
|
} else {
|
||||||
let entry = data[index];
|
let entry = data[index];
|
||||||
if entry.count == 0 {
|
if entry.count == 0 {
|
||||||
list.push(json!({ "time": t }));
|
list.push(None);
|
||||||
} else {
|
} else {
|
||||||
let value = match mode {
|
let value = match mode {
|
||||||
RRDMode::Max => entry.max,
|
RRDMode::Max => entry.max,
|
||||||
RRDMode::Average => entry.average,
|
RRDMode::Average => entry.average,
|
||||||
};
|
};
|
||||||
list.push(json!({ "time": t, "value": value }));
|
list.push(Some(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
t += reso; index = (index + 1) % RRD_DATA_ENTRIES;
|
t += reso; index = (index + 1) % RRD_DATA_ENTRIES;
|
||||||
}
|
}
|
||||||
|
|
||||||
list.into()
|
(start, reso, list.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_raw(mut raw: &[u8]) -> Result<Self, Error> {
|
pub fn from_raw(mut raw: &[u8]) -> Result<Self, Error> {
|
||||||
@ -208,54 +208,3 @@ impl RRD {
|
|||||||
self.last_update = epoch;
|
self.last_update = epoch;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn extract_rrd_data(
|
|
||||||
rrd_list: &[(&str, &RRD)],
|
|
||||||
epoch: u64,
|
|
||||||
timeframe: RRDTimeFrameResolution,
|
|
||||||
mode: RRDMode,
|
|
||||||
) -> Value {
|
|
||||||
|
|
||||||
let reso = timeframe as u64;
|
|
||||||
|
|
||||||
let end = reso*(epoch/reso);
|
|
||||||
let start = end - reso*(RRD_DATA_ENTRIES as u64);
|
|
||||||
|
|
||||||
let mut list = Vec::new();
|
|
||||||
|
|
||||||
let mut t = start;
|
|
||||||
let mut index = ((t/reso) % (RRD_DATA_ENTRIES as u64)) as usize;
|
|
||||||
for _ in 0..RRD_DATA_ENTRIES {
|
|
||||||
let mut item = json!({ "time": t });
|
|
||||||
for (name, rrd) in rrd_list.iter() {
|
|
||||||
let rrd_end = reso*(rrd.last_update/reso);
|
|
||||||
let rrd_start = rrd_end - reso*(RRD_DATA_ENTRIES as u64);
|
|
||||||
|
|
||||||
if t < rrd_start || t > rrd_end {
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
let data = match timeframe {
|
|
||||||
RRDTimeFrameResolution::Hour => &rrd.hour,
|
|
||||||
RRDTimeFrameResolution::Day => &rrd.day,
|
|
||||||
RRDTimeFrameResolution::Week => &rrd.week,
|
|
||||||
RRDTimeFrameResolution::Month => &rrd.month,
|
|
||||||
RRDTimeFrameResolution::Year => &rrd.year,
|
|
||||||
};
|
|
||||||
let entry = data[index];
|
|
||||||
if entry.count == 0 {
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
let value = match mode {
|
|
||||||
RRDMode::Max => entry.max,
|
|
||||||
RRDMode::Average => entry.average,
|
|
||||||
};
|
|
||||||
item[name] = value.into();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
list.push(item);
|
|
||||||
t += reso; index = (index + 1) % RRD_DATA_ENTRIES;
|
|
||||||
}
|
|
||||||
|
|
||||||
list.into()
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user