From 18dd51842832ac21d81b398cc512e9c6ef245039 Mon Sep 17 00:00:00 2001 From: Benedict Börger Date: Mon, 24 Jun 2019 21:49:51 +0200 Subject: [web_frontend] move transaction_filter to util Since the sites transactions and chart both filter the transactions, move the function to util, whre both can access it. --- src/web_frontend/chart.rs | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'src/web_frontend/chart.rs') diff --git a/src/web_frontend/chart.rs b/src/web_frontend/chart.rs index 0992e99..a6b387a 100644 --- a/src/web_frontend/chart.rs +++ b/src/web_frontend/chart.rs @@ -64,16 +64,17 @@ pub fn chart_handler(account : &RawStr, start : Option<&RawStr>, end : Option<&R let t = acc.transactions; // filter transaction to match only the specified timeframe - println!("unfiltered number: {}", t.len()); let mut t_filtered = Vec::new(); for date in date_range { - let mut tmp : Vec<_> = t.iter().filter(|x| x.date.month() == date.month() && x.date.year() == date.year()).collect(); - t_filtered.append(& mut tmp); + let tc = t.clone(); + let mut tmp : Vec<_> = tc.into_iter().filter(|x| x.date.month() == date.month() && x.date.year() == date.year()).collect(); + t_filtered.append(&mut tmp); } - println!("filtered number: {}", t_filtered.len()); - let total_sum = t_filtered.iter().filter(|t| t.amount < 0.0 ) - .fold(0.0, |acc, x| acc + x.amount).abs(); + // spending chart, so do not consider income + let t_final : Vec<_> = t_filtered.clone().into_iter().filter(|t| t.amount < 0.0 ).collect(); + + let total_sum = t_final.clone().into_iter().fold(0.0, |acc, x| acc + x.amount).abs(); println!("total sum: {}", total_sum); let mut total_chart = 0.0; for (section_name, entries) in ini_file.sections { @@ -81,24 +82,22 @@ pub fn chart_handler(account : &RawStr, start : Option<&RawStr>, end : Option<&R let mut complete = 0.0; for (key, values) in entries { for val in values { - filter_string.push_str(&key); - filter_string.push_str("-"); - filter_string.push_str(&val); - filter_string.push_str("."); - let mut t_filtered_cloned = t_filtered.clone(); if val.is_empty() || val.is_empty() { continue } - let re = Regex::new(&val).unwrap(); - let tmp = t_filtered_cloned.into_iter().filter(|transaction| - re.is_match(&transaction.sender_name) ) - .fold(0.0, |acc, x| acc + x.amount); - complete = complete + tmp.abs(); + filter_string.push_str(&key); + filter_string.push_str("-"); + filter_string.push_str(&val); + filter_string.push_str(";"); } } + let t_filtered_cloned = crate::web_frontend::util::apply_transaction_filter(filter_string.clone(), t_final.clone()); + println!("for filter: {}: transactions: {}", filter_string, t_filtered_cloned.len()); + let tmp = t_filtered_cloned.into_iter() + .fold(0.0, |acc, x| acc + x.amount); + complete = complete + tmp.abs(); groups.insert(section_name, GroupValues{ amount: complete, filter : filter_string } ); total_chart = total_chart + complete; - // ALSO INSERT OTHER, AKA THE REST } let context = ChartContext { account_name : account_name, groups : groups, total_sum : total_sum, total_chart : total_chart, -- cgit v1.2.3-70-g09d2