Skip to content

Commit

Permalink
Add function for finding next candidate merge class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carifio24 committed Nov 4, 2024
1 parent e48ade6 commit f981437
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/stories/hubbles_law/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,35 @@ export async function eligibleClassesForMerge(database: Sequelize, classID: numb
`, { type: QueryTypes.SELECT }) as Promise<Class[]>;
}

export async function findClassForMerge(database: Sequelize): Promise<Class> {
// The SQL is complicated enough here; doing this with the ORM
// will probably be unreadable
const result = await database.query(
`
SELECT
id,
COUNT(*) as group_count,
IFNULL(group_id, UUID()) AS unique_gid,
IFNULL(group_id, 0) AS is_group
FROM
Classes
LEFT OUTER JOIN
HubbleClassMergeGroups ON Classes.id = HubbleClassMergeGroups.class_id
INNER JOIN
(
SELECT * FROM StudentsClasses GROUP BY class_id
HAVING COUNT(student_id) >= 12
) C
ON Classes.id = C.class_id
GROUP BY unique_gid
ORDER BY is_group ASC, group_count ASC
LIMIT 1;
`,
{ type: QueryTypes.SELECT }
) as Class[];
return result[0];
}

// Try and merge the class with the given ID with another class such that the total size is above the threshold
// We say "try" because if a client doesn't know that the merge has already occurred, we may get
// multiple such requests from different student clients.
Expand Down

0 comments on commit f981437

Please sign in to comment.