Class: EventFilter

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
lib/event_filter.rb

Overview

rubocop: disable CodeReuse/ActiveRecord

Constant Summary collapse

ALL =
'all'
PUSH =
'push'
MERGED =
'merged'
ISSUE =
'issue'
COMMENTS =
'comments'
TEAM =
'team'
WIKI =
'wiki'
DESIGNS =
'designs'
PROJECT_ONLY_EVENT_TYPES =
[PUSH, MERGED, TEAM, ISSUE, DESIGNS].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter) ⇒ EventFilter

Returns a new instance of EventFilter.



20
21
22
23
24
# File 'lib/event_filter.rb', line 20

def initialize(filter)
  # Split using comma to maintain backward compatibility Ex/ "filter1,filter2"
  filter = filter.to_s.split(',')[0].to_s
  @filter = filters.include?(filter) ? filter : ALL
end

Instance Attribute Details

#filterObject

Returns the value of attribute filter.



7
8
9
# File 'lib/event_filter.rb', line 7

def filter
  @filter
end

Instance Method Details

#active?(key) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/event_filter.rb', line 26

def active?(key)
  filter == key.to_s
end

#apply_filter(events) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/event_filter.rb', line 30

def apply_filter(events)
  case filter
  when PUSH
    events.pushed_action
  when MERGED
    events.merged_action
  when COMMENTS
    events.commented_action
  when TEAM
    events.where(action: Event::TEAM_ACTIONS)
  when ISSUE
    events.where(action: Event::ISSUE_ACTIONS).for_issue
  when WIKI
    wiki_events(events)
  when DESIGNS
    design_events(events)
  else
    events
  end
end

#in_operator_query_builder_params(array_data) ⇒ Object

rubocop: disable Metrics/CyclomaticComplexity This method build specialized in-operator optimized queries based on different filter parameters. All queries will benefit from the index covering the following columns:

  • author_id target_type action id

  • project_id target_type action id

  • group_id target_type action id

More context: docs.gitlab.com/ee/development/database/efficient_in_operator_queries.html#the-inoperatoroptimization-module



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/event_filter.rb', line 59

def in_operator_query_builder_params(array_data)
  case filter
  when ALL
    in_operator_params(array_data: array_data)
  when PUSH
    # Here we need to add an order hint column to force the correct index usage.
    # Without the order hint, the following conditions will use the `index_events_on_author_id_and_id`
    # index which is not as efficient as the `index_events_for_followed_users` index.
    # > target_type IS NULL AND action = 5 AND author_id = X ORDER BY id DESC
    #
    # The order hint adds an extra order by column which doesn't affect the result but forces the planner
    # to use the correct index:
    # > target_type IS NULL AND action = 5 AND author_id = X ORDER BY target_type DESC, id DESC
    in_operator_params(
      array_data: array_data,
      scope: Event.where(target_type: nil).pushed_action,
      order_hint_column: :target_type
    )
  when MERGED
    in_operator_params(
      array_data: array_data,
      scope: Event.where(target_type: MergeRequest.to_s).merged_action
    )
  when COMMENTS
    in_operator_params(
      array_data: array_data,
      scope: Event.commented_action,
      in_column: :target_type,
      in_values: [Note, *Note.descendants].map(&:name) # To make the query efficient we need to list all Note classes
    )
  when TEAM
    in_operator_params(
      array_data: array_data,
      scope: Event.where(target_type: nil),
      order_hint_column: :target_type,
      in_column: :action,
      in_values: Event.actions.values_at(*Event::TEAM_ACTIONS)
    )
  when ISSUE
    in_operator_params(
      array_data: array_data,
      scope: Event.for_issue,
      in_column: :action,
      in_values: Event.actions.values_at(*Event::ISSUE_ACTIONS)
    )
  when WIKI
    in_operator_params(
      array_data: array_data,
      scope: Event.for_wiki_page,
      in_column: :action,
      in_values: Event.actions.values_at(*Event::WIKI_ACTIONS)
    )
  when DESIGNS
    in_operator_params(
      array_data: array_data,
      scope: Event.for_design,
      in_column: :action,
      in_values: Event.actions.values_at(*Event::DESIGN_ACTIONS)
    )
  else
    in_operator_params(array_data: array_data)
  end
end