From 1bd7233c052be32fd4f7840fc008be4fd86fe097 Mon Sep 17 00:00:00 2001 From: Piv <18462828+Piv200@users.noreply.github.com> Date: Wed, 8 Feb 2023 22:12:04 +1030 Subject: [PATCH] Fix compilation, slight optimisation to allocation statistic account index search --- src/overhead_allocation.rs | 53 ++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/src/overhead_allocation.rs b/src/overhead_allocation.rs index 0f98479..0690a48 100644 --- a/src/overhead_allocation.rs +++ b/src/overhead_allocation.rs @@ -25,8 +25,8 @@ pub struct CsvAllocationStatistic { } pub struct AllocationStatisticAccountRange { - start: String, - end: String, + start: usize, + end: usize, } #[derive(Deserialize)] @@ -108,7 +108,6 @@ where .deserialize() .collect::, csv::Error>>()?; - // Sort the accounts, as allocation statistics use account ranges let all_accounts_sorted: Vec = if use_numeric_accounts { lines .iter() @@ -137,38 +136,39 @@ where .map(|allocation_statistic| { ( allocation_statistic, - split_allocation_statistic_range(allocation_statistic), + split_allocation_statistic_range(allocation_statistic, &all_accounts_sorted), ) }) .flat_map(|allocation_statistic| { let mut total_department_costs: HashMap = HashMap::new(); - let cc_costs = lines + lines .iter() .filter(|line| { let line_index = all_accounts_sorted .iter() - .position(|account| account == &line.account); + .position(|account| account == &line.account) + .unwrap(); allocation_statistic .1 .iter() - .find(|range| { - let start_index = all_accounts_sorted - .iter() - .position(|account| account == range.0); - let end_index = all_accounts_sorted - .iter() - .position(|account| account == range.1); - line_index >= start_index && line_index <= end_index - }) + .find(|range| line_index >= range.start && line_index <= range.end) .is_some() }) .for_each(|line| { - *total_department_costs.entry(line.department).or_insert(0.) += line.value; + *total_department_costs + .entry(line.department.clone()) + .or_insert(0.) += line.value; }); total_department_costs .iter() - .map(|entry| (entry.0, allocation_statistic.0.name, entry.1)) - .collect() + .map(|entry| { + ( + entry.0.clone(), + allocation_statistic.0.name.clone(), + *entry.1, + ) + }) + .collect::>() }) .collect(); // TODO: If ignore negative is used, then set values < 0 to 0 @@ -214,21 +214,30 @@ where fn split_allocation_statistic_range( allocation_statistic: &CsvAllocationStatistic, + accounts_sorted: &Vec, ) -> Vec { // TODO: This split needs to be more comprehensive so that we don't split between quotes let split = allocation_statistic.account_ranges.split(";"); split .map(|split| { let range_split = split.split('-').collect::>(); + let start_index = accounts_sorted + .iter() + .position(|account| *account == range_split[0].to_owned()) + .unwrap(); if range_split.len() == 1 { AllocationStatisticAccountRange { - start: range_split[0].to_owned(), - end: range_split[0].to_owned(), + start: start_index, + end: start_index, } } else { + let end_index = accounts_sorted + .iter() + .position(|account| *account == range_split[1].to_owned()) + .unwrap(); AllocationStatisticAccountRange { - start: range_split[0].to_owned(), - end: range_split[1].to_owned(), + start: start_index, + end: end_index, } } })