Reduce SQL queries for external user statistics

Fixes CODEOCEAN-100
Fixes CODEOCEAN-YN
Fixes CODEOCEAN-10H
This commit is contained in:
Sebastian Serth
2023-12-05 20:56:44 +01:00
parent de25cb6712
commit a6f64c83a0
3 changed files with 10 additions and 3 deletions

View File

@ -521,7 +521,8 @@ class ExercisesController < ApplicationController
submissions = Submission.where(contributor: @external_user, exercise: @exercise)
.in_study_group_of(current_user)
.order(:created_at)
@show_autosaves = params[:show_autosaves] == 'true' || submissions.none? {|s| s.cause != 'autosave' }
.includes(:exercise, testruns: [:testrun_messages, {file: [:file_type]}], files: [:file_type])
@show_autosaves = params[:show_autosaves] == 'true' || submissions.where.not(cause: 'autosave').none?
submissions = submissions.where.not(cause: 'autosave') unless @show_autosaves
interventions = UserExerciseIntervention.where('user_id = ? AND exercise_id = ?', @external_user.id,
@exercise.id)

View File

@ -7,6 +7,8 @@ class Testrun < ApplicationRecord
belongs_to :testrun_execution_environment, optional: true, dependent: :destroy
has_many :testrun_messages, dependent: :destroy
CONSOLE_OUTPUT = %i[stdout stderr].freeze
enum status: {
ok: 0,
failed: 1,
@ -21,6 +23,10 @@ class Testrun < ApplicationRecord
validates :status, presence: true
def log
testrun_messages.output.pluck(:log).join.presence
if testrun_messages.loaded?
testrun_messages.filter {|m| m.cmd_write? && CONSOLE_OUTPUT.include?(m.stream) }.pluck(:log).join.presence
else
testrun_messages.output.pluck(:log).join.presence
end
end
end

View File

@ -76,7 +76,7 @@ h1
td = this.cause
td = this.score
td.align-middle
-this.testruns.includes(:file).order("files.name").each do |run|
-this.testruns.sort_by {|t| [t.file&.name ? 0 : 1, t.file&.name]}.each do |run|
- if run.passed
.unit-test-result.positive-result title=[run.file&.filepath, run.log].join.strip
- else