-
Notifications
You must be signed in to change notification settings - Fork 2
Fix/judge redistribution #346
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
Changes from all commits
fe249d7
ff8dfd8
e4307c6
be0cd95
713ea5e
b961928
0d1e82b
d96eda6
177a558
d13b25d
aa1cf17
6652219
000482a
c79a9a4
02d3e2d
8839f35
98e31fc
2b9832d
8e0fb49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import { db } from '../../jest.setup'; | ||
| import { GetJudgeToTeamPairings } from '@datalib/judgeToTeam/getJudgeToTeamPairings'; | ||
| import { ObjectId } from 'mongodb'; | ||
| import JudgeToTeam from '@typeDefs/judgeToTeam'; | ||
|
|
||
| beforeEach(async () => { | ||
| await db.collection('submissions').deleteMany({}); | ||
| }); | ||
|
|
||
| // Helper to create valid submission documents | ||
| function createSubmission(judgeId: ObjectId, teamId: ObjectId) { | ||
| return { | ||
| judge_id: judgeId, | ||
| team_id: teamId, | ||
| social_good: null, | ||
| creativity: null, | ||
| presentation: null, | ||
| scores: [], | ||
| is_scored: false, | ||
| queuePosition: null, | ||
| }; | ||
| } | ||
|
|
||
| describe('GetJudgeToTeamPairings', () => { | ||
| it('should return an empty array when no submissions exist', async () => { | ||
| const result = await GetJudgeToTeamPairings(); | ||
| expect(result.ok).toBe(true); | ||
| expect(result.body).toEqual([]); | ||
| expect(result.error).toBe(null); | ||
| }); | ||
|
|
||
| it('should return pairings with string IDs converted from ObjectIds', async () => { | ||
| const judgeId = new ObjectId(); | ||
| const teamId = new ObjectId(); | ||
|
|
||
| await db | ||
| .collection('submissions') | ||
| .insertOne(createSubmission(judgeId, teamId)); | ||
|
|
||
| const result = await GetJudgeToTeamPairings(); | ||
| expect(result.ok).toBe(true); | ||
| expect(result.body).toHaveLength(1); | ||
| expect(result.error).toBe(null); | ||
|
|
||
| const pairing = result.body?.[0]; | ||
| expect(pairing).toEqual({ | ||
| judge_id: judgeId.toString(), | ||
| team_id: teamId.toString(), | ||
| }); | ||
| }); | ||
|
|
||
| it('should return multiple pairings correctly', async () => { | ||
| const judgeId1 = new ObjectId(); | ||
| const judgeId2 = new ObjectId(); | ||
| const teamId1 = new ObjectId(); | ||
| const teamId2 = new ObjectId(); | ||
| const teamId3 = new ObjectId(); | ||
|
|
||
| await db | ||
| .collection('submissions') | ||
| .insertMany([ | ||
| createSubmission(judgeId1, teamId1), | ||
| createSubmission(judgeId1, teamId2), | ||
| createSubmission(judgeId2, teamId3), | ||
| ]); | ||
|
|
||
| const result = await GetJudgeToTeamPairings(); | ||
| expect(result.ok).toBe(true); | ||
| expect(result.body).toHaveLength(3); | ||
| expect(result.error).toBe(null); | ||
|
|
||
| const pairings = result.body as JudgeToTeam[]; | ||
| expect(pairings[0]).toEqual({ | ||
| judge_id: judgeId1.toString(), | ||
| team_id: teamId1.toString(), | ||
| }); | ||
| expect(pairings[1]).toEqual({ | ||
| judge_id: judgeId1.toString(), | ||
| team_id: teamId2.toString(), | ||
| }); | ||
| expect(pairings[2]).toEqual({ | ||
| judge_id: judgeId2.toString(), | ||
| team_id: teamId3.toString(), | ||
| }); | ||
| }); | ||
|
|
||
| it('should handle duplicate judge-team pairings', async () => { | ||
| const judgeId = new ObjectId(); | ||
| const teamId = new ObjectId(); | ||
|
|
||
| await db | ||
| .collection('submissions') | ||
| .insertMany([ | ||
| createSubmission(judgeId, teamId), | ||
| createSubmission(judgeId, teamId), | ||
| ]); | ||
|
|
||
| const result = await GetJudgeToTeamPairings(); | ||
| expect(result.ok).toBe(true); | ||
| expect(result.body).toHaveLength(2); | ||
|
|
||
| const pairings = result.body as JudgeToTeam[]; | ||
| expect(pairings[0]).toEqual(pairings[1]); | ||
| }); | ||
|
|
||
| it('should convert ObjectIds to strings for duplicate prevention comparison', async () => { | ||
| const judgeId = new ObjectId(); | ||
| const teamId = new ObjectId(); | ||
| const judgeIdString = judgeId.toString(); | ||
| const teamIdString = teamId.toString(); | ||
|
|
||
| await db | ||
| .collection('submissions') | ||
| .insertOne(createSubmission(judgeId, teamId)); | ||
|
|
||
| const result = await GetJudgeToTeamPairings(); | ||
| const pairings = result.body as JudgeToTeam[]; | ||
|
|
||
| // This test verifies that String() conversion is necessary for comparison | ||
| // in algorithms like judgesToTeamsAlgorithm.ts | ||
| expect(String(pairings[0].judge_id)).toBe(judgeIdString); | ||
| expect(String(pairings[0].team_id)).toBe(teamIdString); | ||
|
|
||
| // Simulate the duplicate check from judgesToTeamsAlgorithm.ts (lines 183-189) | ||
| const duplicateExists = pairings.some( | ||
| (entry) => | ||
| String(entry.judge_id) === judgeIdString && | ||
| String(entry.team_id) === teamIdString | ||
| ); | ||
| expect(duplicateExists).toBe(true); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import JudgeToTeam from '@typeDefs/judgeToTeam'; | ||
| import { getDatabase } from '@utils/mongodb/mongoClient.mjs'; | ||
| import { HttpError } from '@utils/response/Errors'; | ||
| import Submission from '@typeDefs/submission'; | ||
| import { ObjectId, Db } from 'mongodb'; | ||
|
|
||
| type MongoSubmission = Omit<Submission, 'judge_id' | 'team_id'> & { | ||
| judge_id: ObjectId; | ||
| team_id: ObjectId; | ||
| }; | ||
|
|
||
| export const GetJudgeToTeamPairings = async () => { | ||
| try { | ||
| const db = (await getDatabase()) as Db; | ||
| const submissions = await db | ||
| .collection<MongoSubmission>('submissions') | ||
| .find() | ||
| .toArray(); | ||
| const pairings = submissions.map((submission) => ({ | ||
| judge_id: String(submission.judge_id), | ||
| team_id: String(submission.team_id), | ||
| })); | ||
| return { ok: true, body: pairings as JudgeToTeam[], error: null }; | ||
| } catch (e) { | ||
| const error = e as HttpError; | ||
| return { ok: false, body: null, error: error.message }; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import { optedHDTracks, nonHDTracks } from '@data/tracks'; | |
|
|
||
| import { GetManyUsers } from '@datalib/users/getUser'; | ||
| import { GetManyTeams } from '@datalib/teams/getTeam'; | ||
| import { GetJudgeToTeamPairings } from '@datalib/judgeToTeam/getJudgeToTeamPairings'; | ||
|
|
||
| interface Judge { | ||
| user: User; | ||
|
|
@@ -66,7 +67,7 @@ export default async function matchAllTeams(options?: { alpha?: number }) { | |
| const teamMatchQualities: { [teamId: string]: number[] } = {}; | ||
| const teamJudgeDomainTypes: { [teamId: string]: string[] } = {}; | ||
|
|
||
| const rounds = 3; | ||
| const rounds = 2; | ||
| const ALPHA = options?.alpha ?? 4; | ||
| // Fetch all checked in judges. | ||
| const judgesResponse = await GetManyUsers({ | ||
|
|
@@ -157,6 +158,19 @@ export default async function matchAllTeams(options?: { alpha?: number }) { | |
| .filter((team) => team.tracks.length < rounds) | ||
| .map((team) => [team._id ?? '', rounds - team.tracks.length]) | ||
| ); | ||
|
|
||
| // Get previous pairings and push it to the judgeToTeam array (so that !duplicateExists is true) | ||
| const previousPairings = await GetJudgeToTeamPairings(); | ||
| if (!previousPairings.ok || !previousPairings.body) { | ||
| throw new Error( | ||
| `Failed to load existing judge-to-team pairings: ${ | ||
| previousPairings.error ?? 'Unknown error' | ||
| }` | ||
| ); | ||
| } | ||
|
Comment on lines
+164
to
+170
|
||
| const previousPairingsBody = previousPairings.body; | ||
| judgeToTeam.push(...previousPairingsBody); | ||
|
|
||
| // Main loop: process each team for each round. | ||
| for (let domainIndex = 0; domainIndex < rounds; domainIndex++) { | ||
| for (const team of modifiedTeams) { | ||
|
|
@@ -168,10 +182,14 @@ export default async function matchAllTeams(options?: { alpha?: number }) { | |
|
|
||
| let selectedJudge: Judge | undefined = undefined; | ||
| for (const judge of judgesQueue) { | ||
| // String() conversion is necessary because: | ||
| // - previousPairings from GetJudgeToTeamPairings converts ObjectIds to strings | ||
| // - judge.user._id and team._id are ObjectIds that need .toString() | ||
| // - Comparing without String() would cause false negatives in duplicate detection | ||
| const duplicateExists = judgeToTeam.some( | ||
| (entry) => | ||
| entry.judge_id === judge.user._id?.toString() && | ||
| entry.team_id === team._id?.toString() | ||
| String(entry.judge_id) === judge.user._id?.toString() && | ||
| String(entry.team_id) === team._id?.toString() | ||
| ); | ||
ReehalS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!duplicateExists) { | ||
| selectedJudge = judge; | ||
|
|
@@ -212,6 +230,25 @@ export default async function matchAllTeams(options?: { alpha?: number }) { | |
| shuffleArray(modifiedTeams); | ||
| } | ||
|
|
||
| // Remove the previous pairings without relying on array insertion order. | ||
| if (previousPairingsBody.length > 0) { | ||
| const previousPairingKeySet = new Set( | ||
| previousPairingsBody.map((pairing) => { | ||
| const judgeId = String(pairing.judge_id); | ||
| const teamId = String(pairing.team_id); | ||
| return `${judgeId}::${teamId}`; | ||
| }) | ||
| ); | ||
| const filteredJudgeToTeam = judgeToTeam.filter((entry) => { | ||
| const judgeId = String(entry.judge_id); | ||
| const teamId = String(entry.team_id); | ||
| const key = `${judgeId}::${teamId}`; | ||
| return !previousPairingKeySet.has(key); | ||
| }); | ||
| judgeToTeam.length = 0; | ||
| judgeToTeam.push(...filteredJudgeToTeam); | ||
| } | ||
|
|
||
| console.log('No. of judgeToTeam:', judgeToTeam.length); | ||
|
|
||
| const judgeAssignments = judgesQueue.map((judge) => judge.teamsAssigned); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.