api2/status: use new rrd::extract_cached_data

and drop the now unused extract_lists function

this also fixes a bug, where we did not add the datastore to the list at
all when there was no rrd data

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Dominik Csapak 2020-06-10 12:02:58 +02:00 committed by Wolfgang Bumiller
parent 431cc7b185
commit ec8f042459
2 changed files with 50 additions and 75 deletions

View File

@ -8,6 +8,7 @@ use proxmox::api::{api, ApiMethod, Router, RpcEnvironment, UserInformation, Subd
use crate::api2::types::{DATASTORE_SCHEMA, RRDMode, RRDTimeFrameResolution}; use crate::api2::types::{DATASTORE_SCHEMA, RRDMode, RRDTimeFrameResolution};
use crate::backup::{DataStore}; use crate::backup::{DataStore};
use crate::config::datastore; use crate::config::datastore;
use crate::tools::epoch_now_f64;
use crate::tools::statistics::{linear_regression}; use crate::tools::statistics::{linear_regression};
use crate::config::cached_user_info::CachedUserInfo; use crate::config::cached_user_info::CachedUserInfo;
use crate::config::acl::{ use crate::config::acl::{
@ -90,59 +91,70 @@ fn datastore_status(
}); });
let rrd_dir = format!("datastore/{}", store); let rrd_dir = format!("datastore/{}", store);
let now = epoch_now_f64()?;
let rrd_resolution = RRDTimeFrameResolution::Month;
let rrd_mode = RRDMode::Average;
let (times, lists) = crate::rrd::extract_lists( let total_res = crate::rrd::extract_cached_data(
&rrd_dir, &rrd_dir,
&[ "total", "used", ], "total",
RRDTimeFrameResolution::Month, now,
RRDMode::Average, rrd_resolution,
)?; rrd_mode,
);
if !lists.contains_key("total") || !lists.contains_key("used") { let used_res = crate::rrd::extract_cached_data(
// we do not have the info, so we can skip calculating &rrd_dir,
continue; "used",
} now,
rrd_resolution,
rrd_mode,
);
let mut usage_list: Vec<f64> = Vec::new(); match (total_res, used_res) {
let mut time_list: Vec<u64> = Vec::new(); (Some((start, reso, total_list)), Some((_, _, used_list))) => {
let mut history = Vec::new(); let mut usage_list: Vec<f64> = Vec::new();
let mut time_list: Vec<u64> = Vec::new();
let mut history = Vec::new();
for (idx, used) in lists["used"].iter().enumerate() { for (idx, used) in used_list.iter().enumerate() {
let total = if idx < lists["total"].len() { let total = if idx < total_list.len() {
lists["total"][idx] total_list[idx]
} else { } else {
None None
}; };
match (total, used) { match (total, used) {
(Some(total), Some(used)) if total != 0.0 => { (Some(total), Some(used)) if total != 0.0 => {
time_list.push(times[idx]); time_list.push(start + (idx as u64)*reso);
let usage = used/total; let usage = used/total;
usage_list.push(usage); usage_list.push(usage);
history.push(json!(usage)); history.push(json!(usage));
}, },
_ => { _ => {
history.push(json!(null)) history.push(json!(null))
}
}
} }
}
}
entry["history"] = history.into(); entry["history"] = history.into();
// we skip the calculation for datastores with not enough data // we skip the calculation for datastores with not enough data
if usage_list.len() >= 7 { if usage_list.len() >= 7 {
if let Some((a,b)) = linear_regression(&time_list, &usage_list) { if let Some((a,b)) = linear_regression(&time_list, &usage_list) {
if b != 0.0 { if b != 0.0 {
let estimate = (1.0 - a) / b; let estimate = (1.0 - a) / b;
entry["estimated-full-date"] = Value::from(estimate.floor() as u64); entry["estimated-full-date"] = Value::from(estimate.floor() as u64);
}
}
} }
} },
_ => {},
} }
list.push(entry); list.push(entry);
} }
Ok(list.into()) Ok(list.into())
} }

View File

@ -65,43 +65,6 @@ pub fn update_value(rel_path: &str, value: f64, dst: DST, save: bool) -> Result<
Ok(()) Ok(())
} }
/// extracts the lists of the given items and a list of timestamps
pub fn extract_lists(
base: &str,
items: &[&str],
timeframe: RRDTimeFrameResolution,
mode: RRDMode,
) -> Result<(Vec<u64>, HashMap<String, Vec<Option<f64>>>), Error> {
let now = now()?;
let map = RRD_CACHE.read().unwrap();
let mut result = HashMap::new();
let mut times = Vec::new();
for name in items.iter() {
let rrd = match map.get(&format!("{}/{}", base, name)) {
Some(rrd) => rrd,
None => continue,
};
let (start, reso, list) = rrd.extract_data(now, timeframe, mode);
result.insert(name.to_string(), list);
if times.len() == 0 {
let mut t = start;
for _ in 0..RRD_DATA_ENTRIES {
times.push(t);
t += reso;
}
}
}
Ok((times, result))
}
pub fn extract_cached_data( pub fn extract_cached_data(
base: &str, base: &str,
name: &str, name: &str,