proxmox-rrd: cleanup - impl FromStr for JournalEntry

Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
This commit is contained in:
Dietmar Maurer 2021-10-19 10:51:22 +02:00
parent 934c8724e2
commit ef2944bc24
2 changed files with 34 additions and 29 deletions

View 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!(

View 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,