Skip to content

Commit

Permalink
use tables in leaderboard schema (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
b9r5 authored Dec 12, 2024
1 parent db3f5fc commit 77f81cb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/discord-cluster-manager/cogs/leaderboard_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ async def submit_github(
# Read and convert reference code
reference_code = None
with self.bot.leaderboard_db as db:
# TODO: query that gets reference code given leaderboard name
leaderboard_item = db.get_leaderboard(leaderboard_name)
if not leaderboard_item:
await interaction.response.send_message(
Expand Down Expand Up @@ -278,6 +279,7 @@ async def get_leaderboard_submissions(
dtype: app_commands.Choice[str] = "fp32",
):
with self.bot.leaderboard_db as db:
# TODO: query that gets leaderboard id given leaderboard name
leaderboard_id = db.get_leaderboard(leaderboard_name)["id"]
if not leaderboard_id:
await interaction.response.send_message(
Expand Down
41 changes: 23 additions & 18 deletions src/discord-cluster-manager/leaderboard_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def create_leaderboard(self, leaderboard: LeaderboardItem):
try:
self.cursor.execute(
"""
INSERT INTO leaderboard (name, deadline, reference_code)
INSERT INTO leaderboard.problem (name, deadline, reference_code)
VALUES (%s, %s, %s)
""",
(
Expand All @@ -121,15 +121,15 @@ def create_submission(self, submission: SubmissionItem):
try:
self.cursor.execute(
"""
INSERT INTO submissions (submission_name, submission_time, leaderboard_id, code, user_id, submission_score)
VALUES (%s, %s, %s, %s, %s, %s)
INSERT INTO leaderboard.submission (problem_id, name, user_id, code, submission_time, score)
VALUES ((SELECT id FROM leaderboard.problem WHERE name = %s), %s, %s, %s, %s, %s)
""",
(
submission["submission_name"],
submission["submission_time"],
submission["leaderboard_name"],
submission["code"],
submission["submission_name"],
submission["user_id"],
submission["code"],
submission["submission_time"],
submission["submission_score"],
),
)
Expand All @@ -139,7 +139,7 @@ def create_submission(self, submission: SubmissionItem):
self.connection.rollback() # Ensure rollback if error occurs

def get_leaderboards(self) -> list[LeaderboardItem]:
self.cursor.execute("SELECT * FROM leaderboard")
self.cursor.execute("SELECT id, name, deadline, reference_code FROM leaderboard.problem")

return [
LeaderboardItem(id=lb[0], name=lb[1], deadline=lb[2], reference_code=lb[3])
Expand All @@ -148,7 +148,8 @@ def get_leaderboards(self) -> list[LeaderboardItem]:

def get_leaderboard(self, leaderboard_name: str) -> int | None:
self.cursor.execute(
"SELECT * FROM leaderboard WHERE name = %s", (leaderboard_name,)
"SELECT id, name, deadline, reference_code FROM leaderboard.problem WHERE name = %s",
(leaderboard_name,)
)

res = self.cursor.fetchone()
Expand All @@ -163,22 +164,26 @@ def get_leaderboard(self, leaderboard_name: str) -> int | None:
def get_leaderboard_submissions(
self, leaderboard_name: str
) -> list[SubmissionItem]:
"""
TODO: Change these all to be IDs instead
"""
self.cursor.execute(
"SELECT * FROM submissions WHERE leaderboard_id = %s ORDER BY submission_score ASC",
"""
SELECT s.name, s.user_id, s.code, s.submission_time, s.score
FROM leaderboard.submission s
JOIN leaderboard.problem p
ON s.problem_id = p.id
WHERE p.name = %s
ORDER BY s.score ASC
""",
(leaderboard_name,),
)

return [
SubmissionItem(
leaderboard_name=submission[1],
submission_name=submission[2],
user_id=submission[3],
code=submission[4],
submission_time=submission[5],
submission_score=submission[6],
leaderboard_name=leaderboard_name,
submission_name=submission[0],
user_id=submission[1],
code=submission[2],
submission_time=submission[3],
submission_score=submission[4],
)
for submission in self.cursor.fetchall()
]
Expand Down

0 comments on commit 77f81cb

Please sign in to comment.