1
1
use async_graphql:: { Context , Object , Result as GqlResult } ;
2
- use sqlx:: { PgPool } ;
2
+ use sqlx:: PgPool ;
3
3
use std:: collections:: HashMap ;
4
4
use std:: sync:: Arc ;
5
5
@@ -9,34 +9,41 @@ pub struct LeaderboardMutation;
9
9
#[ Object ]
10
10
impl LeaderboardMutation {
11
11
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 > > ( )
13
14
. map_err ( |_| async_graphql:: Error :: new ( "Failed to access the database pool" ) ) ?;
14
15
15
-
16
16
let leetcode_stats = sqlx:: query!(
17
17
"SELECT member_id, problems_solved, easy_solved, medium_solved, hard_solved,
18
18
contests_participated, best_rank
19
19
FROM leetcode_stats"
20
20
)
21
21
. fetch_all ( pool. as_ref ( ) )
22
22
. 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
+ } ) ?;
24
26
25
-
26
27
let codeforces_stats = sqlx:: query!(
27
28
"SELECT member_id, codeforces_rating, max_rating, contests_participated
28
29
FROM codeforces_stats"
29
30
)
30
31
. fetch_all ( pool. as_ref ( ) )
31
32
. 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
+ } ) ?;
33
36
34
37
let cf_lookup: HashMap < i32 , ( i32 , i32 , i32 ) > = codeforces_stats
35
38
. iter ( )
36
39
. map ( |row| {
37
40
(
38
41
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
+ ) ,
40
47
)
41
48
} )
42
49
. collect ( ) ;
@@ -48,7 +55,8 @@ impl LeaderboardMutation {
48
55
+ ( 2 * row. contests_participated )
49
56
+ ( 100 - row. best_rank / 10 ) . max ( 0 ) ;
50
57
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 )
52
60
. map ( |( rating, max_rating, contests) | {
53
61
let cf_score = ( rating / 10 ) + ( max_rating / 20 ) + ( 5 * contests) ;
54
62
( cf_score, leetcode_score + cf_score)
@@ -72,12 +80,18 @@ impl LeaderboardMutation {
72
80
. await ;
73
81
74
82
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
+ ) ;
76
87
}
77
88
}
78
89
79
90
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
+ {
81
95
continue ;
82
96
}
83
97
@@ -96,15 +110,17 @@ impl LeaderboardMutation {
96
110
unified_score = EXCLUDED.unified_score,
97
111
last_updated = NOW()" ,
98
112
row. member_id,
99
- 0 ,
100
113
codeforces_score,
101
114
unified_score
102
115
)
103
116
. execute ( pool. as_ref ( ) )
104
117
. await ;
105
118
106
119
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
+ ) ;
108
124
}
109
125
}
110
126
0 commit comments