Skip to content

Commit

Permalink
feat(api/accounting/cost): complete get_flavor_prices_for_period
Browse files Browse the repository at this point in the history
Signed-off-by: Sandro-Alessio Gierens <[email protected]>
  • Loading branch information
gierens committed Jan 17, 2025
1 parent df60db0 commit c79df24
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions api/src/routes/accounting/server_cost/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,25 @@ async fn get_flavor_prices_for_period(
.await?;
let mut prices = HashMap::new();
for price in price_list {
let user_class = UserClass::from_u32(price.user_class)?;
let flavor_name = price.flavor_name.clone();
// TODO: this contains_key insert pattern can be replaced with entry().or_insert()
if !prices.contains_key(&user_class) {
prices.insert(user_class.clone(), HashMap::new());
}
let uprices = prices.get_mut(&user_class).unwrap();
// TODO: this contains_key insert pattern can be replaced with entry().or_insert()
if !uprices.contains_key(&flavor_name) {
uprices.insert(flavor_name.clone(), Vec::new());
}
prices
.get_mut(&user_class)
.unwrap()
.get_mut(&flavor_name)
.unwrap()
.entry(UserClass::from_u32(price.user_class)?)
.or_insert_with(HashMap::new)
.entry(price.flavor_name.clone())
.or_insert_with(Vec::new)
.push(price);
}
for uprices in prices.values_mut() {
for fprices in uprices.values_mut() {
let mut i = fprices.len() - 1;
while i > 0 {
if fprices[i].start_time <= begin {
*fprices = fprices.split_off(i);
break;
}
i -= 1;
}
}
}
Ok(prices)
}

Expand Down

0 comments on commit c79df24

Please sign in to comment.