diff options
author | Adam Niedzielski <adamsunday@gmail.com> | 2017-01-06 15:47:18 +0300 |
---|---|---|
committer | Adam Niedzielski <adamsunday@gmail.com> | 2017-01-06 15:47:18 +0300 |
commit | f13c650c161b07bf8d6ad5db849fe9f442f6d0ac (patch) | |
tree | f8517deb61d8e485f8fbe1b6d87142f4c5b43567 /app/models | |
parent | aec04a47c16665f1dfb1fb61647c3f78a4dde20f (diff) |
Speed up group milestone index by passing group_id to IssuesFinder
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/concerns/milestoneish.rb | 10 | ||||
-rw-r--r-- | app/models/global_milestone.rb | 19 | ||||
-rw-r--r-- | app/models/group_milestone.rb | 19 | ||||
-rw-r--r-- | app/models/milestone.rb | 4 |
4 files changed, 46 insertions, 6 deletions
diff --git a/app/models/concerns/milestoneish.rb b/app/models/concerns/milestoneish.rb index 8f02c226f0b..fcc8feddb39 100644 --- a/app/models/concerns/milestoneish.rb +++ b/app/models/concerns/milestoneish.rb @@ -36,8 +36,8 @@ module Milestoneish def issues_visible_to_user(user) memoize_per_user(user, :issues_visible_to_user) do - params = try(:project_id) ? { project_id: project_id } : {} - IssuesFinder.new(user, params).execute.where(milestone_id: milestoneish_ids) + IssuesFinder.new(user, issues_finder_params) + .execute.where(milestone_id: milestoneish_ids) end end @@ -72,4 +72,10 @@ module Milestoneish @memoized[method_name] ||= {} @memoized[method_name][user.try!(:id)] ||= yield end + + # override in a class that includes this module to get a faster query + # from IssuesFinder + def issues_finder_params + {} + end end diff --git a/app/models/global_milestone.rb b/app/models/global_milestone.rb index a54e478f5f8..b991d78e27f 100644 --- a/app/models/global_milestone.rb +++ b/app/models/global_milestone.rb @@ -1,6 +1,8 @@ class GlobalMilestone include Milestoneish + EPOCH = DateTime.parse('1970-01-01') + attr_accessor :title, :milestones alias_attribute :name, :title @@ -8,13 +10,22 @@ class GlobalMilestone @first_milestone end - def self.build_collection(milestones) - milestones = milestones.group_by(&:title) + def self.build_collection(projects, params) + child_milestones = MilestonesFinder.new.execute(projects, params) - milestones.map do |title, milestones| - milestones_relation = Milestone.where(id: milestones.map(&:id)) + milestones = child_milestones.select(:id, :title).group_by(&:title).map do |title, grouped| + milestones_relation = Milestone.where(id: grouped.map(&:id)) new(title, milestones_relation) end + + milestones.sort_by { |milestone| milestone.due_date || EPOCH } + end + + def self.build(projects, title) + child_milestones = Milestone.of_projects(projects).where(title: title) + return if child_milestones.blank? + + new(title, child_milestones) end def initialize(title, milestones) diff --git a/app/models/group_milestone.rb b/app/models/group_milestone.rb new file mode 100644 index 00000000000..7b6db2634b7 --- /dev/null +++ b/app/models/group_milestone.rb @@ -0,0 +1,19 @@ +class GroupMilestone < GlobalMilestone + attr_accessor :group + + def self.build_collection(group, projects, params) + super(projects, params).each do |milestone| + milestone.group = group + end + end + + def self.build(group, projects, title) + super(projects, title).tap do |milestone| + milestone.group = group if milestone + end + end + + def issues_finder_params + { group_id: group.id } + end +end diff --git a/app/models/milestone.rb b/app/models/milestone.rb index 8a11f47dd67..7331000a9f2 100644 --- a/app/models/milestone.rb +++ b/app/models/milestone.rb @@ -202,4 +202,8 @@ class Milestone < ActiveRecord::Base errors.add(:start_date, "Can't be greater than due date") end end + + def issues_finder_params + { project_id: project_id } + end end |