BigW Consortium Gitlab

milestoneish.rb 2.42 KB
Newer Older
1
module Milestoneish
2
  def closed_items_count(user)
3
    memoize_per_user(user, :closed_items_count) do
4
      (count_issues_by_state(user)['closed'] || 0) + merge_requests.closed_and_merged.size
5
    end
6 7
  end

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

14
  def total_issues_count(user)
15
    count_issues_by_state(user).values.sum
16 17
  end

18
  def complete?(user)
19
    total_items_count(user) > 0 && total_items_count(user) == closed_items_count(user)
20 21
  end

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

  def remaining_days
    return 0 if !due_date || expired?

    (due_date - Date.today).to_i
  end
33

34 35 36 37 38 39
  def elapsed_days
    return 0 if !start_date || start_date.future?

    (Date.today - start_date).to_i
  end

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

47 48 49 50 51 52 53 54
  def sorted_issues(user)
    issues_visible_to_user(user).preload_associations.sort('label_priority')
  end

  def sorted_merge_requests
    merge_requests.sort('label_priority')
  end

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  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
72

73
  def group_milestone?
Felipe Artur committed
74 75 76
    false
  end

77
  def project_milestone?
Felipe Artur committed
78 79 80
    false
  end

81
  def legacy_group_milestone?
Felipe Artur committed
82 83 84
    false
  end

85
  def dashboard_milestone?
Felipe Artur committed
86 87 88
    false
  end

89
  def total_issue_time_spent
90
    @total_issue_time_spent ||= issues.joins(:timelogs).sum(:time_spent)
91 92 93 94 95 96
  end

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

97 98 99 100
  private

  def count_issues_by_state(user)
    memoize_per_user(user, :count_issues_by_state) do
101
      issues_visible_to_user(user).reorder(nil).group(:state).count
102 103 104 105 106 107
    end
  end

  def memoize_per_user(user, method_name)
    @memoized ||= {}
    @memoized[method_name] ||= {}
108
    @memoized[method_name][user&.id] ||= yield
109
  end
110 111 112 113

  # override in a class that includes this module to get a faster query
  # from IssuesFinder
  def issues_finder_params
114
    {}
115
  end
116
end