]> git.proxmox.com Git - proxmox-backup.git/commitdiff
rrd: move creation of serde value into api
authorDominik Csapak <d.csapak@proxmox.com>
Wed, 10 Jun 2020 10:02:57 +0000 (12:02 +0200)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Wed, 10 Jun 2020 11:31:14 +0000 (13:31 +0200)
there is now a 'extract_cached_data' which just returns
the data of the specified field, and an api function that converts
a list of fields to the correct serde value

this way we do not have to create a serde value in rrd/cache.rs
(makes for a better interface)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
src/api2/admin/datastore.rs
src/api2/node.rs
src/api2/node/rrd.rs
src/rrd/cache.rs

index 222e4d214a2b00452f481a8b96174926a7158d6b..5d0b863500d5fcad01d1bcbb0a563b6fa097ccab 100644 (file)
@@ -18,6 +18,7 @@ use proxmox::try_block;
 use proxmox::{http_err, identity, list_subdirs_api_method, sortable};
 
 use crate::api2::types::*;
+use crate::api2::node::rrd::create_value_from_rrd;
 use crate::backup::*;
 use crate::config::datastore;
 use crate::config::cached_user_info::CachedUserInfo;
@@ -855,10 +856,8 @@ fn get_rrd_stats(
     _param: Value,
 ) -> Result<Value, Error> {
 
-    let rrd_dir = format!("datastore/{}", store);
-
-    crate::rrd::extract_data(
-        &rrd_dir,
+    create_value_from_rrd(
+        &format!("datastore/{}", store),
         &[
             "total", "used",
             "read_ios", "read_bytes",
index 8ff8220de8ad6abc79b8904c9af9f85e650ae176..13ff282cd1d3ca7d53d172c9404faa6dc48f366d 100644 (file)
@@ -9,7 +9,7 @@ mod syslog;
 mod journal;
 mod services;
 mod status;
-mod rrd;
+pub(crate) mod rrd;
 pub mod disks;
 
 pub const SUBDIRS: SubdirMap = &[
index 80e5c35ce6272311802fb7a34fa7c9816a917ee3..b857cd3ae94b4dea0ccec7dcd871d7fa383efb8e 100644 (file)
@@ -1,9 +1,47 @@
 use anyhow::Error;
-use serde_json::Value;
+use serde_json::{Value, json};
 
 use proxmox::api::{api, Router};
 
 use crate::api2::types::*;
+use crate::tools::epoch_now_f64;
+use crate::rrd::{extract_cached_data, RRD_DATA_ENTRIES};
+
+pub fn create_value_from_rrd(
+    basedir: &str,
+    list: &[&str],
+    timeframe: RRDTimeFrameResolution,
+    cf: RRDMode,
+) -> Result<Value, Error> {
+
+    let mut result = Vec::new();
+    let now = epoch_now_f64()?;
+
+    for name in list {
+        let (start, reso, list) = match extract_cached_data(basedir, name, now, timeframe, cf) {
+            Some(result) => result,
+            None => continue,
+        };
+
+        let mut t = start;
+        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(result.into())
+}
 
 #[api(
     input: {
@@ -27,7 +65,7 @@ fn get_node_stats(
     _param: Value,
 ) -> Result<Value, Error> {
 
-    crate::rrd::extract_data(
+    create_value_from_rrd(
         "host",
         &[
             "cpu", "iowait",
index 90d2c9d1a0960c5ce2f9123872babe9bb2ce9ff8..ce3c551ca9d50294ee7364d99215780b73da6ca3 100644 (file)
@@ -4,7 +4,6 @@ use std::sync::{RwLock};
 
 use anyhow::{format_err, Error};
 use lazy_static::lazy_static;
-use serde_json::{json, Value};
 
 use proxmox::tools::fs::{create_path, CreateOptions};
 
@@ -103,41 +102,18 @@ pub fn extract_lists(
     Ok((times, result))
 }
 
-pub fn extract_data(
+pub fn extract_cached_data(
     base: &str,
-    items: &[&str],
+    name: &str,
+    now: f64,
     timeframe: RRDTimeFrameResolution,
     mode: RRDMode,
-) -> Result<Value, Error> {
-
-    let now = epoch_now_f64()?;
+) -> Option<(u64, u64, Vec<Option<f64>>)> {
 
     let map = RRD_CACHE.read().unwrap();
 
-    let mut result = 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);
-        let mut t = start;
-        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;
-        }
+    match map.get(&format!("{}/{}", base, name)) {
+        Some(rrd) => Some(rrd.extract_data(now, timeframe, mode)),
+        None => None,
     }
-
-    Ok(result.into())
 }