summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMario Krehl <mario-krehl@gmx.de>2017-12-16 11:13:53 +0100
committerMario Krehl <mario-krehl@gmx.de>2017-12-16 11:13:53 +0100
commitaff5f24382de6fa6f00be31383d7fd0cefd193a7 (patch)
treed2fb7ee93a9c06e5baf71bc8604ee6f7842330ff
parent5aed593ead5ac2687937132c689be74a72d3eb92 (diff)
Refactor functionality to create the target_hash hashmap into a function
-rw-r--r--src/main.rs98
1 files 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<Query>, config: State<Config>) -> Result<Json<QueryResponse>
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<Query>, config: State<Config>) -> Result<Json<QueryResponse>
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<Target>)
+ -> Result<HashMap<&'a String, (&'a LogItem, Vec<(String, String)>)>> {
+
+ 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")