From a6f64c83a03ed7824a30ac46f78e929558d25931 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Tue, 5 Dec 2023 20:56:44 +0100 Subject: [PATCH] Reduce SQL queries for external user statistics Fixes CODEOCEAN-100 Fixes CODEOCEAN-YN Fixes CODEOCEAN-10H --- app/controllers/exercises_controller.rb | 3 ++- app/models/testrun.rb | 8 +++++++- app/views/exercises/external_users/statistics.html.slim | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/app/controllers/exercises_controller.rb b/app/controllers/exercises_controller.rb index 1711822f..b8eedfe8 100644 --- a/app/controllers/exercises_controller.rb +++ b/app/controllers/exercises_controller.rb @@ -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) diff --git a/app/models/testrun.rb b/app/models/testrun.rb index 9131790c..ead309aa 100644 --- a/app/models/testrun.rb +++ b/app/models/testrun.rb @@ -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 diff --git a/app/views/exercises/external_users/statistics.html.slim b/app/views/exercises/external_users/statistics.html.slim index d6628369..dd43c334 100644 --- a/app/views/exercises/external_users/statistics.html.slim +++ b/app/views/exercises/external_users/statistics.html.slim @@ -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