mentorenwahl/backend/src/backend/worker/jobs/assignment_job.cr
Dominic Grimm 8055a5e4db
Some checks failed
continuous-integration/drone/push Build is failing
Update
2023-01-17 06:56:19 +01:00

145 lines
5.3 KiB
Crystal

# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes.
# Copyright (C) 2022 Dominic Grimm
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
module Backend
module Worker
module Jobs
# Assigns students to teachers when all students voted
class AssignmentJob < Mosquito::QueuedJob
alias TeacherVote = {student: Int32, priority: Int32}
alias Assignment = {teacher: Int32, priority: Int32}
def rescheduleable?
false
end
# :ditto:
def perform : Nil
teacher_count = Db::Teacher.query.count
student_count = Db::Student.query.count
vote_count = Db::Vote.query.count
if teacher_count == 0
log "No teachers found, skipping assignment"
fail
elsif student_count == 0
log "No students found, skipping assignment"
fail
elsif student_count != Db::User.query.where(role: Db::UserRole::Student).count
log "Not all students registered, skipping assignment"
fail
elsif vote_count < student_count
log "Not all students voted, skipping assignment"
fail
elsif Db::Assignment.query.count > 0
log "Assignment has already run, skipping another assignment"
fail
end
teachers = Db::Teacher.query
.where do
raw("EXISTS (SELECT 1 FROM teacher_votes WHERE teacher_id = teachers.id)") &
(max_students > 0)
end
.with_teacher_votes
.to_a
teacher_ids = teachers.map(&.id)
teacher_max_students = Hash.zip(teacher_ids, teachers.map(&.max_students))
teacher_votes : Hash(Int32, Array(TeacherVote)) = Hash.zip(
teacher_ids,
teachers.map do |t|
t.teacher_votes.map do |tv|
{
student: tv.vote.student.id,
priority: tv.priority,
}
end
end
)
students = Db::Student.query
.with_vote(&.with_teacher_votes(&.order_by(priority: :desc)))
.to_a
student_ids = students.map(&.id)
votes = Hash.zip(
student_ids,
students.map do |s|
s.vote.not_nil!.teacher_votes
.to_a
.select! { |tv| teacher_votes.has_key?(tv.teacher.id) }
.map do |tv|
{
teacher: tv.teacher.id,
priority: tv.priority,
}
end
end
)
votes_a = votes.to_a
best : {assignment: Hash(Int32, Assignment), priority_score: UInt32, teacher_score: UInt32}? = nil
empty_assignment_count = Hash.zip(teachers.map(&.id), [0] * teachers.size)
Backend.config.assignment_possibility_count.times.each do
assignment = {} of Int32 => Assignment
assignment_count = empty_assignment_count.clone
votes_a.shuffle(Random::Secure).each do |s, tvs|
tvs.each do |tv|
if assignment_count[tv[:teacher]] < teacher_max_students[tv[:teacher]]
if assignment[s]?.nil?
assignment_count[tv[:teacher]] += 1
assignment[s] = {teacher: tv[:teacher], priority: tv[:priority]}
else
assignment_count[assignment[s][:teacher]] -= 1
assignment_count[tv[:teacher]] += 1
assignment[s] = {teacher: tv[:teacher], priority: tv[:priority]}
end
end
end
end
priority_score = 0_u32
assignment.each do |_, a|
priority_score += a[:priority] ** 2
end
teacher_score = 0_u32
assignment_count.each do |t, c|
teacher_score += (teacher_max_students[t] ** c) * teacher_max_students[t]
end
if best.nil?
best = {
assignment: assignment,
priority_score: priority_score,
teacher_score: teacher_score,
}
elsif priority_score < best.not_nil![:priority_score] && teacher_score < best.not_nil![:teacher_score]
best = {
assignment: assignment,
priority_score: priority_score,
teacher_score: teacher_score,
}
end
end
pp! best
Db::Assignment.import(best.not_nil![:assignment].map { |s, a| Db::Assignment.new({student_id: s, teacher_id: a[:teacher]}) })
end
end
end
end
end