improve section_context_codes performance

don't initialize AR records just to get asset_strings

refs #CNVS-21317

Change-Id: If6afb664807926936d6957cac6aed24cfadc74b4
Reviewed-on: https://gerrit.instructure.com/61251
Reviewed-by: Cody Cutrer <cody@instructure.com>
Tested-by: Jenkins
Product-Review: James Williams  <jamesw@instructure.com>
QA-Review: James Williams  <jamesw@instructure.com>
This commit is contained in:
James Williams 2015-08-19 08:40:12 -06:00
parent 13237275ce
commit 3e9b184dd3
3 changed files with 39 additions and 10 deletions

View File

@ -530,7 +530,7 @@ class UsersController < ApplicationController
Shackles.activate(:slave) do
prepare_current_user_dashboard_items
if @show_recent_feedback = (@current_user.student_enrollments.active.present?)
if @show_recent_feedback = (@current_user.student_enrollments.active.exists?)
@recent_feedback = (@current_user && @current_user.recent_feedback) || []
end
end

View File

@ -2093,21 +2093,32 @@ class Course < ActiveRecord::Base
end
end
def sections_visible_to(user, sections = active_course_sections)
# returns :all, :none, or an array of section ids
def course_section_visibility(user)
visibilities = section_visibilities_for(user)
visibility = enrollment_visibility_level_for(user, visibilities)
section_ids = visibilities.map{ |s| s[:course_section_id] }
is_scope = sections.respond_to?(:where)
if [:full, :limited, :restricted, :sections].include?(visibility)
if visibility == :sections || visibilities.all?{ |v| ['StudentEnrollment', 'StudentViewEnrollment', 'ObserverEnrollment'].include? v[:type] }
is_scope ? sections.where(:id => section_ids) : sections.select{|section| section_ids.include?(section.id)}
visibilities.map{ |s| s[:course_section_id] }
else
sections
:all
end
else
:none
end
end
def sections_visible_to(user, sections = active_course_sections)
is_scope = sections.respond_to?(:where)
section_ids = course_section_visibility(user)
case section_ids
when :all
sections
when :none
# return an empty set, but keep it as a scope for downstream consistency
is_scope ? sections.none : []
when Array
is_scope ? sections.where(:id => section_ids) : sections.select{|section| section_ids.include?(section.id)}
end
end

View File

@ -1806,7 +1806,7 @@ class User < ActiveRecord::Base
order('last_updated_at_from_db DESC').
limit(opts[:limit]).to_a
submissions = submissions.sort_by{|t| (t.last_updated_at_from_db.to_datetime.in_time_zone rescue nil) || t.created_at}.reverse
submissions = submissions.sort_by{|t| t['last_updated_at_from_db'] || t.created_at}.reverse
submissions = submissions.uniq
submissions.first(opts[:limit])
@ -2139,9 +2139,27 @@ class User < ActiveRecord::Base
def section_context_codes(context_codes)
course_ids = context_codes.grep(/\Acourse_\d+\z/).map{ |s| s.sub(/\Acourse_/, '').to_i }
return [] unless course_ids.present?
Course.where(id: course_ids).inject([]) do |ary, course|
ary.concat course.sections_visible_to(self).map(&:asset_string)
section_ids = []
full_course_ids = []
Course.where(id: course_ids).each do |course|
result = course.course_section_visibility(self)
case result
when Array
section_ids.concat(result)
when :all
full_course_ids << course.id
end
end
if full_course_ids.any?
current_shard = Shard.current
Shard.partition_by_shard(full_course_ids) do |shard_course_ids|
section_ids.concat(CourseSection.active.where(:course_id => shard_course_ids).pluck(:id).
map{|id| Shard.relative_id_for(id, Shard.current, current_shard)})
end
end
section_ids.map{|id| "course_section_#{id}"}
end
def manageable_courses(include_concluded = false)