Reduce SQL queries for querying files when creating submissions

This is especially useful for the RemoteEvaluationsController.

Closes CODEOCEAN-KX
This commit is contained in:
Sebastian Serth
2023-04-02 17:05:22 +02:00
parent fb2a87144d
commit 0aa73d30e9
3 changed files with 9 additions and 5 deletions

View File

@ -5,7 +5,8 @@ module FileParameters
if exercise && params if exercise && params
# We only want to load the files once, to avoid multiple database queries. # 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 # 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| params.reject do |_, file_attributes|
# This mechanism seems cumbersome, but we cannot use an index here. # 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 == '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' 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 false
end end
else else

View File

@ -86,11 +86,10 @@ class RemoteEvaluationController < ApplicationController
files_attributes = remote_evaluation_params[:files_attributes] files_attributes = remote_evaluation_params[:files_attributes]
submission_params = remote_evaluation_params.except(:validation_token) submission_params = remote_evaluation_params.except(:validation_token)
submission_params[:exercise_id] = remote_evaluation_mapping.exercise_id submission_params[:exercise] = remote_evaluation_mapping.exercise
submission_params[:user_id] = remote_evaluation_mapping.user_id submission_params[:user] = remote_evaluation_mapping.user
submission_params[:study_group_id] = remote_evaluation_mapping.study_group_id submission_params[:study_group_id] = remote_evaluation_mapping.study_group_id
submission_params[:cause] = cause submission_params[:cause] = cause
submission_params[:user_type] = remote_evaluation_mapping.user_type
submission_params[:files_attributes] = submission_params[:files_attributes] =
reject_illegal_file_attributes(remote_evaluation_mapping.exercise, files_attributes) reject_illegal_file_attributes(remote_evaluation_mapping.exercise, files_attributes)
submission_params submission_params

View File

@ -131,7 +131,7 @@ module CodeOcean
end end
def set_ancestor_values def set_ancestor_values
%i[feedback_message file_type_id hidden name path read_only role weight].each do |attribute| %i[feedback_message file_type hidden name path read_only role weight].each do |attribute|
send(:"#{attribute}=", ancestor.send(attribute)) send(:"#{attribute}=", ancestor.send(attribute))
end end
end end