2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-27 13:42:15 -04:00
|
|
|
# A collection of events to display in an event list.
|
|
|
|
#
|
|
|
|
# An EventCollection is meant to be used for displaying events to a user (e.g.
|
|
|
|
# in a controller), it's not suitable for building queries that are used for
|
|
|
|
# building other queries.
|
|
|
|
class EventCollection
|
2019-10-11 02:06:27 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2022-05-13 08:08:49 -04:00
|
|
|
attr_reader :filter
|
|
|
|
|
2017-07-27 13:42:15 -04:00
|
|
|
# To prevent users from putting too much pressure on the database by cycling
|
|
|
|
# through thousands of events we put a limit on the number of pages.
|
|
|
|
MAX_PAGE = 10
|
|
|
|
|
|
|
|
# projects - An ActiveRecord::Relation object that returns the projects for
|
|
|
|
# which to retrieve events.
|
|
|
|
# filter - An EventFilter instance to use for filtering events.
|
2019-10-11 02:06:27 -04:00
|
|
|
def initialize(projects, limit: 20, offset: 0, filter: nil, groups: nil)
|
2017-07-27 13:42:15 -04:00
|
|
|
@projects = projects
|
|
|
|
@limit = limit
|
|
|
|
@offset = offset
|
2022-05-13 08:08:49 -04:00
|
|
|
@filter = filter || EventFilter.new(EventFilter::ALL)
|
2019-10-11 02:06:27 -04:00
|
|
|
@groups = groups
|
2017-07-27 13:42:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns an Array containing the events.
|
|
|
|
def to_a
|
|
|
|
return [] if current_page > MAX_PAGE
|
|
|
|
|
2019-10-11 02:06:27 -04:00
|
|
|
relation = if groups
|
|
|
|
project_and_group_events
|
2017-07-27 13:42:15 -04:00
|
|
|
else
|
2019-12-18 13:08:04 -05:00
|
|
|
project_events
|
2017-07-27 13:42:15 -04:00
|
|
|
end
|
|
|
|
|
2019-10-11 02:06:27 -04:00
|
|
|
relation = paginate_events(relation)
|
2017-07-27 13:42:15 -04:00
|
|
|
relation.with_associations.to_a
|
|
|
|
end
|
|
|
|
|
2019-12-18 13:08:04 -05:00
|
|
|
def all_project_events
|
2020-07-16 20:09:37 -04:00
|
|
|
Event.from_union([project_events]).recent
|
2019-12-18 13:08:04 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 13:42:15 -04:00
|
|
|
private
|
|
|
|
|
2019-12-18 13:08:04 -05:00
|
|
|
def project_events
|
2022-05-13 08:08:49 -04:00
|
|
|
in_operator_optimized_relation('project_id', projects, Project)
|
2019-12-18 13:08:04 -05:00
|
|
|
end
|
|
|
|
|
2022-03-11 04:07:59 -05:00
|
|
|
def group_events
|
2022-05-13 08:08:49 -04:00
|
|
|
in_operator_optimized_relation('group_id', groups, Namespace)
|
2022-03-11 04:07:59 -05:00
|
|
|
end
|
2017-07-27 13:42:15 -04:00
|
|
|
|
2022-03-11 04:07:59 -05:00
|
|
|
def project_and_group_events
|
2022-05-13 08:08:49 -04:00
|
|
|
if EventFilter::PROJECT_ONLY_EVENT_TYPES.include?(filter.filter)
|
|
|
|
project_events
|
|
|
|
else
|
|
|
|
Event.from_union([project_events, group_events]).recent
|
|
|
|
end
|
2017-07-27 13:42:15 -04:00
|
|
|
end
|
|
|
|
|
2022-05-13 08:08:49 -04:00
|
|
|
def in_operator_optimized_relation(parent_column, parents, parent_model)
|
|
|
|
query_builder_params = if Feature.enabled?(:optimized_project_and_group_activity_queries)
|
|
|
|
array_data = {
|
|
|
|
scope_ids: parents.pluck(:id),
|
|
|
|
scope_model: parent_model,
|
|
|
|
mapping_column: parent_column
|
|
|
|
}
|
|
|
|
filter.in_operator_query_builder_params(array_data)
|
|
|
|
else
|
|
|
|
{
|
|
|
|
scope: filtered_events,
|
|
|
|
array_scope: parents.select(:id),
|
|
|
|
array_mapping_scope: -> (parent_id_expression) { Event.where(Event.arel_table[parent_column].eq(parent_id_expression)).reorder(id: :desc) },
|
|
|
|
finder_query: -> (id_expression) { Event.where(Event.arel_table[:id].eq(id_expression)) }
|
|
|
|
}
|
|
|
|
end
|
2022-03-11 04:07:59 -05:00
|
|
|
|
|
|
|
Gitlab::Pagination::Keyset::InOperatorOptimization::QueryBuilder
|
2022-05-13 08:08:49 -04:00
|
|
|
.new(**query_builder_params)
|
2022-03-11 04:07:59 -05:00
|
|
|
.execute
|
2022-05-13 08:08:49 -04:00
|
|
|
.limit(@limit + @offset)
|
2017-07-27 13:42:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def filtered_events
|
2022-05-13 08:08:49 -04:00
|
|
|
filter.apply_filter(base_relation)
|
2017-07-27 13:42:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def paginate_events(events)
|
|
|
|
events.limit(@limit).offset(@offset)
|
|
|
|
end
|
|
|
|
|
|
|
|
def base_relation
|
|
|
|
# We want to have absolute control over the event queries being built, thus
|
|
|
|
# we're explicitly opting out of any default scopes that may be set.
|
|
|
|
Event.unscoped.recent
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_page
|
|
|
|
(@offset / @limit) + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def projects
|
|
|
|
@projects.except(:order)
|
|
|
|
end
|
2019-10-11 02:06:27 -04:00
|
|
|
|
|
|
|
def groups
|
|
|
|
strong_memoize(:groups) do
|
|
|
|
groups.except(:order) if @groups
|
|
|
|
end
|
|
|
|
end
|
2017-07-27 13:42:15 -04:00
|
|
|
end
|
2022-05-13 08:08:49 -04:00
|
|
|
|
|
|
|
EventCollection.prepend_mod
|