Fix compilation, slight optimisation to allocation statistic account index search
This commit is contained in:
@@ -25,8 +25,8 @@ pub struct CsvAllocationStatistic {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct AllocationStatisticAccountRange {
|
pub struct AllocationStatisticAccountRange {
|
||||||
start: String,
|
start: usize,
|
||||||
end: String,
|
end: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@@ -108,7 +108,6 @@ where
|
|||||||
.deserialize()
|
.deserialize()
|
||||||
.collect::<Result<Vec<CsvCost>, csv::Error>>()?;
|
.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 {
|
let all_accounts_sorted: Vec<String> = if use_numeric_accounts {
|
||||||
lines
|
lines
|
||||||
.iter()
|
.iter()
|
||||||
@@ -137,38 +136,39 @@ where
|
|||||||
.map(|allocation_statistic| {
|
.map(|allocation_statistic| {
|
||||||
(
|
(
|
||||||
allocation_statistic,
|
allocation_statistic,
|
||||||
split_allocation_statistic_range(allocation_statistic),
|
split_allocation_statistic_range(allocation_statistic, &all_accounts_sorted),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.flat_map(|allocation_statistic| {
|
.flat_map(|allocation_statistic| {
|
||||||
let mut total_department_costs: HashMap<String, f64> = HashMap::new();
|
let mut total_department_costs: HashMap<String, f64> = HashMap::new();
|
||||||
let cc_costs = lines
|
lines
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|line| {
|
.filter(|line| {
|
||||||
let line_index = all_accounts_sorted
|
let line_index = all_accounts_sorted
|
||||||
.iter()
|
.iter()
|
||||||
.position(|account| account == &line.account);
|
.position(|account| account == &line.account)
|
||||||
|
.unwrap();
|
||||||
allocation_statistic
|
allocation_statistic
|
||||||
.1
|
.1
|
||||||
.iter()
|
.iter()
|
||||||
.find(|range| {
|
.find(|range| line_index >= range.start && line_index <= range.end)
|
||||||
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
|
|
||||||
})
|
|
||||||
.is_some()
|
.is_some()
|
||||||
})
|
})
|
||||||
.for_each(|line| {
|
.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
|
total_department_costs
|
||||||
.iter()
|
.iter()
|
||||||
.map(|entry| (entry.0, allocation_statistic.0.name, entry.1))
|
.map(|entry| {
|
||||||
.collect()
|
(
|
||||||
|
entry.0.clone(),
|
||||||
|
allocation_statistic.0.name.clone(),
|
||||||
|
*entry.1,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect::<Vec<(String, String, f64)>>()
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
// TODO: If ignore negative is used, then set values < 0 to 0
|
// TODO: If ignore negative is used, then set values < 0 to 0
|
||||||
@@ -214,21 +214,30 @@ where
|
|||||||
|
|
||||||
fn split_allocation_statistic_range(
|
fn split_allocation_statistic_range(
|
||||||
allocation_statistic: &CsvAllocationStatistic,
|
allocation_statistic: &CsvAllocationStatistic,
|
||||||
|
accounts_sorted: &Vec<String>,
|
||||||
) -> Vec<AllocationStatisticAccountRange> {
|
) -> Vec<AllocationStatisticAccountRange> {
|
||||||
// TODO: This split needs to be more comprehensive so that we don't split between quotes
|
// TODO: This split needs to be more comprehensive so that we don't split between quotes
|
||||||
let split = allocation_statistic.account_ranges.split(";");
|
let split = allocation_statistic.account_ranges.split(";");
|
||||||
split
|
split
|
||||||
.map(|split| {
|
.map(|split| {
|
||||||
let range_split = split.split('-').collect::<Vec<_>>();
|
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 {
|
if range_split.len() == 1 {
|
||||||
AllocationStatisticAccountRange {
|
AllocationStatisticAccountRange {
|
||||||
start: range_split[0].to_owned(),
|
start: start_index,
|
||||||
end: range_split[0].to_owned(),
|
end: start_index,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
let end_index = accounts_sorted
|
||||||
|
.iter()
|
||||||
|
.position(|account| *account == range_split[1].to_owned())
|
||||||
|
.unwrap();
|
||||||
AllocationStatisticAccountRange {
|
AllocationStatisticAccountRange {
|
||||||
start: range_split[0].to_owned(),
|
start: start_index,
|
||||||
end: range_split[1].to_owned(),
|
end: end_index,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user