From ec8f0424599cdc17bbcb462d445a0d80c9e434e0 Mon Sep 17 00:00:00 2001 From: Dominik Csapak Date: Wed, 10 Jun 2020 12:02:58 +0200 Subject: [PATCH] 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 Signed-off-by: Wolfgang Bumiller --- src/api2/status.rs | 88 ++++++++++++++++++++++++++-------------------- src/rrd/cache.rs | 37 ------------------- 2 files changed, 50 insertions(+), 75 deletions(-) diff --git a/src/api2/status.rs b/src/api2/status.rs index 5aca93fb..18bf30e1 100644 --- a/src/api2/status.rs +++ b/src/api2/status.rs @@ -8,6 +8,7 @@ use proxmox::api::{api, ApiMethod, Router, RpcEnvironment, UserInformation, Subd use crate::api2::types::{DATASTORE_SCHEMA, RRDMode, RRDTimeFrameResolution}; use crate::backup::{DataStore}; use crate::config::datastore; +use crate::tools::epoch_now_f64; use crate::tools::statistics::{linear_regression}; use crate::config::cached_user_info::CachedUserInfo; use crate::config::acl::{ @@ -90,59 +91,70 @@ fn datastore_status( }); 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, - &[ "total", "used", ], - RRDTimeFrameResolution::Month, - RRDMode::Average, - )?; + "total", + now, + rrd_resolution, + rrd_mode, + ); - if !lists.contains_key("total") || !lists.contains_key("used") { - // we do not have the info, so we can skip calculating - continue; - } + let used_res = crate::rrd::extract_cached_data( + &rrd_dir, + "used", + now, + rrd_resolution, + rrd_mode, + ); - let mut usage_list: Vec = Vec::new(); - let mut time_list: Vec = Vec::new(); - let mut history = Vec::new(); + match (total_res, used_res) { + (Some((start, reso, total_list)), Some((_, _, used_list))) => { + let mut usage_list: Vec = Vec::new(); + let mut time_list: Vec = Vec::new(); + let mut history = Vec::new(); - for (idx, used) in lists["used"].iter().enumerate() { - let total = if idx < lists["total"].len() { - lists["total"][idx] - } else { - None - }; + for (idx, used) in used_list.iter().enumerate() { + let total = if idx < total_list.len() { + total_list[idx] + } else { + None + }; - match (total, used) { - (Some(total), Some(used)) if total != 0.0 => { - time_list.push(times[idx]); - let usage = used/total; - usage_list.push(usage); - history.push(json!(usage)); - }, - _ => { - history.push(json!(null)) + match (total, used) { + (Some(total), Some(used)) if total != 0.0 => { + time_list.push(start + (idx as u64)*reso); + let usage = used/total; + usage_list.push(usage); + history.push(json!(usage)); + }, + _ => { + history.push(json!(null)) + } + } } - } - } - entry["history"] = history.into(); + entry["history"] = history.into(); - // we skip the calculation for datastores with not enough data - if usage_list.len() >= 7 { - if let Some((a,b)) = linear_regression(&time_list, &usage_list) { - if b != 0.0 { - let estimate = (1.0 - a) / b; - entry["estimated-full-date"] = Value::from(estimate.floor() as u64); + // we skip the calculation for datastores with not enough data + if usage_list.len() >= 7 { + if let Some((a,b)) = linear_regression(&time_list, &usage_list) { + if b != 0.0 { + let estimate = (1.0 - a) / b; + entry["estimated-full-date"] = Value::from(estimate.floor() as u64); + } + } } - } + }, + _ => {}, } list.push(entry); } - Ok(list.into()) } diff --git a/src/rrd/cache.rs b/src/rrd/cache.rs index ce3c551c..f08d6c9e 100644 --- a/src/rrd/cache.rs +++ b/src/rrd/cache.rs @@ -65,43 +65,6 @@ pub fn update_value(rel_path: &str, value: f64, dst: DST, save: bool) -> Result< 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, HashMap>>), 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( base: &str, name: &str,