Skip to content

Commit c49b23e

Browse files
committed
refactor:auto-formated code using rustfmt
1 parent 1626748 commit c49b23e

File tree

10 files changed

+102
-64
lines changed

10 files changed

+102
-64
lines changed

src/daily_task/mod.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use chrono::NaiveTime;
2+
use crate::graphql::mutations::{
3+
fetch_and_update_codeforces_stats, fetch_and_update_leetcode, update_leaderboard_scores,
4+
};
25
use chrono_tz::Asia::Kolkata;
36
use sqlx::PgPool;
47
use std::sync::Arc;
58
use tokio::time::sleep_until;
69
use tracing::{debug, error, info};
7-
use crate::graphql::mutations::{fetch_and_update_codeforces_stats,fetch_and_update_leetcode,update_leaderboard_scores};
810

9-
use crate::{
10-
models::{
11-
leaderboard::{CodeforcesStats, LeetCodeStats},
12-
member::Member,
13-
},
11+
use crate::models::{
12+
leaderboard::{CodeforcesStats, LeetCodeStats},
13+
member::Member,
1414
};
1515

1616
pub async fn run_daily_task_at_midnight(pool: Arc<PgPool>) {
@@ -88,10 +88,13 @@ pub async fn update_leaderboard_task(pool: Arc<PgPool>) {
8888
let username = leetcode_stats.leetcode_username.clone();
8989

9090
// Fetch and update LeetCode stats
91-
match fetch_and_update_leetcode(pool.clone(), member.member_id, &username).await {
92-
Ok(_) => println!("LeetCode stats updated for member ID: {}", member.member_id),
91+
match fetch_and_update_leetcode(pool.clone(), member.member_id, &username).await
92+
{
93+
Ok(_) => {
94+
println!("LeetCode stats updated for member ID: {}", member.member_id)
95+
}
9396
Err(e) => eprintln!(
94-
"Failed to update LeetCode stats for member ID {}: {:?}",
97+
"Failed to update LeetCode stats for member ID {}: {:?}",
9598
member.member_id, e
9699
),
97100
}
@@ -109,11 +112,19 @@ pub async fn update_leaderboard_task(pool: Arc<PgPool>) {
109112
let username = codeforces_stats.codeforces_handle.clone();
110113

111114
// Fetch and update Codeforces stats
112-
match fetch_and_update_codeforces_stats(pool.clone(), member.member_id, &username).await
115+
match fetch_and_update_codeforces_stats(
116+
pool.clone(),
117+
member.member_id,
118+
&username,
119+
)
120+
.await
113121
{
114-
Ok(_) => println!("Codeforces stats updated for member ID: {}", member.member_id),
122+
Ok(_) => println!(
123+
"Codeforces stats updated for member ID: {}",
124+
member.member_id
125+
),
115126
Err(e) => eprintln!(
116-
"Failed to update Codeforces stats for member ID {}: {:?}",
127+
"Failed to update Codeforces stats for member ID {}: {:?}",
117128
member.member_id, e
118129
),
119130
}
@@ -130,8 +141,6 @@ pub async fn update_leaderboard_task(pool: Arc<PgPool>) {
130141
}
131142
}
132143

133-
134-
135144
async fn update_attendance(members: Vec<Member>, pool: &PgPool) {
136145
#[allow(deprecated)]
137146
let today = chrono::Utc::now()
@@ -208,4 +217,4 @@ async fn update_status_history(members: &Vec<Member>, pool: &PgPool) {
208217
}
209218
}
210219
}
211-
}
220+
}

src/graphql/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
use async_graphql::MergedObject;
2-
use mutations::{AttendanceMutations, MemberMutations, ProjectMutations, StreakMutations,FetchLeetCode,FetchCodeForces,LeaderboardMutation};
3-
use queries::{AttendanceQueries, MemberQueries, ProjectQueries, StreakQueries, LeaderboardQueries};
2+
use mutations::{
3+
AttendanceMutations, FetchCodeForces, FetchLeetCode, LeaderboardMutation, MemberMutations,
4+
ProjectMutations, StreakMutations,
5+
};
6+
use queries::{
7+
AttendanceQueries, LeaderboardQueries, MemberQueries, ProjectQueries, StreakQueries,
8+
};
49

510
pub mod mutations;
611
pub mod queries;
@@ -23,5 +28,4 @@ pub struct Mutation(
2328
FetchLeetCode,
2429
FetchCodeForces,
2530
LeaderboardMutation,
26-
2731
);

src/graphql/mutations/codeforces_status.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use super::leaderboard_api::fetch_and_update_codeforces_stats;
12
use async_graphql::{Context, Object, Result};
23
use sqlx::PgPool;
34
use std::sync::Arc;
4-
use super::leaderboard_api::fetch_and_update_codeforces_stats;
55

66
#[derive(Default)]
77
pub struct FetchCodeForces;

src/graphql/mutations/leaderboard_api.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
use reqwest;
2+
use reqwest::Client;
23
use serde_json::Value;
34
use sqlx::PgPool;
4-
use std::sync::Arc;
55
use std::collections::HashMap;
6-
use reqwest::Client;
7-
6+
use std::sync::Arc;
87

98
pub async fn fetch_and_update_codeforces_stats(
109
pool: Arc<PgPool>,
1110
member_id: i32,
1211
username: &str,
13-
) -> Result<(), Box<dyn std::error::Error + Send + Sync>>{
12+
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
1413
let url = format!("https://codeforces.com/api/user.rating?handle={}", username);
1514
let response = reqwest::get(&url).await?.text().await?;
1615
let data: Value = serde_json::from_str(&response)?;
@@ -87,7 +86,11 @@ pub async fn update_leaderboard_scores(pool: Arc<PgPool>) -> Result<(), sqlx::Er
8786
.map(|row| {
8887
(
8988
row.member_id,
90-
(row.codeforces_rating, row.max_rating, row.contests_participated),
89+
(
90+
row.codeforces_rating,
91+
row.max_rating,
92+
row.contests_participated,
93+
),
9194
)
9295
})
9396
.collect();
@@ -99,7 +102,8 @@ pub async fn update_leaderboard_scores(pool: Arc<PgPool>) -> Result<(), sqlx::Er
99102
+ (2 * row.contests_participated)
100103
+ (100 - row.best_rank / 10).max(0);
101104

102-
let (codeforces_score, unified_score) = cf_lookup.get(&row.member_id)
105+
let (codeforces_score, unified_score) = cf_lookup
106+
.get(&row.member_id)
103107
.map(|(rating, max_rating, contests)| {
104108
let cf_score = (rating / 10) + (max_rating / 20) + (5 * contests);
105109
(cf_score, leetcode_score + cf_score)
@@ -123,18 +127,23 @@ pub async fn update_leaderboard_scores(pool: Arc<PgPool>) -> Result<(), sqlx::Er
123127
.await;
124128

125129
if let Err(e) = result {
126-
eprintln!("Failed to update leaderboard for member ID {}: {:?}", row.member_id, e);
130+
eprintln!(
131+
"Failed to update leaderboard for member ID {}: {:?}",
132+
row.member_id, e
133+
);
127134
}
128135
}
129136

130137
for row in &codeforces_stats {
131-
if leetcode_stats.iter().any(|lc| lc.member_id == row.member_id) {
138+
if leetcode_stats
139+
.iter()
140+
.any(|lc| lc.member_id == row.member_id)
141+
{
132142
continue;
133143
}
134144

135-
let codeforces_score = (row.codeforces_rating / 10)
136-
+ (row.max_rating / 20)
137-
+ (5 * row.contests_participated);
145+
let codeforces_score =
146+
(row.codeforces_rating / 10) + (row.max_rating / 20) + (5 * row.contests_participated);
138147

139148
let unified_score = codeforces_score;
140149

@@ -155,15 +164,16 @@ pub async fn update_leaderboard_scores(pool: Arc<PgPool>) -> Result<(), sqlx::Er
155164
.await;
156165

157166
if let Err(e) = result {
158-
eprintln!("Failed to update leaderboard for Codeforces-only member ID {}: {:?}", row.member_id, e);
167+
eprintln!(
168+
"Failed to update leaderboard for Codeforces-only member ID {}: {:?}",
169+
row.member_id, e
170+
);
159171
}
160172
}
161173

162174
Ok(())
163175
}
164176

165-
166-
167177
pub async fn fetch_and_update_leetcode(
168178
pool: Arc<PgPool>,
169179
member_id: i32,

src/graphql/mutations/leetcode_status.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use super::leaderboard_api::fetch_and_update_leetcode;
12
use async_graphql::{Context, Object, Result};
23
use sqlx::PgPool;
34
use std::sync::Arc;
4-
use super::leaderboard_api::fetch_and_update_leetcode;
55

66
#[derive(Default)]
77
pub struct FetchLeetCode;

src/graphql/mutations/mod.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
pub mod attendance_mutations;
2+
pub mod codeforces_status;
3+
pub mod leaderboard_api;
4+
pub mod leetcode_status;
25
pub mod member_mutations;
36
pub mod project_mutations;
47
pub mod streak_mutations;
5-
pub mod update_leaderboard; //leaderboard
6-
pub mod leetcode_status;
7-
pub mod codeforces_status;
8-
pub mod leaderboard_api;
9-
8+
pub mod update_leaderboard; //leaderboard
109

1110
pub use attendance_mutations::AttendanceMutations;
12-
pub use member_mutations::MemberMutations;
13-
pub use project_mutations::ProjectMutations;
14-
pub use streak_mutations::StreakMutations;
15-
pub use leetcode_status::FetchLeetCode;
1611
pub use codeforces_status::FetchCodeForces;
17-
pub use update_leaderboard::LeaderboardMutation;
1812
pub use leaderboard_api::fetch_and_update_codeforces_stats;
1913
pub use leaderboard_api::fetch_and_update_leetcode;
2014
pub use leaderboard_api::update_leaderboard_scores;
15+
pub use leetcode_status::FetchLeetCode;
16+
pub use member_mutations::MemberMutations;
17+
pub use project_mutations::ProjectMutations;
18+
pub use streak_mutations::StreakMutations;
19+
pub use update_leaderboard::LeaderboardMutation;
2120

22-
23-
//use any mutations for leaderboard if needed
21+
//use any mutations for leaderboard if needed

src/graphql/mutations/update_leaderboard.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use async_graphql::{Context, Object, Result as GqlResult};
2-
use sqlx::{PgPool};
2+
use sqlx::PgPool;
33
use std::collections::HashMap;
44
use std::sync::Arc;
55

@@ -9,34 +9,41 @@ pub struct LeaderboardMutation;
99
#[Object]
1010
impl 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

src/graphql/queries/leaderboard_queries.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ use async_graphql::{Context, Object};
22
use sqlx::PgPool;
33
use std::sync::Arc;
44

5-
use crate::models::leaderboard::{CodeforcesStatsWithName, LeaderboardWithMember, LeetCodeStatsWithName};
5+
use crate::models::leaderboard::{
6+
CodeforcesStatsWithName, LeaderboardWithMember, LeetCodeStatsWithName,
7+
};
68

79
#[derive(Default)]
8-
pub struct LeaderboardQueries;
10+
pub struct LeaderboardQueries;
911

1012
#[Object]
1113
impl LeaderboardQueries {

src/graphql/queries/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
pub mod attendance_queries;
2+
pub mod leaderboard_queries;
23
pub mod member_queries;
34
pub mod project_queries;
45
pub mod streak_queries;
5-
pub mod leaderboard_queries;
6-
76

87
pub use attendance_queries::AttendanceQueries;
8+
pub use leaderboard_queries::LeaderboardQueries;
99
pub use member_queries::MemberQueries;
1010
pub use project_queries::ProjectQueries;
1111
pub use streak_queries::StreakQueries;
12-
pub use leaderboard_queries::LeaderboardQueries;

src/models/leaderboard.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ pub struct CodeforcesStatsWithName {
6868
pub codeforces_rating: i32,
6969
pub max_rating: i32,
7070
pub contests_participated: i32,
71-
}
71+
}

0 commit comments

Comments
 (0)