Skip to content

Commit

Permalink
refactor(api/accounting/cost): apply map.entry().or_default/insert() …
Browse files Browse the repository at this point in the history
…pattern

Signed-off-by: Sandro-Alessio Gierens <[email protected]>
  • Loading branch information
gierens committed Jan 15, 2025
1 parent 8e6596b commit df60db0
Showing 1 changed file with 7 additions and 27 deletions.
34 changes: 7 additions & 27 deletions api/src/routes/accounting/server_consumption/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,11 @@ pub async fn calculate_server_consumption_for_server(
}
}
for state in states {
// TODO: this contains_key insert pattern can be replaced with entry().or_insert()
if !consumption.contains_key(&state.flavor_name) {
consumption.insert(state.flavor_name.clone(), 0.0);
}
let entry = consumption.entry(state.flavor_name).or_default();
if !CONSUMING_STATES.contains(&state.status.as_str()) {
continue;
}
*consumption.get_mut(&state.flavor_name).unwrap() +=
(state.end.unwrap() - state.begin).num_seconds() as f64;
*entry += (state.end.unwrap() - state.begin).num_seconds() as f64;
}
// TODO:
Ok(consumption)
Expand Down Expand Up @@ -118,13 +114,9 @@ pub async fn calculate_server_consumption_for_user(
let mut server_state_map: HashMap<String, Vec<ServerState>> =
HashMap::new();
for state in states {
// TODO: this contains_key insert pattern can be replaced with entry().or_insert()
if !server_state_map.contains_key(state.instance_id.as_str()) {
server_state_map.insert(state.instance_id.clone(), Vec::new());
}
server_state_map
.get_mut(state.instance_id.as_str())
.unwrap()
.entry(state.instance_id.clone())
.or_default()
.push(state);
}

Expand All @@ -145,11 +137,7 @@ pub async fn calculate_server_consumption_for_user(

for server_consumption in consumption.servers.values() {
for (flavor, value) in server_consumption {
// TODO: this contains_key insert pattern can be replaced with entry().or_insert()
if !consumption.total.contains_key(flavor.as_str()) {
consumption.total.insert(flavor.clone(), 0.0);
}
*consumption.total.get_mut(flavor.as_str()).unwrap() += value;
*consumption.total.entry(flavor.clone()).or_default() += value;
}
}

Expand Down Expand Up @@ -193,11 +181,7 @@ pub async fn calculate_server_consumption_for_project(
};

for (flavor, value) in user_consumption.total.clone() {
// TODO: this contains_key insert pattern can be replaced with entry().or_insert()
if !consumption.total.contains_key(flavor.as_str()) {
consumption.total.insert(flavor.clone(), 0.0);
}
*consumption.total.get_mut(flavor.as_str()).unwrap() += value;
*consumption.total.entry(flavor.clone()).or_default() += value;
}

consumption
Expand Down Expand Up @@ -244,11 +228,7 @@ pub async fn calculate_server_consumption_for_all(
};

for (flavor, value) in project_consumption.total.clone() {
// TODO: this contains_key insert pattern can be replaced with entry().or_insert()
if !consumption.total.contains_key(flavor.as_str()) {
consumption.total.insert(flavor.clone(), 0.0);
}
*consumption.total.get_mut(flavor.as_str()).unwrap() += value;
*consumption.total.entry(flavor.clone()).or_default() += value;
}

consumption
Expand Down

0 comments on commit df60db0

Please sign in to comment.