mentorenwahl/backend/src/backend/worker/jobs/assignment_job.cr

187 lines
6.7 KiB
Crystal
Raw Normal View History

2022-04-14 14:44:49 +00:00
# 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
2022-12-29 21:24:29 +00:00
class AssignmentJob < Mosquito::QueuedJob
2022-11-21 18:48:53 +00:00
alias TeacherVote = {student: Int32, priority: Int32}
alias Assignment = {teacher: Int32, priority: Int32}
2023-01-17 05:56:19 +00:00
def rescheduleable?
false
end
2022-04-14 14:44:49 +00:00
# :ditto:
def perform : Nil
2023-03-05 12:52:30 +00:00
if Db::Config.query.where { active }.first!.can_vote
log "Voting still allowed, skipping assignment"
fail
end
2022-07-28 12:05:10 +00:00
teacher_count = Db::Teacher.query.count
student_count = Db::Student.query.count
vote_count = Db::Vote.query.count
2022-11-21 18:48:53 +00:00
if teacher_count == 0
2022-04-14 14:44:49 +00:00
log "No teachers found, skipping assignment"
fail
2022-07-28 12:05:10 +00:00
elsif student_count == 0
2022-04-14 14:44:49 +00:00
log "No students found, skipping assignment"
fail
2022-07-28 12:05:10 +00:00
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
2022-04-14 14:44:49 +00:00
log "Not all students voted, skipping assignment"
fail
2023-03-05 12:52:30 +00:00
end
if Db::Teacher.query.sum(:max_students, Int64) < student_count
log "Capacity too low, teachers need to adopt more students, skipping assignment"
2022-11-21 18:48:53 +00:00
fail
2022-04-14 14:44:49 +00:00
end
2022-11-21 18:48:53 +00:00
teachers = Db::Teacher.query
2022-07-28 12:05:10 +00:00
.where do
2022-11-21 18:48:53 +00:00
raw("EXISTS (SELECT 1 FROM teacher_votes WHERE teacher_id = teachers.id)") &
2022-12-17 11:27:31 +00:00
(max_students > 0)
2022-11-21 18:48:53 +00:00
end
2022-12-17 11:27:31 +00:00
.with_teacher_votes
.to_a
2022-12-29 21:24:29 +00:00
teacher_ids = teachers.map(&.id)
teacher_max_students = Hash.zip(teacher_ids, teachers.map(&.max_students))
2022-12-17 11:27:31 +00:00
teacher_votes : Hash(Int32, Array(TeacherVote)) = Hash.zip(
2022-12-29 21:24:29 +00:00
teacher_ids,
2022-12-17 11:27:31 +00:00
teachers.map do |t|
t.teacher_votes.map do |tv|
{
student: tv.vote.student.id,
priority: tv.priority,
}
end
end
)
2023-03-05 12:52:30 +00:00
log "Got teachers"
2022-12-17 11:27:31 +00:00
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
2022-12-29 21:24:29 +00:00
.select! { |tv| teacher_votes.has_key?(tv.teacher.id) }
2022-12-17 11:27:31 +00:00
.map do |tv|
{
2022-12-29 21:24:29 +00:00
teacher: tv.teacher.id,
priority: tv.priority,
2022-12-17 11:27:31 +00:00
}
end
end
)
votes_a = votes.to_a
2023-03-05 12:52:30 +00:00
log "Got students' votes"
2022-12-17 11:27:31 +00:00
2023-02-05 16:25:03 +00:00
best : {assignment: Hash(Int32, Assignment), priority_score: Int64, teacher_score: Int64}? = nil
2022-12-29 21:24:29 +00:00
empty_assignment_count = Hash.zip(teachers.map(&.id), [0] * teachers.size)
2023-03-05 12:52:30 +00:00
log "Starting assignment"
valid_count = 0_u64
invalid_count = 0_u64
max_span = Time::Span.new(minutes: Backend.config.assignment_run_time)
start_time = Time.utc
loop do
2022-12-17 11:27:31 +00:00
assignment = {} of Int32 => Assignment
2022-12-29 21:24:29 +00:00
assignment_count = empty_assignment_count.clone
2022-12-17 11:27:31 +00:00
votes_a.shuffle(Random::Secure).each do |s, tvs|
2022-12-29 21:24:29 +00:00
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
2022-12-17 11:27:31 +00:00
end
end
end
2023-03-05 12:52:30 +00:00
if assignment.size != student_count
invalid_count += 1
log "Invalid combination, next (#{assignment.size} != #{student_count}, valid: #{valid_count} invalid: #{invalid_count})"
else
log "Valid combination (valid: #{valid_count} invalid: #{invalid_count})!"
2023-03-03 19:04:57 +00:00
2023-03-05 12:52:30 +00:00
priority_score = 0_i64
assignment.each do |_, a|
priority_score += a[:priority] ** 2
end
2022-12-29 21:24:29 +00:00
2023-03-05 12:52:30 +00:00
teacher_score = 0_i64
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
2022-12-17 11:27:31 +00:00
2023-03-05 12:52:30 +00:00
valid_count += 1
2022-12-17 11:27:31 +00:00
end
2023-03-05 12:52:30 +00:00
break if (Time.utc - start_time) > max_span && valid_count > 0
2022-12-17 11:27:31 +00:00
end
pp! best
2023-03-05 12:52:30 +00:00
log "Saving best assignment into database"
2023-02-05 16:25:03 +00:00
Db::Assignment.query.where { active }.to_update.set(active: false).execute
assignment_id = Db::Assignment.create!({
active: true,
priority_score: best.not_nil![:priority_score],
teacher_score: best.not_nil![:teacher_score],
2023-03-03 19:04:57 +00:00
}).id
2023-02-05 16:25:03 +00:00
Db::StudentAssignment.import(
best.not_nil![:assignment].map do |s, a|
Db::StudentAssignment.new({
assignment_id: assignment_id,
student_id: s,
teacher_id: a[:teacher],
})
end
)
2022-04-14 14:44:49 +00:00
end
end
end
end
end