11use async_graphql:: { Context , Object , Result as GqlResult } ;
2- use sqlx:: { PgPool } ;
2+ use sqlx:: PgPool ;
33use std:: collections:: HashMap ;
44use std:: sync:: Arc ;
55
@@ -9,34 +9,41 @@ pub struct LeaderboardMutation;
99#[ Object ]
1010impl LeaderboardMutation {
1111 pub async fn update_leaderboard ( & self , ctx : & Context < ' _ > ) -> GqlResult < bool > {
12- let pool = ctx. data :: < Arc < PgPool > > ( )
12+ let pool = ctx
13+ . data :: < Arc < PgPool > > ( )
1314 . map_err ( |_| async_graphql:: Error :: new ( "Failed to access the database pool" ) ) ?;
1415
15-
1616 let leetcode_stats = sqlx:: query!(
1717 "SELECT member_id, problems_solved, easy_solved, medium_solved, hard_solved,
1818 contests_participated, best_rank
1919 FROM leetcode_stats"
2020 )
2121 . fetch_all ( pool. as_ref ( ) )
2222 . await
23- . map_err ( |e| async_graphql:: Error :: new ( format ! ( "Failed to fetch LeetCode stats: {:?}" , e) ) ) ?;
23+ . map_err ( |e| {
24+ async_graphql:: Error :: new ( format ! ( "Failed to fetch LeetCode stats: {:?}" , e) )
25+ } ) ?;
2426
25-
2627 let codeforces_stats = sqlx:: query!(
2728 "SELECT member_id, codeforces_rating, max_rating, contests_participated
2829 FROM codeforces_stats"
2930 )
3031 . fetch_all ( pool. as_ref ( ) )
3132 . await
32- . map_err ( |e| async_graphql:: Error :: new ( format ! ( "Failed to fetch Codeforces stats: {:?}" , e) ) ) ?;
33+ . map_err ( |e| {
34+ async_graphql:: Error :: new ( format ! ( "Failed to fetch Codeforces stats: {:?}" , e) )
35+ } ) ?;
3336
3437 let cf_lookup: HashMap < i32 , ( i32 , i32 , i32 ) > = codeforces_stats
3538 . iter ( )
3639 . map ( |row| {
3740 (
3841 row. member_id ,
39- ( row. codeforces_rating , row. max_rating , row. contests_participated ) ,
42+ (
43+ row. codeforces_rating ,
44+ row. max_rating ,
45+ row. contests_participated ,
46+ ) ,
4047 )
4148 } )
4249 . collect ( ) ;
@@ -48,7 +55,8 @@ impl LeaderboardMutation {
4855 + ( 2 * row. contests_participated )
4956 + ( 100 - row. best_rank / 10 ) . max ( 0 ) ;
5057
51- let ( codeforces_score, unified_score) = cf_lookup. get ( & row. member_id )
58+ let ( codeforces_score, unified_score) = cf_lookup
59+ . get ( & row. member_id )
5260 . map ( |( rating, max_rating, contests) | {
5361 let cf_score = ( rating / 10 ) + ( max_rating / 20 ) + ( 5 * contests) ;
5462 ( cf_score, leetcode_score + cf_score)
@@ -72,12 +80,18 @@ impl LeaderboardMutation {
7280 . await ;
7381
7482 if let Err ( e) = result {
75- eprintln ! ( "Failed to update leaderboard for member ID {}: {:?}" , row. member_id, e) ;
83+ eprintln ! (
84+ "Failed to update leaderboard for member ID {}: {:?}" ,
85+ row. member_id, e
86+ ) ;
7687 }
7788 }
7889
7990 for row in & codeforces_stats {
80- if leetcode_stats. iter ( ) . any ( |lc| lc. member_id == row. member_id ) {
91+ if leetcode_stats
92+ . iter ( )
93+ . any ( |lc| lc. member_id == row. member_id )
94+ {
8195 continue ;
8296 }
8397
@@ -96,15 +110,17 @@ impl LeaderboardMutation {
96110 unified_score = EXCLUDED.unified_score,
97111 last_updated = NOW()" ,
98112 row. member_id,
99- 0 ,
100113 codeforces_score,
101114 unified_score
102115 )
103116 . execute ( pool. as_ref ( ) )
104117 . await ;
105118
106119 if let Err ( e) = result {
107- eprintln ! ( "Failed to update leaderboard for Codeforces-only member ID {}: {:?}" , row. member_id, e) ;
120+ eprintln ! (
121+ "Failed to update leaderboard for Codeforces-only member ID {}: {:?}" ,
122+ row. member_id, e
123+ ) ;
108124 }
109125 }
110126
0 commit comments