From eb67a0911631d8017a075bb853306d31c418e8a4 Mon Sep 17 00:00:00 2001 From: Mario Krehl Date: Sun, 10 Dec 2017 18:17:38 +0100 Subject: Fix indentation in main.rs --- src/main.rs | 103 ++++++++++++++++++++++++++++-------------------------------- 1 file changed, 48 insertions(+), 55 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index f8014cc..6bd119c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,24 +64,24 @@ fn query(data: Json, config: State) -> Result if let Some(&mut (_litem, ref mut cnames)) = target_hash.get_mut(&li.alias()) { cnames.push(( t.target - .split('.') - .nth(1) - .ok_or(Error::from("no capture name found"))? - .into(), + .split('.') + .nth(1) + .ok_or(Error::from("no capture name found"))? + .into(), t.target.clone()) - ); + ); } } else { target_hash.insert( li.alias(), (&li, vec![( - t.target + t.target .split('.') .nth(1) .ok_or(Error::from("no capture name found"))? .into(), - t.target.clone()) + t.target.clone()) ] ) ); @@ -89,6 +89,8 @@ fn query(data: Json, config: State) -> Result } } } + let date_from = data.0.range.from.timestamp(); + let date_to = data.0.range.to.timestamp(); let mut response : Vec = Vec::new(); for (_alias, &(logitem, ref cns)) in target_hash.iter() { @@ -99,31 +101,30 @@ fn query(data: Json, config: State) -> Result let mut line_iter = BufReader::new( File::open(logitem.file()) .chain_err(|| format!("antikoerper log file could not be opened: {}", logitem.file()))? - ).lines(); + ).lines(); while let Some(Ok(line)) = line_iter.next() { - let capture_groups = logitem - .regex() - .captures_iter(&line) - .next() - .ok_or(Error::from("regex did not match"))?; - let timestamp = capture_groups["ts"] - .parse::() - .chain_err(|| "Failed to parse the filestamp")?; - for i in 0..cns.len() { - let captured = capture_groups[ - cns.get(i) - .ok_or(Error::from("out of bounds: capture_groups"))? - .0.as_str() - ].parse::() - .chain_err(|| "failed to parse the capture group")?; - series_vec - .get_mut(i) - .ok_or(Error::from("out of bounds: series_vec"))? - .datapoints - .push([ - captured, - timestamp - ]); + if let Some(capture_groups) = logitem.regex().captures_iter(&line).next() { + let timestamp = capture_groups["ts"] + .parse::() + .chain_err(|| "Failed to parse the filestamp")?; + if (timestamp as i64) > date_from && (timestamp as i64) < date_to { + for i in 0..cns.len() { + let captured = capture_groups[ + cns.get(i) + .ok_or(Error::from("out of bounds: capture_groups"))? + .0.as_str() + ].parse::() + .chain_err(|| "failed to parse the capture group")?; + series_vec + .get_mut(i) + .ok_or(Error::from("out of bounds: series_vec"))? + .datapoints + .push([ + captured, + timestamp * 1000.0 + ]); + } + } } } for series in series_vec.iter() { @@ -132,34 +133,26 @@ fn query(data: Json, config: State) -> Result } Ok( Json( QueryResponse{ 0 : response } ) ) - /* Series{ - target : *k, - BufReader::new(File::open(li.file())?).lines() - .map(|line| { - //let capture_groups = li.regex().captures_iter(line).first()?; - - Err(Error::from("not implemented")) - */ } fn main() { let matches = App::new("aklog-server") - .version("0.1.0") - .author("Mario Krehl ") - .about("Presents antikoerper-logfiles to grafana") - .arg(Arg::with_name("config") - .short("c") - .long("config") - .value_name("FILE") - .help("configuration file to use") - .takes_value(true) - .required(true)) - .arg(Arg::with_name("verbosity") - .short("v") - .long("verbose") - .help("sets the level of verbosity") - .multiple(true)) - .get_matches(); + .version("0.1.0") + .author("Mario Krehl ") + .about("Presents antikoerper-logfiles to grafana") + .arg(Arg::with_name("config") + .short("c") + .long("config") + .value_name("FILE") + .help("configuration file to use") + .takes_value(true) + .required(true)) + .arg(Arg::with_name("verbosity") + .short("v") + .long("verbose") + .help("sets the level of verbosity") + .multiple(true)) + .get_matches(); match matches.occurrences_of("verbosity") { 0 => SimpleLogger::init(LogLevelFilter::Warn, LogConfig::default()).unwrap(), -- cgit v1.2.3