Fix compilation, slight optimisation to allocation statistic account index search

This commit is contained in:
Piv
2023-02-08 22:12:04 +10:30
parent 5595ab2f7c
commit 1bd7233c05

View File

@@ -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::<Result<Vec<CsvCost>, csv::Error>>()?;
// Sort the accounts, as allocation statistics use account ranges
let all_accounts_sorted: Vec<String> = 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<String, f64> = 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::<Vec<(String, String, f64)>>()
})
.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<String>,
) -> Vec<AllocationStatisticAccountRange> {
// 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::<Vec<_>>();
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,
}
}
})