diff --git a/ultros-db/src/sales.rs b/ultros-db/src/sales.rs index f0f473c8..23cb20cd 100644 --- a/ultros-db/src/sales.rs +++ b/ultros-db/src/sales.rs @@ -115,16 +115,33 @@ impl UltrosDb { limit: u64, ) -> Result, anyhow::Error> { let all = futures::future::try_join_all( - world_ids - .map(|world_id| self.get_sale_history_with_character(world_id, item_id, limit)), + world_ids.map(|world_id| self.get_sale_history_for_item(world_id, item_id, limit)), ) .await; - let mut val: Vec = - all?.into_iter().flat_map(|w| w.into_iter()).collect(); - val.sort_by_key(|sale| std::cmp::Reverse(sale.0.sold_date)); - val.truncate(limit as usize); - Ok(val) + let mut sales: Vec<_> = all?.into_iter().flat_map(|w| w.into_iter()).collect(); + sales.sort_by_key(|sale| std::cmp::Reverse(sale.sold_date)); + sales.truncate(limit as usize); + + let buyers = try_join_all( + sales + .iter() + .map(|s| s.buying_character_id) + .unique() + .map(|c| unknown_final_fantasy_character::Entity::find_by_id(c).one(&self.db)), + ) + .await? + .into_iter() + .filter_map(|c| c.map(|c| (c.id, c))) + .collect::>(); + let sales = sales + .into_iter() + .map(|sale| { + let buyer = buyers.get(&sale.buying_character_id).cloned(); + SaleHistoryReturn(sale, buyer) + }) + .collect(); + Ok(sales) } async fn lookup_buyer_names( @@ -165,45 +182,6 @@ impl UltrosDb { .collect()) } - pub async fn get_sale_history_with_character( - &self, - world_id: i32, - item_id: i32, - limit: u64, - ) -> Result, anyhow::Error> { - let instant = Instant::now(); - let sale_history = sale_history::Entity::find() - .filter(sale_history::Column::SoldItemId.eq(item_id)) - .filter(sale_history::Column::WorldId.eq(world_id)) - .order_by_desc(sale_history::Column::SoldDate) - .limit(limit) - .all(&self.db) - .await?; - let character_ids = try_join_all( - sale_history - .iter() - .map(|s| s.buying_character_id) - .unique() - .map(|c| unknown_final_fantasy_character::Entity::find_by_id(c).one(&self.db)), - ) - .await?; - let character_map: HashMap = character_ids - .into_iter() - .flatten() - .map(|f| (f.id, f)) - .collect(); - let sale_history = sale_history - .into_iter() - .map(|sale| { - let character = character_map.get(&sale.buying_character_id).cloned(); - SaleHistoryReturn(sale, character) - }) - .collect::>(); - histogram!("ultros_db_query_sale_history_with_character_duration_seconds") - .record(instant.elapsed()); - Ok(sale_history) - } - pub async fn get_sale_history_for_item( &self, world_id: i32,