diff options
| author | Benedict Börger <benedict@0xb8000.de> | 2019-06-24 21:49:51 +0200 |
|---|---|---|
| committer | Benedict Börger <benedict@0xb8000.de> | 2019-06-24 21:49:51 +0200 |
| commit | 18dd51842832ac21d81b398cc512e9c6ef245039 (patch) | |
| tree | 9447e175112569d10aab1b4431bf32a5316d483c /src/web_frontend/chart.rs | |
| parent | b7a7d1d9512357bb2309fff50afaebd4082f9dd8 (diff) | |
[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.
Diffstat (limited to 'src/web_frontend/chart.rs')
| -rw-r--r-- | src/web_frontend/chart.rs | 33 |
1 files changed, 16 insertions, 17 deletions
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, |
