diff --git a/proxmox-rrd/src/cache.rs b/proxmox-rrd/src/cache.rs index 547f6603..137e376e 100644 --- a/proxmox-rrd/src/cache.rs +++ b/proxmox-rrd/src/cache.rs @@ -120,33 +120,6 @@ impl RRDCache { RRD::new(dst, rra_list) } - fn parse_journal_line(line: &str) -> Result { - - 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!( diff --git a/proxmox-rrd/src/cache/journal.rs b/proxmox-rrd/src/cache/journal.rs index 3514e9e7..1f9c67b9 100644 --- a/proxmox-rrd/src/cache/journal.rs +++ b/proxmox-rrd/src/cache/journal.rs @@ -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 { + + 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,