milestoneish.rb 2.97 KB
Newer Older
1 2
# frozen_string_literal: true

3
module Milestoneish
4
  def closed_items_count(user)
5
    memoize_per_user(user, :closed_items_count) do
6
      (count_issues_by_state(user)['closed'] || 0) + merge_requests.closed_and_merged.size
7
    end
8 9
  end

10
  def total_items_count(user)
11
    memoize_per_user(user, :total_items_count) do
12
      total_issues_count(user) + merge_requests.size
13
    end
14 15
  end

16
  def total_issues_count(user)
17
    count_issues_by_state(user).values.sum
18 19
  end

20
  def complete?(user)
21
    total_items_count(user) > 0 && total_items_count(user) == closed_items_count(user)
22 23
  end

24
  def percent_complete(user)
25
    ((closed_items_count(user) * 100) / total_items_count(user)).abs
26 27 28 29 30 31 32 33 34
  rescue ZeroDivisionError
    0
  end

  def remaining_days
    return 0 if !due_date || expired?

    (due_date - Date.today).to_i
  end
35

36 37 38 39 40 41
  def elapsed_days
    return 0 if !start_date || start_date.future?

    (Date.today - start_date).to_i
  end

42
  def issues_visible_to_user(user)
43
    memoize_per_user(user, :issues_visible_to_user) do
44
      IssuesFinder.new(user, issues_finder_params)
45
        .execute.preload(:assignees).where(milestone_id: milestoneish_id)
46
    end
47
  end
48

49
  def sorted_issues(user)
50
    issues_visible_to_user(user).preload_associations.sort_by_attribute('label_priority')
51 52
  end

53 54
  def sorted_merge_requests(user)
    merge_requests_visible_to_user(user).sort_by_attribute('label_priority')
55 56
  end

57 58 59 60 61 62 63
  def merge_requests_visible_to_user(user)
    memoize_per_user(user, :merge_requests_visible_to_user) do
      MergeRequestsFinder.new(user, issues_finder_params)
        .execute.where(milestone_id: milestoneish_id)
    end
  end

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  def upcoming?
    start_date && start_date.future?
  end

  def expires_at
    if due_date
      if due_date.past?
        "expired on #{due_date.to_s(:medium)}"
      else
        "expires on #{due_date.to_s(:medium)}"
      end
    end
  end

  def expired?
    due_date && due_date.past?
  end
81

82
  def group_milestone?
Felipe Artur's avatar
Felipe Artur committed
83 84 85
    false
  end

86
  def project_milestone?
Felipe Artur's avatar
Felipe Artur committed
87 88 89
    false
  end

90
  def legacy_group_milestone?
Felipe Artur's avatar
Felipe Artur committed
91 92 93
    false
  end

94
  def dashboard_milestone?
Felipe Artur's avatar
Felipe Artur committed
95 96 97
    false
  end

98
  def total_issue_time_spent
99
    @total_issue_time_spent ||= issues.joins(:timelogs).sum(:time_spent)
100 101 102 103 104 105
  end

  def human_total_issue_time_spent
    Gitlab::TimeTrackingFormatter.output(total_issue_time_spent)
  end

106 107 108 109 110 111 112 113
  def total_issue_time_estimate
    @total_issue_time_estimate ||= issues.sum(:time_estimate)
  end

  def human_total_issue_time_estimate
    Gitlab::TimeTrackingFormatter.output(total_issue_time_estimate)
  end

114 115
  def count_issues_by_state(user)
    memoize_per_user(user, :count_issues_by_state) do
116
      issues_visible_to_user(user).reorder(nil).group(:state).count
117 118 119
    end
  end

120 121
  private

122
  def memoize_per_user(user, method_name)
123 124 125 126 127
    memoized_users[method_name][user&.id] ||= yield
  end

  def memoized_users
    @memoized_users ||= Hash.new { |h, k| h[k] = {} }
128
  end
129 130 131 132

  # override in a class that includes this module to get a faster query
  # from IssuesFinder
  def issues_finder_params
133
    {}
134
  end
135
end