Reduce SQL queries for querying files when creating submissions
This is especially useful for the RemoteEvaluationsController. Closes CODEOCEAN-KX
This commit is contained in:
@ -5,7 +5,8 @@ module FileParameters
|
||||
if exercise && params
|
||||
# We only want to load the files once, to avoid multiple database queries.
|
||||
# Further, we use `unscope` to avoid that the `order` scope is applied
|
||||
files = CodeOcean::File.unscope(:order).where(id: params.values.pluck(:file_id))
|
||||
# Optimization: We query for the `file_type` here, which is used in `CodeOcean::File#set_ancestor_values`.
|
||||
files = CodeOcean::File.unscope(:order).where(id: params.values.pluck(:file_id)).includes(:file_type)
|
||||
|
||||
params.reject do |_, file_attributes|
|
||||
# This mechanism seems cumbersome, but we cannot use an index here.
|
||||
@ -19,6 +20,10 @@ module FileParameters
|
||||
next true if file.context_type == 'Submission' && (file.context.user_id != current_user.id || file.context.user_type != current_user.class.name)
|
||||
next true if file.context_type == 'CommunitySolution' && controller_name != 'community_solutions'
|
||||
|
||||
# Optimization: We already queried the ancestor file, let's reuse the object.
|
||||
file_attributes[:file] = file
|
||||
file_attributes.delete(:file_id)
|
||||
|
||||
false
|
||||
end
|
||||
else
|
||||
|
@ -86,11 +86,10 @@ class RemoteEvaluationController < ApplicationController
|
||||
|
||||
files_attributes = remote_evaluation_params[:files_attributes]
|
||||
submission_params = remote_evaluation_params.except(:validation_token)
|
||||
submission_params[:exercise_id] = remote_evaluation_mapping.exercise_id
|
||||
submission_params[:user_id] = remote_evaluation_mapping.user_id
|
||||
submission_params[:exercise] = remote_evaluation_mapping.exercise
|
||||
submission_params[:user] = remote_evaluation_mapping.user
|
||||
submission_params[:study_group_id] = remote_evaluation_mapping.study_group_id
|
||||
submission_params[:cause] = cause
|
||||
submission_params[:user_type] = remote_evaluation_mapping.user_type
|
||||
submission_params[:files_attributes] =
|
||||
reject_illegal_file_attributes(remote_evaluation_mapping.exercise, files_attributes)
|
||||
submission_params
|
||||
|
Reference in New Issue
Block a user