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:
		
				
					committed by
					
						 Wolfgang Bumiller
						Wolfgang Bumiller
					
				
			
			
				
	
			
			
			
						parent
						
							431cc7b185
						
					
				
				
					commit
					ec8f042459
				
			| @ -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<f64> = Vec::new(); | ||||
|         let mut time_list: Vec<u64> = 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<f64> = Vec::new(); | ||||
|                 let mut time_list: Vec<u64> = 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()) | ||||
| } | ||||
|  | ||||
|  | ||||
| @ -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<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( | ||||
|     base: &str, | ||||
|     name: &str, | ||||
|  | ||||
		Reference in New Issue
	
	Block a user