From 2bb5df40f7983665287d9a0b87b23eb9d04b668b Mon Sep 17 00:00:00 2001 From: Josh Smith Date: Sun, 21 Apr 2024 11:45:05 -0400 Subject: [PATCH] Use new tables & columns for stats reads (#8) --- src/api/routes/reworks/user.rs | 26 +++++++------- src/processor/mod.rs | 58 ++++++++++++-------------------- src/repositories/leaderboards.rs | 15 ++++----- 3 files changed, 39 insertions(+), 60 deletions(-) diff --git a/src/api/routes/reworks/user.rs b/src/api/routes/reworks/user.rs index f06b74d..98393e3 100644 --- a/src/api/routes/reworks/user.rs +++ b/src/api/routes/reworks/user.rs @@ -30,18 +30,17 @@ async fn get_rework_user( ctx: Extension>, Path(user_id): Path, ) -> AppResult>> { - let stats: Option<(String, String)> = sqlx::query_as( - "SELECT users.username, country FROM users INNER JOIN users_stats USING(id) WHERE id = ?", - ) - .bind(user_id) - .fetch_optional(ctx.database.get().await?.deref_mut()) - .await?; + let user_info: Option<(String, String)> = + sqlx::query_as("SELECT username, country FROM users WHERE id = ?") + .bind(user_id) + .fetch_optional(ctx.database.get().await?.deref_mut()) + .await?; - if stats.is_none() { + if user_info.is_none() { return Ok(Json(None)); } - let (user_name, country) = stats.unwrap(); + let (user_name, country) = user_info.unwrap(); let rework_stats: Vec = sqlx::query_as("SELECT * FROM rework_stats WHERE user_id = ?") @@ -90,12 +89,11 @@ async fn get_rework_stats( let stats = stats.unwrap(); - let user_info: (String, String) = sqlx::query_as( - "SELECT users_stats.country, users.username FROM users_stats INNER JOIN users USING(id) WHERE users_stats.id = ?" - ) - .bind(user_id) - .fetch_one(ctx.database.get().await?.deref_mut()) - .await?; + let user_info: (String, String) = + sqlx::query_as("SELECT username, country FROM users WHERE id = ?") + .bind(user_id) + .fetch_one(ctx.database.get().await?.deref_mut()) + .await?; let mut redis_connection = ctx.redis.get_async_connection().await?; diff --git a/src/processor/mod.rs b/src/processor/mod.rs index 989ce1c..2175307 100644 --- a/src/processor/mod.rs +++ b/src/processor/mod.rs @@ -230,19 +230,19 @@ async fn handle_queue_request( let scores: Vec = sqlx::query_as( &format!( - "SELECT s.id, s.beatmap_md5, s.userid, s.score, s.max_combo, s.full_combo, s.mods, s.300_count, - s.100_count, s.50_count, s.katus_count, s.gekis_count, s.misses_count, s.time, s.play_mode, s.completed, - s.accuracy, s.pp, s.checksum, s.patcher, s.pinned, b.beatmap_id, b.beatmapset_id, b.song_name - FROM {} s - INNER JOIN - beatmaps b - USING(beatmap_md5) - WHERE - userid = ? - AND completed = 3 - AND play_mode = ? - AND ranked IN (3, 2) - ORDER BY pp DESC + "SELECT s.id, s.beatmap_md5, s.userid, s.score, s.max_combo, s.full_combo, s.mods, s.300_count, + s.100_count, s.50_count, s.katus_count, s.gekis_count, s.misses_count, s.time, s.play_mode, s.completed, + s.accuracy, s.pp, s.checksum, s.patcher, s.pinned, b.beatmap_id, b.beatmapset_id, b.song_name + FROM {} s + INNER JOIN + beatmaps b + USING(beatmap_md5) + WHERE + userid = ? + AND completed = 3 + AND play_mode = ? + AND ranked IN (3, 2) + ORDER BY pp DESC LIMIT 100", scores_table ) @@ -268,8 +268,8 @@ async fn handle_queue_request( for rework_score in rework_scores { sqlx::query( - "REPLACE INTO rework_scores (score_id, beatmap_id, beatmapset_id, user_id, rework_id, max_combo, - mods, accuracy, score, num_300s, num_100s, num_50s, num_gekis, num_katus, num_misses, old_pp, new_pp) + "REPLACE INTO rework_scores (score_id, beatmap_id, beatmapset_id, user_id, rework_id, max_combo, + mods, accuracy, score, num_300s, num_100s, num_50s, num_gekis, num_katus, num_misses, old_pp, new_pp) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ) .bind(rework_score.score_id) @@ -293,28 +293,12 @@ async fn handle_queue_request( .await?; } - let stats_table = match rework.rx { - 0 => "users_stats", - 1 => "rx_stats", - 2 => "ap_stats", - _ => unreachable!(), - }; - - let stats_prefix = match rework.mode { - 0 => "std", - 1 => "taiko", - 2 => "ctb", - 3 => "mania", - _ => unreachable!(), - }; - - let old_pp: i32 = sqlx::query_scalar(&format!( - r#"SELECT pp_{} FROM {} WHERE id = ?"#, - stats_prefix, stats_table - )) - .bind(request.user_id) - .fetch_one(context.database.get().await?.deref_mut()) - .await?; + let old_pp: i32 = + sqlx::query_scalar(r#"SELECT pp FROM user_stats WHERE user_id = ? AND mode = ?"#) + .bind(request.user_id) + .bind(rework.mode + (rework.rx * 4)) + .fetch_one(context.database.get().await?.deref_mut()) + .await?; let rework_stats = ReworkStats { user_id: request.user_id, diff --git a/src/repositories/leaderboards.rs b/src/repositories/leaderboards.rs index 6855332..6c0832f 100644 --- a/src/repositories/leaderboards.rs +++ b/src/repositories/leaderboards.rs @@ -35,19 +35,16 @@ impl LeaderboardsRepository { .await?; let rework_users: Vec = sqlx::query_as( - "SELECT user_id, users_stats.country, users.username user_name, rework_id, old_pp, new_pp, - DENSE_RANK() OVER (ORDER BY old_pp DESC) old_rank, DENSE_RANK() OVER (ORDER BY new_pp DESC) new_rank - FROM - rework_stats - INNER JOIN - users_stats - ON users_stats.id = rework_stats.user_id + "SELECT user_id, country, users.username user_name, rework_id, old_pp, new_pp, + DENSE_RANK() OVER (ORDER BY old_pp DESC) old_rank, DENSE_RANK() OVER (ORDER BY new_pp DESC) new_rank + FROM + rework_stats INNER JOIN users ON users.id = rework_stats.user_id - WHERE + WHERE rework_id = ? - ORDER BY + ORDER BY new_pp DESC LIMIT ?, ?" )