Skip to content

Commit 4fde489

Browse files
authored
Refactor average rating calculation for user clarity and performance (#1406)
* refactor: rename calculate_average_rating to calculate_average_rating_for_user for clarity * refactor: update calculate_average_rating_for_user to accept user_id as a parameter * refactor: update calculate_average_rating_for_user to filter reviews by user_id * refactor: optimize calculate_average_rating_for_user to improve readability and performance * refactor: optimize calculate_average_rating_for_user to improve performance and readability
1 parent 113330c commit 4fde489

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

crates/services/miscellaneous/src/user_details.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::sync::Arc;
22

3-
use application_utils::calculate_average_rating;
3+
use application_utils::calculate_average_rating_for_user;
44
use async_graphql::Result;
55
use database_models::functions::get_user_to_entity_association;
66
use database_utils::{entity_in_collections, item_reviews};
@@ -123,7 +123,7 @@ pub async fn user_metadata_details(
123123
let seen_by: usize = seen_by.try_into().unwrap();
124124
let user_to_meta =
125125
get_user_to_entity_association(&ss.db, &user_id, &metadata_id, EntityLot::Metadata).await;
126-
let average_rating = calculate_average_rating(&reviews);
126+
let average_rating = calculate_average_rating_for_user(&user_id, &reviews);
127127
let seen_by_user_count = history.len();
128128
let show_progress = if let Some(show_specifics) = media_details.model.show_specifics {
129129
let mut seasons = vec![];
@@ -209,7 +209,7 @@ pub async fn user_person_details(
209209
get_entity_recently_consumed(&user_id, &person_id, EntityLot::Person, ss).await?;
210210
let person_meta =
211211
get_user_to_entity_association(&ss.db, &user_id, &person_id, EntityLot::Person).await;
212-
let average_rating = calculate_average_rating(&reviews);
212+
let average_rating = calculate_average_rating_for_user(&user_id, &reviews);
213213
Ok(UserPersonDetails {
214214
reviews,
215215
collections,
@@ -242,7 +242,7 @@ pub async fn user_metadata_group_details(
242242
let is_recently_consumed =
243243
get_entity_recently_consumed(&user_id, &metadata_group_id, EntityLot::MetadataGroup, ss)
244244
.await?;
245-
let average_rating = calculate_average_rating(&reviews);
245+
let average_rating = calculate_average_rating_for_user(&user_id, &reviews);
246246
let metadata_group_meta = get_user_to_entity_association(
247247
&ss.db,
248248
&user_id,

crates/utils/application/src/lib.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,19 @@ pub async fn create_oidc_client(
193193
}
194194
}
195195

196-
pub fn calculate_average_rating(reviews: &[ReviewItem]) -> Option<Decimal> {
197-
let reviews_with_ratings = reviews.iter().filter_map(|r| r.rating).count();
198-
match reviews_with_ratings {
196+
pub fn calculate_average_rating_for_user(
197+
user_id: &String,
198+
reviews: &[ReviewItem],
199+
) -> Option<Decimal> {
200+
let (sum, count) = reviews
201+
.iter()
202+
.filter(|r| r.posted_by.id == *user_id && r.rating.is_some())
203+
.map(|r| r.rating.unwrap())
204+
.fold((Decimal::ZERO, 0), |(sum, count), rating| {
205+
(sum + rating, count + 1)
206+
});
207+
match count {
199208
0 => None,
200-
_ => Some(
201-
reviews.iter().filter_map(|r| r.rating).sum::<Decimal>()
202-
/ Decimal::from(reviews_with_ratings),
203-
),
209+
_ => Some(sum / Decimal::from(count)),
204210
}
205211
}

0 commit comments

Comments
 (0)