Skip to content

Commit

Permalink
fix challenge and project leaderboards
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinBeczak committed Sep 1, 2024
1 parent c1e212c commit bbc21f2
Show file tree
Hide file tree
Showing 4 changed files with 549 additions and 0 deletions.
112 changes: 112 additions & 0 deletions app/org/maproulette/framework/controller/LeaderboardController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,118 @@ class LeaderboardController @Inject() (
}
}

/**
* Gets the top scoring users, based on task completion, over the given
* number of months (or using start and end dates). Included with each user is their top challenges
* (by amount of activity).
*
* @param monthDuration the number of months to consider for the leaderboard
* @param maxChallengesInList the maximum number of top challenges to include for each user
* @param limit the limit on the number of users returned
* @param offset the number of users to skip before starting to return results (for pagination)
* @return Top-ranked users with scores based on task completion activity
*/
def getMainLeaderboard(
monthDuration: Int,
maxChallengesInList: Int,
limit: Int,
offset: Int
): Action[AnyContent] = Action.async { implicit request =>
this.sessionManager.userAwareRequest { implicit user =>
Ok(
Json.toJson(
this.service.getMainLeaderboard(monthDuration, maxChallengesInList, limit, offset)
)
)
}
}

/**
* Gets the top scoring users, based on task completion, over the given
* number of months (or using start and end dates). Included with each user is their top challenges
* (by amount of activity).
*
* @param id the ID of the challenge
* @param monthDuration the number of months to consider for the leaderboard
* @param maxChallengesInList the maximum number of top challenges to include for each user
* @param limit the limit on the number of users returned
* @param offset the number of users to skip before starting to return results (for pagination)
* @return Top-ranked users with scores based on task completion activity
*/
def getChallengeLeaderboard(
id: Int,
monthDuration: Int,
maxChallengesInList: Int,
limit: Int,
offset: Int
): Action[AnyContent] = Action.async { implicit request =>
this.sessionManager.userAwareRequest { implicit user =>
Ok(
Json.toJson(
this.service
.getChallengeLeaderboard(id, monthDuration, maxChallengesInList, limit, offset)
)
)
}
}

/**
* Gets the top scoring users, based on task completion, over the given
* number of months (or using start and end dates). Included with each user is their top challenges
* (by amount of activity).
*
* @param id the ID of the project
* @param monthDuration the number of months to consider for the leaderboard
* @param maxChallengesInList the maximum number of top challenges to include for each user
* @param limit the limit on the number of users returned
* @param offset the number of users to skip before starting to return results (for pagination)
* @return Top-ranked users with scores based on task completion activity
*/
def getProjectLeaderboard(
id: Int,
monthDuration: Int,
maxChallengesInList: Int,
limit: Int,
offset: Int
): Action[AnyContent] = Action.async { implicit request =>
this.sessionManager.userAwareRequest { implicit user =>
Ok(
Json.toJson(
this.service.getProjectLeaderboard(id, monthDuration, maxChallengesInList, limit, offset)
)
)
}
}

/**
* Gets the top scoring users, based on task completion, over the given
* number of months (or using start and end dates). Included with each user is their top challenges
* (by amount of activity).
*
* @param country the country to filter the leaderboard by
* @param monthDuration the number of months to consider for the leaderboard
* @param maxChallengesInList the maximum number of top challenges to include for each user
* @param limit the limit on the number of users returned
* @param offset the number of users to skip before starting to return results (for pagination)
* @return Top-ranked users with scores based on task completion activity
*/
def getCountryLeaderboard(
country: String,
monthDuration: Int,
maxChallengesInList: Int,
limit: Int,
offset: Int
): Action[AnyContent] = Action.async { implicit request =>
this.sessionManager.userAwareRequest { implicit user =>
Ok(
Json.toJson(
this.service
.getCountryLeaderboard(country, monthDuration, maxChallengesInList, limit, offset)
)
)
}
}

/**
* Gets the leaderboard ranking for a user, based on task completion, over
* the given number of months (or start and end dates). Included with the user is their top challenges
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,63 @@ class LeaderboardRepository @Inject() (override val db: Database) extends Reposi
}
}

/**
* Queries the user_leaderboard table
*
* @param query
* @param getTopChallengesBlock - function to return the top challenges for a user id
* @return List of LeaderboardUsers
**/
def queryMainLeaderboard(
query: Query,
getTopChallengesBlock: Long => List[LeaderboardChallenge]
): List[LeaderboardUser] = {
withMRConnection { implicit c =>
query
.build(
"""
SELECT *,
COALESCE(user_leaderboard.completed_tasks, 0) as completed_tasks,
COALESCE(user_leaderboard.avg_time_spent, 0) as avg_time_spent
FROM user_leaderboard
"""
)
.as(this.userLeaderboardParser(getTopChallengesBlock).*)
}
}

/**
* Queries the user_leaderboard table
*
* @param query
* @param getTopChallengesBlock - function to return the top challenges for a user id
* @return List of LeaderboardUsers
**/
def queryChallengeLeaderboard(
query: Query,
getTopChallengesBlock: Long => List[LeaderboardChallenge]
): List[LeaderboardUser] = {
withMRConnection { implicit c =>
query
.build(
"""
SELECT * FROM user_top_challengess
"""
)
.as(this.leaderboardChallengeParser.*)
}
}

def queryLeaderboardChallenges(query: Query): List[LeaderboardChallenge] = {
withMRConnection { implicit c =>
query
.build(
"SELECT challenge_id, challenge_name, activity FROM user_top_challenges"
)
.as(this.leaderboardChallengeParser.*)
}
}

/**
* Queries the user_leaderboard table with ranking sql
*
Expand Down
Loading

0 comments on commit bbc21f2

Please sign in to comment.