Fix n+1 query in gradebook history feed endpoint

Closes PLAT-2686

Test Plan:
- As a student create submissions for several assignments
- Retrieve gradebook history via the api for that student:
  /api/v1/courses/<id>/gradebook_history/feed?user_id=<id>
- Verify that an n+1 query does not occure for originality
  reports. (the repeating query we want to avoid look something like this:
  SQL OriginalityReport Load (4.1ms)  SELECT "originality_reports"
  .* FROM "originality_reports" WHERE "originality_reports"
  ."submission_id" = 55487625)

Change-Id: Id5ba23772bc3eaa3b7ac253b4e6c61a8744ed6b3
Reviewed-on: https://gerrit.instructure.com/116270
Tested-by: Jenkins
Reviewed-by: Andrew Butterfield <abutterfield@instructure.com>
Reviewed-by: Keith T. Garner <kgarner@instructure.com>
QA-Review: August Thornton <august@instructure.com>
Product-Review: Weston Dransfield <wdransfield@instructure.com>
This commit is contained in:
wdransfield 2017-06-20 13:18:36 -06:00 committed by Weston Dransfield
parent 6f532cdd21
commit 9ac4eea42d
2 changed files with 26 additions and 0 deletions

View File

@ -67,6 +67,7 @@ module Api::V1
unless opts[:submission]
ActiveRecord::Associations::Preloader.new.preload(versions, :versionable)
submissions = versions.map(&:versionable)
ActiveRecord::Associations::Preloader.new.preload(versions.map(&:model), :originality_reports)
ActiveRecord::Associations::Preloader.new.preload(submissions, :assignment) unless opts[:assignment]
ActiveRecord::Associations::Preloader.new.preload(submissions, :user) unless opts[:student]
ActiveRecord::Associations::Preloader.new.preload(submissions, :grader)

View File

@ -135,6 +135,31 @@ describe Api::V1::GradebookHistory do
end
end
describe '#versions_json' do
let(:grader) { User.create!(:name => 'grader') }
let(:student) { User.create! }
let(:assignment) { course.assignments.create!(:title => "some assignment") }
let(:versions) { [Version.create!(versionable: submission, model: submission)] }
let(:harness) { GradebookHistoryHarness.new }
let(:submission) do
s = assignment.submit_homework(student)
s.update_attributes(graded_at: now, score: 90, grade: '90', grader: grader)
s
end
let(:course) do
c = Course.create!
c.enroll_student(student)
c.enroll_teacher(grader)
c
end
it "does preloads originality reports" do
submission.reload
harness.versions_json(course, versions, api_context)
expect(versions.first.model.association(:originality_reports).loaded?).to eq true
end
end
describe '#submissions_for' do
before :once do
@course = Course.create!