]> git.proxmox.com Git - proxmox.git/commitdiff
proxmox-rrd: cleanup - impl FromStr for JournalEntry
authorDietmar Maurer <dietmar@proxmox.com>
Tue, 19 Oct 2021 08:51:22 +0000 (10:51 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Tue, 19 Oct 2021 09:17:09 +0000 (11:17 +0200)
Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
proxmox-rrd/src/cache.rs
proxmox-rrd/src/cache/journal.rs

index 547f66038b2a20844a59fa1a0368809213225b14..137e376e46177027f3a1ad0fc797c9371126b949 100644 (file)
@@ -120,33 +120,6 @@ impl RRDCache {
         RRD::new(dst, rra_list)
     }
 
-    fn parse_journal_line(line: &str) -> Result<JournalEntry, Error> {
-
-        let line = line.trim();
-
-        let parts: Vec<&str> = line.splitn(4, ':').collect();
-        if parts.len() != 4 {
-            bail!("wrong numper of components");
-        }
-
-        let time: f64 = parts[0].parse()
-            .map_err(|_| format_err!("unable to parse time"))?;
-        let value: f64 = parts[1].parse()
-            .map_err(|_| format_err!("unable to parse value"))?;
-        let dst: u8 = parts[2].parse()
-            .map_err(|_| format_err!("unable to parse data source type"))?;
-
-        let dst = match dst {
-            0 => DST::Gauge,
-            1 => DST::Derive,
-            _ => bail!("got strange value for data source type '{}'", dst),
-        };
-
-        let rel_path = parts[3].to_string();
-
-        Ok(JournalEntry { time, value, dst, rel_path })
-    }
-
     pub fn sync_journal(&self) -> Result<(), Error> {
         self.state.read().unwrap().sync_journal()
     }
@@ -301,7 +274,7 @@ fn apply_journal_lines(
 
         if len == 0 { break; }
 
-        let entry = match RRDCache::parse_journal_line(&line) {
+        let entry: JournalEntry = match line.parse() {
             Ok(entry) => entry,
             Err(err) => {
                 log::warn!(
index 3514e9e7c6bb4daaca9fc9f94e88e478d3dff5b3..1f9c67b9e1d48d29d808e9751efcefbd692d42d4 100644 (file)
@@ -4,8 +4,9 @@ use std::sync::Arc;
 use std::io::{Write, BufReader};
 use std::ffi::OsStr;
 use std::os::unix::io::AsRawFd;
+use std::str::FromStr;
 
-use anyhow::Error;
+use anyhow::{bail, format_err, Error};
 use nix::fcntl::OFlag;
 use crossbeam_channel::Receiver;
 
@@ -32,6 +33,37 @@ pub struct JournalEntry {
     pub rel_path: String,
 }
 
+impl FromStr for JournalEntry {
+    type Err = Error;
+
+   fn from_str(line: &str) -> Result<Self, Self::Err> {
+
+       let line = line.trim();
+
+        let parts: Vec<&str> = line.splitn(4, ':').collect();
+        if parts.len() != 4 {
+            bail!("wrong numper of components");
+        }
+
+        let time: f64 = parts[0].parse()
+            .map_err(|_| format_err!("unable to parse time"))?;
+        let value: f64 = parts[1].parse()
+            .map_err(|_| format_err!("unable to parse value"))?;
+        let dst: u8 = parts[2].parse()
+            .map_err(|_| format_err!("unable to parse data source type"))?;
+
+        let dst = match dst {
+            0 => DST::Gauge,
+            1 => DST::Derive,
+            _ => bail!("got strange value for data source type '{}'", dst),
+        };
+
+        let rel_path = parts[3].to_string();
+
+        Ok(JournalEntry { time, value, dst, rel_path })
+   }
+}
+
 pub struct JournalFileInfo {
     pub time: u64,
     pub name: String,