Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue#27 Seasonal Leaderboard #287

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 86 additions & 10 deletions challenges/src/endpoints/leaderboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use poem_openapi::{
};
use schemas::challenges::leaderboard::{Leaderboard, Rank};
use uuid::Uuid;
use chrono::NaiveDateTime;

use super::Tags;
use crate::services::leaderboard::{
Expand All @@ -23,25 +24,71 @@ pub struct LeaderboardEndpoints {
pub cache: Cache<JsonFormatter>,
}

fn get_current_quarter_range() -> (NaiveDateTime, NaiveDateTime) {
let now = Local::now();
let year = now.year();
let quarter = (now.month() - 1) / 3;

let start_month = quarter * 3 + 1;
let end_month = start_month + 3;

let start = NaiveDateTime::new(
NaiveDate::from_ymd_opt(year, start_month, 1).unwrap(),
NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
);

let end = if end_month > 12 {
NaiveDateTime::new(
NaiveDate::from_ymd_opt(year + 1, 1, 1).unwrap(),
NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
)
} else {
NaiveDateTime::new(
NaiveDate::from_ymd_opt(year, end_month, 1).unwrap(),
NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
)
};

(start, end)
}

#[OpenApi(tag = "Tags::Leaderboard")]
impl LeaderboardEndpoints {
#[oai(path = "/leaderboard", method = "get")]
async fn get_leaderboard(
&self,
#[oai(validator(maximum(value = "100")))] limit: Query<u64>,
offset: Query<u64>,
current_quarter: Query<Option<bool>>,
_auth: VerifiedUserAuth,
) -> GetLeaderboard::Response<VerifiedUserAuth> {
GetLeaderboard::ok(get_global_leaderboard(&self.state.services, limit.0, offset.0).await?)
let date_range = if current_quarter.0.unwrap_or(false) {
Some(get_current_quarter_range())
} else {
None
};

GetLeaderboard::ok(
get_global_leaderboard(&self.state.services, limit.0, offset.0, date_range).await?
)
}

#[oai(path = "/leaderboard/:user_id", method = "get")]
async fn get_leaderboard_user(
&self,
user_id: Query<Uuid>,
current_quarter: Query<Option<bool>>,
_auth: VerifiedUserAuth,
) -> GetLeaderboardUser::Response<VerifiedUserAuth> {
GetLeaderboardUser::ok(get_global_leaderboard_user(&self.state.services, user_id.0).await?)
let date_range = if current_quarter.0.unwrap_or(false) {
Some(get_current_quarter_range())
} else {
None
};

GetLeaderboardUser::ok(
get_global_leaderboard_user(&self.state.services, user_id.0, date_range).await?
)
}

#[oai(path = "/leaderboard/by-task/:task_id", method = "get")]
Expand All @@ -50,16 +97,23 @@ impl LeaderboardEndpoints {
task_id: Path<Uuid>,
#[oai(validator(maximum(value = "100")))] limit: Query<u64>,
offset: Query<u64>,
current_quarter: Query<Option<bool>>,
db: Data<&DbTxn>,
_auth: VerifiedUserAuth,
) -> GetTaskLeaderboard::Response<VerifiedUserAuth> {
let date_range = if current_quarter.0.unwrap_or(false) {
Some(get_current_quarter_range())
} else {
None
};

let leaderboard = self
.cache
.cached_result(
key!(task_id.0, limit.0, offset.0),
key!(task_id.0, limit.0, offset.0, current_quarter.0),
&[],
Some(Duration::from_secs(10)),
|| get_task_leaderboard(&db, &self.state.services, task_id.0, limit.0, offset.0),
|| get_task_leaderboard(&db, &self.state.services, task_id.0, limit.0, offset.0, date_range),
)
.await??;
GetTaskLeaderboard::ok(leaderboard)
Expand All @@ -70,16 +124,23 @@ impl LeaderboardEndpoints {
&self,
task_id: Path<Uuid>,
user_id: Path<Uuid>,
current_quarter: Query<Option<bool>>,
db: Data<&DbTxn>,
_auth: VerifiedUserAuth,
) -> GetTaskLeaderboardUser::Response<VerifiedUserAuth> {
let date_range = if current_quarter.0.unwrap_or(false) {
Some(get_current_quarter_range())
} else {
None
};

let rank = self
.cache
.cached_result(
key!(task_id.0, user_id.0),
key!(task_id.0, user_id.0, current_quarter.0),
&[],
Some(Duration::from_secs(10)),
|| get_task_leaderboard_user(&db, task_id.0, user_id.0),
|| get_task_leaderboard_user(&db, task_id.0, user_id.0, date_range),
)
.await??;
GetTaskLeaderboardUser::ok(rank)
Expand All @@ -91,13 +152,20 @@ impl LeaderboardEndpoints {
language: Path<String>,
#[oai(validator(maximum(value = "100")))] limit: Query<u64>,
offset: Query<u64>,
current_quarter: Query<Option<bool>>,
db: Data<&DbTxn>,
_auth: VerifiedUserAuth,
) -> GetLanguageLeaderboard::Response<VerifiedUserAuth> {
let date_range = if current_quarter.0.unwrap_or(false) {
Some(get_current_quarter_range())
} else {
None
};

let leaderboard = self
.cache
.cached_result(
key!(&language.0, limit.0, offset.0),
key!(&language.0, limit.0, offset.0, current_quarter.0),
&[],
Some(Duration::from_secs(10)),
|| {
Expand All @@ -106,7 +174,8 @@ impl LeaderboardEndpoints {
&self.state.services,
&language.0,
limit.0,
offset.0,
offset.0,
date_range
)
},
)
Expand All @@ -119,16 +188,23 @@ impl LeaderboardEndpoints {
&self,
language: Path<String>,
user_id: Path<Uuid>,
current_quarter: Query<Option<bool>>,
db: Data<&DbTxn>,
_auth: VerifiedUserAuth,
) -> GetLanguageLeaderboardUser::Response<VerifiedUserAuth> {
let date_range = if current_quarter.0.unwrap_or(false) {
Some(get_current_quarter_range())
} else {
None
};

let rank = self
.cache
.cached_result(
key!(&language.0, user_id.0),
key!(&language.0, user_id.0, current_quarter.0),
&[],
Some(Duration::from_secs(10)),
|| get_language_leaderboard_user(&db, &language.0, user_id.0),
|| get_language_leaderboard_user(&db, &language.0, user_id.0, date_range),
)
.await??;
GetLanguageLeaderboardUser::ok(rank)
Expand Down
33 changes: 31 additions & 2 deletions challenges/src/services/leaderboard/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,29 @@ use futures::future::try_join_all;
use lib::services::Services;
use schemas::challenges::leaderboard::{Leaderboard, Rank};
use uuid::Uuid;
use chrono::NaiveDateTime;

use super::resolve_user;

/// Get the global leaderboard with optional date range filtering
pub async fn get_global_leaderboard(
services: &Services,
limit: u64,
offset: u64,
date_range: Option<(NaiveDateTime, NaiveDateTime)>,
) -> anyhow::Result<Leaderboard> {
let leaderboard = services.skills.get_leaderboard(limit, offset).await?;
let leaderboard = match date_range {
Some((start_date, end_date)) => {
services.skills
.get_leaderboard_with_date_range(limit, offset, start_date, end_date)
.await?
}
None => {
services.skills
.get_leaderboard(limit, offset)
.await?
}
};

Ok(Leaderboard {
leaderboard: try_join_all(
Expand All @@ -24,9 +38,24 @@ pub async fn get_global_leaderboard(
})
}

/// Get a specific user's global rank with optional date range filtering
pub async fn get_global_leaderboard_user(
services: &Services,
user_id: Uuid,
date_range: Option<(NaiveDateTime, NaiveDateTime)>,
) -> anyhow::Result<Rank> {
Ok(services.skills.get_leaderboard_user(user_id).await?.into())
let rank = match date_range {
Some((start_date, end_date)) => {
services.skills
.get_leaderboard_user_with_date_range(user_id, start_date, end_date)
.await?
}
None => {
services.skills
.get_leaderboard_user(user_id)
.await?
}
};

Ok(rank.into())
}
Loading
Loading