Class: IssuableFinder

Inherits:
Object
  • Object
show all
Includes:
CreatedAtFilter, FinderMethods, Gitlab::Utils::StrongMemoize
Defined in:
app/finders/issuable_finder.rb,
app/finders/issuable_finder/params.rb

Overview

IssuableFinder

Used to filter Issues and MergeRequests collections by set of params

Arguments:

klass - actual class like Issue or MergeRequest
current_user - which user use
params:
  scope: 'created_by_me' or 'assigned_to_me' or 'all'
  state: 'opened' or 'closed' or 'locked' or 'all'
  group_id: integer
  project_id: integer
  milestone_title: string
  release_tag: string
  author_id: integer
  author_username: string
  assignee_id: integer or 'None' or 'Any'
  assignee_username: string
  search: string
  in: 'title', 'description', or a string joining them with comma
  label_name: string
  sort: string
  non_archived: boolean
  iids: integer[]
  my_reaction_emoji: string
  created_after: datetime
  created_before: datetime
  updated_after: datetime
  updated_before: datetime
  attempt_group_search_optimizations: boolean
  attempt_project_search_optimizations: boolean

Direct Known Subclasses

IssuesFinder, MergeRequestsFinder

Defined Under Namespace

Classes: Params

Constant Summary collapse

NEGATABLE_PARAMS_HELPER_KEYS =
%i[project_id scope status include_subgroups].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods included from CreatedAtFilter

#by_created_at

Methods included from FinderMethods

#find, #find_by, #find_by!

Constructor Details

#initialize(current_user, params = {}) ⇒ IssuableFinder

Returns a new instance of IssuableFinder.


94
95
96
97
# File 'app/finders/issuable_finder.rb', line 94

def initialize(current_user, params = {})
  @current_user = current_user
  @params = params_class.new(params, current_user, klass)
end

Instance Attribute Details

#current_userObject

Returns the value of attribute current_user


46
47
48
# File 'app/finders/issuable_finder.rb', line 46

def current_user
  @current_user
end

#paramsObject

Returns the value of attribute params


46
47
48
# File 'app/finders/issuable_finder.rb', line 46

def params
  @params
end

Class Method Details

.array_paramsObject


66
67
68
# File 'app/finders/issuable_finder.rb', line 66

def array_params
  @array_params ||= { label_name: [], assignee_username: [] }
end

.negatable_array_paramsObject

This should not be used in controller strong params!


76
77
78
# File 'app/finders/issuable_finder.rb', line 76

def negatable_array_params
  @negatable_array_params ||= array_params.keys.append(:iids)
end

.negatable_paramsObject

This should not be used in controller strong params!


81
82
83
# File 'app/finders/issuable_finder.rb', line 81

def negatable_params
  @negatable_params ||= negatable_scalar_params + negatable_array_params
end

.negatable_scalar_paramsObject

This should not be used in controller strong params!


71
72
73
# File 'app/finders/issuable_finder.rb', line 71

def negatable_scalar_params
  @negatable_scalar_params ||= scalar_params - %i[search in]
end

.scalar_paramsObject


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/finders/issuable_finder.rb', line 51

def scalar_params
  @scalar_params ||= %i[
  assignee_id
  assignee_username
  author_id
  author_username
  label_name
  milestone_title
  release_tag
  my_reaction_emoji
  search
  in
]
end

.valid_paramsObject


85
86
87
# File 'app/finders/issuable_finder.rb', line 85

def valid_params
  @valid_params ||= scalar_params + [array_params.merge(not: {})]
end

Instance Method Details

#count_by_stateObject

We often get counts for each state by running a query per state, and counting those results. This is typically slower than running one query (even if that query is slower than any of the individual state queries) and grouping and counting within that query.

rubocop: disable CodeReuse/ActiveRecord


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/finders/issuable_finder.rb', line 162

def count_by_state
  count_params = params.merge(state: nil, sort: nil, force_cte: true)
  finder = self.class.new(current_user, count_params)

  counts = Hash.new(0)

  # Searching by label includes a GROUP BY in the query, but ours will be last
  # because it is added last. Searching by multiple labels also includes a row
  # per issuable, so we have to count those in Ruby - which is bad, but still
  # better than performing multiple queries.
  #
  # This does not apply when we are using a CTE for the search, as the labels
  # GROUP BY is inside the subquery in that case, so we set labels_count to 1.
  #
  # Groups and projects have separate feature flags to suggest the use
  # of a CTE. The CTE will not be used if the sort doesn't support it,
  # but will always be used for the counts here as we ignore sorting
  # anyway.
  labels_count = params.label_names.any? ? params.label_names.count : 1
  labels_count = 1 if use_cte_for_search?

  finder.execute.reorder(nil).group(:state_id).count.each do |key, value|
    counts[count_key(key)] += value / labels_count
  end

  counts[:all] = counts.values.sum

  counts.with_indifferent_access
end

#executeObject


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/finders/issuable_finder.rb', line 99

def execute
  items = init_collection
  items = filter_items(items)

  # Let's see if we have to negate anything
  items = filter_negated_items(items)

  # This has to be last as we use a CTE as an optimization fence
  # for counts by passing the force_cte param and passing the
  # attempt_group_search_optimizations param
  # https://www.postgresql.org/docs/current/static/queries-with.html
  items = by_search(items)

  items = sort(items)

  items
end

#filter_items(items) ⇒ Object


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/finders/issuable_finder.rb', line 117

def filter_items(items)
  items = by_project(items)
  items = by_group(items)
  items = by_scope(items)
  items = by_created_at(items)
  items = by_updated_at(items)
  items = by_closed_at(items)
  items = by_state(items)
  items = by_group(items)
  items = by_assignee(items)
  items = by_author(items)
  items = by_non_archived(items)
  items = by_iids(items)
  items = by_milestone(items)
  items = by_release(items)
  items = by_label(items)
  by_my_reaction_emoji(items)
end

#filter_negated_items(items) ⇒ Object

Negates all params found in `negatable_params`


137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/finders/issuable_finder.rb', line 137

def filter_negated_items(items)
  return items unless Feature.enabled?(:not_issuable_queries, params.group || params.project, default_enabled: true)

  # API endpoints send in `nil` values so we test if there are any non-nil
  return items unless not_params.present? && not_params.values.any?

  items = by_negated_author(items)
  items = by_negated_assignee(items)
  items = by_negated_label(items)
  items = by_negated_milestone(items)
  items = by_negated_release(items)
  items = by_negated_my_reaction_emoji(items)
  by_negated_iids(items)
end

#params_classObject


90
91
92
# File 'app/finders/issuable_finder.rb', line 90

def params_class
  IssuableFinder::Params
end

#row_countObject


152
153
154
# File 'app/finders/issuable_finder.rb', line 152

def row_count
  Gitlab::IssuablesCountForState.new(self).for_state_or_opened(params[:state])
end

#searchObject

rubocop: enable CodeReuse/ActiveRecord


193
194
195
# File 'app/finders/issuable_finder.rb', line 193

def search
  params[:search].presence
end

#use_cte_for_search?Boolean

Returns:

  • (Boolean)

197
198
199
200
201
202
203
204
205
# File 'app/finders/issuable_finder.rb', line 197

def use_cte_for_search?
  strong_memoize(:use_cte_for_search) do
    next false unless search
    # Only simple unsorted & simple sorts can use CTE
    next false if params[:sort].present? && !params[:sort].in?(klass.simple_sorts.keys)

    attempt_group_search_optimizations? || attempt_project_search_optimizations?
  end
end