From aff5f24382de6fa6f00be31383d7fd0cefd193a7 Mon Sep 17 00:00:00 2001 From: Mario Krehl Date: Sat, 16 Dec 2017 11:13:53 +0100 Subject: Refactor functionality to create the target_hash hashmap into a function --- src/main.rs | 98 +++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 53 insertions(+), 45 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6a84be1..6f00175 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,51 +75,8 @@ fn query(data: Json, config: State) -> Result let targets = data.0.targets; debug!("targets: {:?}", targets); - // If there are several targets, it is possible they would different data - // from the same file; - // this HashMap is created for the sole purpose of being able to read and - // apply a regex on a potentially huge file only once. - // HashMap - // |------- Alias : &String - // \ - // Tuple - // |------- &LogItem - // |------- Vector of Tuple - // |--- capturegroup name : String - // |--- target/metric - let mut target_hash : HashMap<&String, (&LogItem, Vec<(String, String)>)> = HashMap::new(); - for li in config.items() { - for t in targets.clone() { - if li.aliases().contains(&t.target) { - if target_hash.contains_key(&li.alias()) { - 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(), - t.target.clone()) - ); - } - } - else { - target_hash.insert( - li.alias(), - (&li, vec![( - t.target - .split('.') - .nth(1) - .ok_or(Error::from("no capture name found"))? - .into(), - t.target.clone()) - ] - ) - ); - } - } - } - } + // create hashmap to iterate over + let mut target_hash = hash_map_targets(&config, targets)?; let date_from = data.0.range.from.timestamp(); let date_to = data.0.range.to.timestamp(); @@ -193,6 +150,57 @@ fn query(data: Json, config: State) -> Result Ok( Json( QueryResponse{ 0 : response } ) ) } +/// If there are several targets, it is possible they would different data +/// from the same file; +/// this HashMap is created for the sole purpose of being able to read and +/// apply a regex on a potentially huge file only once. +/// HashMap +/// |------- Alias : &String +/// \ +/// Tuple +/// |------- &LogItem +/// |------- Vector of Tuple +/// |--- capturegroup name : String +/// |--- target/metric +fn hash_map_targets<'a>(c : &'a Config, targets : Vec) + -> Result)>> { + + let mut _res : HashMap<&String, (&LogItem, Vec<(String, String)>)> = HashMap::new(); + for li in c.items() { + for t in targets.clone() { + if li.aliases().contains(&t.target) { + if _res.contains_key(&li.alias()) { + if let Some(&mut (_litem, ref mut cnames)) = _res.get_mut(&li.alias()) { + cnames.push(( + t.target + .split('.') + .nth(1) + .ok_or(Error::from("no capture name found"))? + .into(), + t.target.clone()) + ); + } + } + else { + _res.insert( + li.alias(), + (&li, vec![( + t.target + .split('.') + .nth(1) + .ok_or(Error::from("no capture name found"))? + .into(), + t.target.clone()) + ] + ) + ); + } + } + } + } + Ok(_res) +} + fn main() { let matches = App::new("aklog-server") -- cgit v1.2.3 From 32cfc8d684b8cda57f122fc13d7b89f9efa2050c Mon Sep 17 00:00:00 2001 From: Mario Krehl Date: Sat, 16 Dec 2017 11:33:54 +0100 Subject: Iterate the HashMap via an additional function; reduce query function --- src/main.rs | 133 ++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 70 insertions(+), 63 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6f00175..4e4492c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,19 +72,77 @@ fn query(data: Json, config: State) -> Result debug!("handling query: {:?}", data.0); - let targets = data.0.targets; - debug!("targets: {:?}", targets); + Ok( + Json( + QueryResponse{ + 0 : hash_map_iter( + hash_map_targets(&config, data.0.targets)?, + data.0.range.from.timestamp(), + data.0.range.to.timestamp() + )? + } + ) + ) +} - // create hashmap to iterate over - let mut target_hash = hash_map_targets(&config, targets)?; +/// If there are several targets, it is possible they would different data +/// from the same file; +/// this HashMap is created for the sole purpose of being able to read and +/// apply a regex on a potentially huge file only once. +/// HashMap +/// |------- Alias : &String +/// \ +/// Tuple +/// |------- &LogItem +/// |------- Vector of Tuple +/// |--- capturegroup name : String +/// |--- target/metric +fn hash_map_targets<'a>(c : &'a Config, targets : Vec) + -> Result)>> { - let date_from = data.0.range.from.timestamp(); - let date_to = data.0.range.to.timestamp(); + debug!("targets: {:?}", targets); + let mut _res : HashMap<&String, (&LogItem, Vec<(String, String)>)> = HashMap::new(); + for li in c.items() { + for t in targets.clone() { + if li.aliases().contains(&t.target) { + if _res.contains_key(&li.alias()) { + if let Some(&mut (_litem, ref mut cnames)) = _res.get_mut(&li.alias()) { + cnames.push(( + t.target + .split('.') + .nth(1) + .ok_or(Error::from("no capture name found"))? + .into(), + t.target.clone()) + ); + } + } + else { + _res.insert( + li.alias(), + (&li, vec![( + t.target + .split('.') + .nth(1) + .ok_or(Error::from("no capture name found"))? + .into(), + t.target.clone()) + ] + ) + ); + } + } + } + } + Ok(_res) +} - let mut response : Vec = Vec::new(); +/// Iterate the hashmap created with the above function +fn hash_map_iter(h : HashMap<&String, (&LogItem, Vec<(String, String)>)>, d_from : i64, d_to : i64) + -> Result> { - // iterate the HashMap - for (_alias, &(logitem, ref cns)) in target_hash.iter() { + let mut _res = Vec::new(); + for (_alias, &(logitem, ref cns)) in h.iter() { // prepare an empty Vector of Series let mut series_vec = Vec::new(); @@ -110,7 +168,7 @@ fn query(data: Json, config: State) -> Result .chain_err(|| "Failed to parse the filestamp")?; // ignore every entry not in the timerange - if (timestamp as i64) > date_from && (timestamp as i64) < date_to { + if (timestamp as i64) > d_from && (timestamp as i64) < d_to { // Multiple Vectors need to be accessed with the same // index, so no iterator here. @@ -143,64 +201,13 @@ fn query(data: Json, config: State) -> Result // fill the prepared vector with all Series's for series in series_vec.iter() { - response.push(TargetData::Series((*series).clone())); - } - } - - Ok( Json( QueryResponse{ 0 : response } ) ) -} - -/// If there are several targets, it is possible they would different data -/// from the same file; -/// this HashMap is created for the sole purpose of being able to read and -/// apply a regex on a potentially huge file only once. -/// HashMap -/// |------- Alias : &String -/// \ -/// Tuple -/// |------- &LogItem -/// |------- Vector of Tuple -/// |--- capturegroup name : String -/// |--- target/metric -fn hash_map_targets<'a>(c : &'a Config, targets : Vec) - -> Result)>> { - - let mut _res : HashMap<&String, (&LogItem, Vec<(String, String)>)> = HashMap::new(); - for li in c.items() { - for t in targets.clone() { - if li.aliases().contains(&t.target) { - if _res.contains_key(&li.alias()) { - if let Some(&mut (_litem, ref mut cnames)) = _res.get_mut(&li.alias()) { - cnames.push(( - t.target - .split('.') - .nth(1) - .ok_or(Error::from("no capture name found"))? - .into(), - t.target.clone()) - ); - } - } - else { - _res.insert( - li.alias(), - (&li, vec![( - t.target - .split('.') - .nth(1) - .ok_or(Error::from("no capture name found"))? - .into(), - t.target.clone()) - ] - ) - ); - } - } + _res.push(TargetData::Series((*series).clone())); } } Ok(_res) } + fn main() { let matches = App::new("aklog-server") -- cgit v1.2.3 From 4736031a1b46924ac0aff584c74b4fb957f9ff98 Mon Sep 17 00:00:00 2001 From: Mario Krehl Date: Sat, 16 Dec 2017 11:52:36 +0100 Subject: Add helper function to get a capture group name out of a target --- src/main.rs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4e4492c..56cf351 100644 --- a/src/main.rs +++ b/src/main.rs @@ -108,26 +108,17 @@ fn hash_map_targets<'a>(c : &'a Config, targets : Vec) if _res.contains_key(&li.alias()) { if let Some(&mut (_litem, ref mut cnames)) = _res.get_mut(&li.alias()) { cnames.push(( - t.target - .split('.') - .nth(1) - .ok_or(Error::from("no capture name found"))? - .into(), + cname_from_target(&t.target)?, t.target.clone()) - ); + ); } } else { _res.insert( li.alias(), - (&li, vec![( - t.target - .split('.') - .nth(1) - .ok_or(Error::from("no capture name found"))? - .into(), - t.target.clone()) - ] + ( + &li, + vec![(cname_from_target(&t.target)?, t.target.clone())] ) ); } @@ -137,6 +128,16 @@ fn hash_map_targets<'a>(c : &'a Config, targets : Vec) Ok(_res) } +/// splits the target and return the capture name part +fn cname_from_target<'a>(t : &'a String) -> Result { + Ok( + t.split('.') + .nth(1) + .ok_or(Error::from("no capture name found"))? + .into() + ) +} + /// Iterate the hashmap created with the above function fn hash_map_iter(h : HashMap<&String, (&LogItem, Vec<(String, String)>)>, d_from : i64, d_to : i64) -> Result> { -- cgit v1.2.3 From 83cc44aec65898440373c309284adbd7c2092106 Mon Sep 17 00:00:00 2001 From: Mario Krehl Date: Sat, 16 Dec 2017 12:06:47 +0100 Subject: Fix typos --- src/config.rs | 2 +- src/main.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config.rs b/src/config.rs index ef0e330..9f28158 100644 --- a/src/config.rs +++ b/src/config.rs @@ -135,7 +135,7 @@ impl LogItem { } } -/// Containts more immediately usable data +/// Contains more immediately usable data pub struct Config { items : Vec, all_aliases : Vec, diff --git a/src/main.rs b/src/main.rs index 56cf351..43d9a48 100644 --- a/src/main.rs +++ b/src/main.rs @@ -166,7 +166,7 @@ fn hash_map_iter(h : HashMap<&String, (&LogItem, Vec<(String, String)>)>, d_from // save the timestamp for later let timestamp = capture_groups["ts"] .parse::() - .chain_err(|| "Failed to parse the filestamp")?; + .chain_err(|| "Failed to parse the timestamp")?; // ignore every entry not in the timerange if (timestamp as i64) > d_from && (timestamp as i64) < d_to { -- cgit v1.2.3