Skip to content

Commit

Permalink
Add batch show.
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanzdosilovic committed Jan 17, 2020
1 parent 595c7e1 commit 04c900c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
29 changes: 26 additions & 3 deletions app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,29 @@ def show
render_conversion_error(:bad_request)
end

def batch_show
tokens = (request.headers[:tokens] || params[:tokens]).to_s.strip.split(",")
existing_submissions = Hash[Submission.where(token: tokens).collect{ |s| [s.token, s] }]

submissions = []
tokens.each do |token|
if existing_submissions.has_key?(token)
serialized_submission = ActiveModelSerializers::SerializableResource.new(
existing_submissions[token], { serializer: SubmissionSerializer, base64_encoded: @base64_encoded, fields: @requested_fields }
)
submissions << serialized_submission.as_json
else
submissions << nil
end
end

render json: { submissions: submissions }
rescue Encoding::UndefinedConversionError => e
render json: {
error: "some attributes for one or more submissions cannot be converted to UTF-8, use base64_encoded=true query parameter"
}, status: :bad_request
end

def create
submission = Submission.new(submission_params(params))

Expand All @@ -80,7 +103,7 @@ def create
end

def batch_create
number_of_submissions = params[:_json].try(:size).to_i
number_of_submissions = params[:submissions].try(:size).to_i

if number_of_submissions > Config::MAX_SUBMISSION_BATCH_SIZE
render json: {
Expand All @@ -94,7 +117,7 @@ def batch_create
return
end

submissions = params[:_json].each.collect { |p| Submission.new(submission_params(p)) }
submissions = params[:submissions].each.collect{ |p| Submission.new(submission_params(p)) }

response = []
has_valid_submission = false
Expand Down Expand Up @@ -156,7 +179,7 @@ def check_wait
end

def check_queue_size
number_of_submissions = params[:_json].try(:size).presence || 1
number_of_submissions = params[:submissions].try(:size).presence || 1
if Resque.size(ENV["JUDGE0_VERSION"]) + number_of_submissions > Config::MAX_QUEUE_SIZE
render json: { error: "queue is full" }, status: :service_unavailable
end
Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
root 'home#docs'

resources :submissions, only: [:index, :show, :create, :destroy], param: :token do
post 'batch_create', to: 'submissions#batch_create', on: :collection
post 'batch', to: 'submissions#batch_create', on: :collection
get 'batch', to: 'submissions#batch_show', on: :collection
end

resources :languages, only: [:index, :show] do
Expand Down

0 comments on commit 04c900c

Please sign in to comment.