Skip to content

Commit

Permalink
Add API action for zeroing result for users without solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
just806me committed Feb 8, 2024
1 parent cb8bfde commit 502e7c6
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions app/channels/api_channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,34 @@ def zero_results data
end
end

def finish data
def zero_no_solution data
return unless scoring_open data

task = Task.find task_id
force_close_and_finish! task
dispatch_self 'notifications/push', kind: 'info', message: 'Zeroing results for users without solutions …'

task.update_column :scoring_open, false
RedisLockManager.release_all
dispatch_all 'locks/load', RedisLockManager.all
dispatch_all 'results/load', CriterionUserResult.includes(:user).where(criterion: task_criterions)
dispatch_all 'comments/load', Comment.includes(:user).where(task_id:)
dispatch_all 'app/finish'
users_without_any_solution = Set.new task_users.where.missing(:solutions).pluck(:id)
begin
ActiveRecord::Base.transaction do
task.contest.users.find_each do |user|
next if user.solutions.exists?(task_id:)

task.criterions.each { CriterionUserResult.create_or_find_by!(user:, criterion: _1).update!(value: 0) }
comment = users_without_any_solution.include?(user.id) ? 'папка відсутня' : 'файл відсутній'
Comment.create_or_find_by!(user:, task_id:).update!(value: comment)
end
end
ensure
reopen! task
end
end

def finish data
return unless scoring_open data

task = Task.find task_id
force_close_and_finish! task
dispatch_self 'notifications/push', kind: 'info', message: 'Calculating results …'

users_without_result = []
Expand All @@ -170,8 +186,7 @@ def finish data
raise "Users that missing some result: #{users_without_result.to_sentence}" unless users_without_result.empty?
end
rescue StandardError => e
task.update_column :scoring_open, true
dispatch_self 'app/ready', ready_info
reopen! task
raise e
else
dispatch_self 'notifications/push', kind: 'success', message: 'Results calculated'
Expand Down Expand Up @@ -223,4 +238,20 @@ def scoring_open data
dispatch_self 'errors/push', "#{data['action']} failed: Scoring is closed"
false
end

def force_close_and_finish! task
task.update_column :scoring_open, false
RedisLockManager.release_all
dispatch_all 'locks/load', RedisLockManager.all
dispatch_all 'results/load', CriterionUserResult.includes(:user).where(criterion: task_criterions)
dispatch_all 'comments/load', Comment.includes(:user).where(task_id:)
dispatch_all 'app/finish'
end

def reopen! task
task.update_column :scoring_open, true
dispatch_all 'results/load', CriterionUserResult.includes(:user).where(criterion: task_criterions)
dispatch_all 'comments/load', Comment.includes(:user).where(task_id:)
dispatch_all 'app/ready', ready_info
end
end

0 comments on commit 502e7c6

Please sign in to comment.