Further optimize extracting errors

The previous solution worked, but always queried all ErrorTemplateAttributes, even if there was no match of the ErrorTemplate. This solution resolves this issue and still benefits from less SQL queries.
This commit is contained in:
Sebastian Serth
2023-03-21 07:34:31 +01:00
parent 7af7484f7a
commit e8a21ea319

View File

@ -353,9 +353,17 @@ class SubmissionsController < ApplicationController
def extract_errors
results = []
if @testrun[:output].present?
@submission.exercise.execution_environment.error_templates.left_joins(:error_template_attributes).includes(:error_template_attributes).each do |template|
# First, we test all error templates for a match.
matching_error_templates = @submission.exercise.execution_environment.error_templates.select do |template|
pattern = Regexp.new(template.signature).freeze
results << StructuredError.create_from_template(template, @testrun[:output], @submission) if pattern.match(@testrun[:output])
pattern.match(@testrun[:output])
end
# Second, if there is a match, we preload all ErrorTemplateAttributes and create a StructuredError
#
# Reloading the ErrorTemplate is necessary to allow preloading the ErrorTemplateAttributes.
# However, this results in less (and faster) SQL queries than performing manual lookups.
ErrorTemplate.where(id: matching_error_templates).joins(:error_template_attributes).includes(:error_template_attributes).each do |template|
results << StructuredError.create_from_template(template, @testrun[:output], @submission)
end
end
results