Skip to content
Merged
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
19 changes: 19 additions & 0 deletions src/kernelbot/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,25 @@ async def admin_delete_leaderboard(
return {"status": "ok", "leaderboard": leaderboard_name, "force": force}


@app.get("/admin/leaderboards/{leaderboard_name}/submissions")
async def admin_list_leaderboard_submissions(
leaderboard_name: str,
_: Annotated[None, Depends(require_admin)],
db_context=Depends(get_db),
limit: int = 100,
offset: int = 0,
) -> dict:
with db_context as db:
submissions = db.get_leaderboard_submission_history(leaderboard_name, limit, offset)
return {
"status": "ok",
"leaderboard": leaderboard_name,
"limit": limit,
"offset": offset,
Comment on lines +705 to +706
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Return clamped pagination values in response

get_leaderboard_submission_history clamps limit to [1, 1000] and offset to >=0, but this handler echoes the raw query values, so the response metadata can disagree with what the DB actually applied (for example, limit=5000 returns at most 1000 rows while reporting limit: 5000, or offset=-10 reports -10 while querying from 0). This can break admin pagination clients that trust the returned limit/offset to compute the next page.

Useful? React with 👍 / 👎.

"submissions": submissions,
}


@app.delete("/admin/submissions/{submission_id}")
async def admin_delete_submission(
submission_id: int,
Expand Down
49 changes: 49 additions & 0 deletions src/libkernelbot/leaderboard_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,55 @@ def get_user_submissions(
logger.exception("Error fetching user submissions for user %s", user_id, exc_info=e)
raise KernelBotError("Error fetching user submissions") from e

def get_leaderboard_submission_history(
self,
leaderboard_name: str,
limit: int = 100,
offset: int = 0,
) -> list[dict]:
"""Get all submissions for a leaderboard with admin-oriented metadata."""
limit = max(1, min(limit, 1000))
offset = max(0, offset)

try:
self.get_leaderboard_id(leaderboard_name)
self.cursor.execute(
"""
SELECT s.id, lb.name, s.file_name, s.user_id, ui.user_name,
s.submission_time, s.done
FROM leaderboard.submission s
JOIN leaderboard.leaderboard lb ON s.leaderboard_id = lb.id
LEFT JOIN leaderboard.user_info ui ON ui.id = s.user_id
WHERE lb.name = %s
ORDER BY s.submission_time DESC, s.id DESC
LIMIT %s OFFSET %s
""",
(leaderboard_name, limit, offset),
)
return [
{
"id": row[0],
"leaderboard_name": row[1],
"file_name": row[2],
"user_id": row[3],
"user_name": row[4],
"submission_time": row[5],
"done": row[6],
}
for row in self.cursor.fetchall()
]
except KernelBotError:
self.connection.rollback()
raise
except psycopg2.Error as e:
self.connection.rollback()
logger.exception(
"Error fetching submissions for leaderboard %s",
leaderboard_name,
exc_info=e,
)
raise KernelBotError("Error fetching leaderboard submissions") from e

def get_submission_by_id(self, submission_id: int) -> Optional["SubmissionItem"]:
query = """
SELECT s.leaderboard_id, lb.name, s.file_name, s.user_id,
Expand Down
60 changes: 60 additions & 0 deletions tests/test_admin_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,66 @@ def test_admin_stats_with_leaderboard_name(self, test_client, mock_backend):
class TestAdminSubmissions:
"""Test admin submission endpoints."""

def test_list_leaderboard_submissions(self, test_client, mock_backend):
"""GET /admin/leaderboards/{name}/submissions returns all submission metadata."""
mock_backend.db.__enter__ = MagicMock(return_value=mock_backend.db)
mock_backend.db.__exit__ = MagicMock(return_value=None)
mock_backend.db.get_leaderboard_submission_history = MagicMock(return_value=[
{
"id": 123,
"leaderboard_name": "test-lb",
"file_name": "submission.py",
"user_id": "42",
"user_name": "alice",
"submission_time": "2026-04-07T12:00:00Z",
"done": True,
},
{
"id": 122,
"leaderboard_name": "test-lb",
"file_name": "submission_old.py",
"user_id": "43",
"user_name": "bob",
"submission_time": "2026-04-07T11:00:00Z",
"done": False,
},
])

response = test_client.get(
"/admin/leaderboards/test-lb/submissions?limit=50&offset=10",
headers={"Authorization": "Bearer test_token"},
)
assert response.status_code == 200
assert response.json() == {
"status": "ok",
"leaderboard": "test-lb",
"limit": 50,
"offset": 10,
"submissions": [
{
"id": 123,
"leaderboard_name": "test-lb",
"file_name": "submission.py",
"user_id": "42",
"user_name": "alice",
"submission_time": "2026-04-07T12:00:00Z",
"done": True,
},
{
"id": 122,
"leaderboard_name": "test-lb",
"file_name": "submission_old.py",
"user_id": "43",
"user_name": "bob",
"submission_time": "2026-04-07T11:00:00Z",
"done": False,
},
],
}
mock_backend.db.get_leaderboard_submission_history.assert_called_once_with(
"test-lb", 50, 10
)

def test_get_submission(self, test_client, mock_backend):
"""GET /admin/submissions/{id} returns submission."""
mock_backend.db.__enter__ = MagicMock(return_value=mock_backend.db)
Expand Down
Loading