Skip to content

Commit

Permalink
Use new tables & columns for stats reads (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyui authored Apr 21, 2024
1 parent 64b24c4 commit 2bb5df4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 60 deletions.
26 changes: 12 additions & 14 deletions src/api/routes/reworks/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,17 @@ async fn get_rework_user(
ctx: Extension<Arc<Context>>,
Path(user_id): Path<i32>,
) -> AppResult<Json<Option<ReworkUser>>> {
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<ReworkStats> =
sqlx::query_as("SELECT * FROM rework_stats WHERE user_id = ?")
Expand Down Expand Up @@ -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?;

Expand Down
58 changes: 21 additions & 37 deletions src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,19 +230,19 @@ async fn handle_queue_request(

let scores: Vec<RippleScore> = 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
)
Expand All @@ -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)
Expand All @@ -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,
Expand Down
15 changes: 6 additions & 9 deletions src/repositories/leaderboards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,16 @@ impl LeaderboardsRepository {
.await?;

let rework_users: Vec<APIReworkStats> = 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 ?, ?"
)
Expand Down

0 comments on commit 2bb5df4

Please sign in to comment.