Commit 46920f7e authored by Sean McGivern's avatar Sean McGivern

Merge branch 'jej-memoize-milestoneish-visible-to-user' into 'master'

Memoize Milestoneish#issues_visible_to_user to reduce lookups

https://gitlab.com/gitlab-org/gitlab-ce/issues/25748

See merge request !8146
parents d84cfeaf 27c936ad
module Milestoneish module Milestoneish
def closed_items_count(user) def closed_items_count(user)
issues_visible_to_user(user).closed.size + merge_requests.closed_and_merged.size memoize_per_user(user, :closed_items_count) do
(count_issues_by_state(user)['closed'] || 0) + merge_requests.closed_and_merged.size
end
end end
def total_items_count(user) def total_items_count(user)
issues_visible_to_user(user).size + merge_requests.size memoize_per_user(user, :total_items_count) do
issues_count = count_issues_by_state(user).values.sum
issues_count + merge_requests.size
end
end end
def complete?(user) def complete?(user)
...@@ -30,7 +35,10 @@ module Milestoneish ...@@ -30,7 +35,10 @@ module Milestoneish
end end
def issues_visible_to_user(user) def issues_visible_to_user(user)
IssuesFinder.new(user).execute.where(id: issues) 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)
end
end end
def upcoming? def upcoming?
...@@ -50,4 +58,18 @@ module Milestoneish ...@@ -50,4 +58,18 @@ module Milestoneish
def expired? def expired?
due_date && due_date.past? due_date && due_date.past?
end end
private
def count_issues_by_state(user)
memoize_per_user(user, :count_issues_by_state) do
issues_visible_to_user(user).reorder(nil).group(:state).count
end
end
def memoize_per_user(user, method_name)
@memoized ||= {}
@memoized[method_name] ||= {}
@memoized[method_name][user.try!(:id)] ||= yield
end
end end
...@@ -24,12 +24,16 @@ class GlobalMilestone ...@@ -24,12 +24,16 @@ class GlobalMilestone
@first_milestone = milestones.find {|m| m.description.present? } || milestones.first @first_milestone = milestones.find {|m| m.description.present? } || milestones.first
end end
def milestoneish_ids
milestones.select(:id)
end
def safe_title def safe_title
@title.to_slug.normalize.to_s @title.to_slug.normalize.to_s
end end
def projects def projects
@projects ||= Project.for_milestones(milestones.select(:id)) @projects ||= Project.for_milestones(milestoneish_ids)
end end
def state def state
...@@ -49,11 +53,11 @@ class GlobalMilestone ...@@ -49,11 +53,11 @@ class GlobalMilestone
end end
def issues def issues
@issues ||= Issue.of_milestones(milestones.select(:id)).includes(:project, :assignee, :labels) @issues ||= Issue.of_milestones(milestoneish_ids).includes(:project, :assignee, :labels)
end end
def merge_requests def merge_requests
@merge_requests ||= MergeRequest.of_milestones(milestones.select(:id)).includes(:target_project, :assignee, :labels) @merge_requests ||= MergeRequest.of_milestones(milestoneish_ids).includes(:target_project, :assignee, :labels)
end end
def participants def participants
......
...@@ -129,6 +129,10 @@ class Milestone < ActiveRecord::Base ...@@ -129,6 +129,10 @@ class Milestone < ActiveRecord::Base
self.title self.title
end end
def milestoneish_ids
id
end
def can_be_closed? def can_be_closed?
active? && issues.opened.count.zero? active? && issues.opened.count.zero?
end end
......
---
title: Milestoneish SQL performance partially improved and memoized
merge_request: 8146
author:
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment