Skip to content

Commit

Permalink
feat!: ranker consistent sync (#26)
Browse files Browse the repository at this point in the history
* feat!: using consistent sort for ranker sync

Breaks current ranker implementations

* chore: bump version

* chore: add types to solution sync api
  • Loading branch information
thezzisu authored Jan 20, 2024
1 parent 195308d commit cf2c653
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
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

0 comments on commit cf2c653

Please sign in to comment.