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

feat!: ranker consistent sync #26

Merged
merged 3 commits into from
Jan 20, 2024
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
2 changes: 2 additions & 0 deletions .yarn/versions/6327b22c.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
releases:
"@aoi-js/server": patch
1 change: 1 addition & 0 deletions apps/server/src/db/contest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface IContestParticipant {

export const contestParticipants = db.collection<IContestParticipant>('contestParticipants')
await contestParticipants.createIndex({ userId: 1, contestId: 1 }, { unique: true })
await contestParticipants.createIndex({ contestId: 1, updatedAt: 1, _id: 1 })

export interface IContestProblem {
problemId: BSON.UUID
Expand Down
8 changes: 8 additions & 0 deletions apps/server/src/db/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,11 @@ await solutions.createIndex(
)
await solutions.createIndex({ problemId: 1, submittedAt: -1 })
await solutions.createIndex({ contestId: 1, submittedAt: -1 })
await solutions.createIndex(
{ contestId: 1, completedAt: 1, _id: 1 },
{
partialFilterExpression: {
state: SolutionState.COMPLETED
}
}
)
47 changes: 38 additions & 9 deletions apps/server/src/routes/runner/ranklist.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BSON } from 'mongodb'
import { BSON, UUID } from 'mongodb'
import {
ContestRanklistState,
SolutionState,
Expand Down Expand Up @@ -119,7 +119,8 @@ const runnerRanklistTaskRoutes = defineRoutes(async (s) => {
schema: {
description: 'Get participants for contest',
querystring: Type.Object({
since: Type.Number()
since: Type.Number(),
lastId: Type.UUID()
}),
response: {
200: Type.Array(
Expand Down Expand Up @@ -158,9 +159,12 @@ const runnerRanklistTaskRoutes = defineRoutes(async (s) => {
.find(
{
contestId: ctx._contestId,
updatedAt: { $gt: req.query.since }
$or: [
{ updatedAt: { $gt: req.query.since } },
{ updatedAt: req.query.since, _id: { $gt: new UUID(req.query.lastId) } }
]
},
{ limit: 50, sort: { updatedAt: 1 } }
{ limit: 50, sort: { updatedAt: 1, _id: 1 } }
)
.toArray()
return list
Expand All @@ -173,8 +177,29 @@ const runnerRanklistTaskRoutes = defineRoutes(async (s) => {
schema: {
description: 'Get solutions for contest',
querystring: Type.Object({
since: Type.Number()
})
since: Type.Number(),
lastId: Type.UUID()
}),
response: {
200: Type.Array(
Type.Object({
_id: Type.UUID(),
problemId: Type.UUID(),
userId: Type.UUID(),
label: Type.String(),
problemDataHash: Type.String(),
state: Type.Integer(),
solutionDataHash: Type.String(),
score: Type.Number(),
metrics: Type.Record(Type.String(), Type.Number()),
status: Type.String(),
message: Type.String(),
createdAt: Type.Integer(),
submittedAt: Type.Integer(),
completedAt: Type.Integer()
})
)
}
}
},
async (req, rep) => {
Expand All @@ -190,12 +215,16 @@ const runnerRanklistTaskRoutes = defineRoutes(async (s) => {
{
contestId: ctx._contestId,
state: SolutionState.COMPLETED,
completedAt: { $gt: req.query.since }
$or: [
{ completedAt: { $gt: req.query.since } },
{ completedAt: req.query.since, _id: { $gt: new UUID(req.query.lastId) } }
]
},
{ limit: 50, projection: { taskId: 0 }, sort: { completedAt: 1 } }
{ limit: 50, projection: { taskId: 0 }, sort: { completedAt: 1, _id: 1 } }
)
.toArray()
return list
// Since state is COMPLETED, we can safely cast to the correct type
return list as Array<(typeof list)[number] & { submittedAt: number; completedAt: number }>
}
)

Expand Down