2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module Gitlab
|
|
|
|
module CycleAnalytics
|
|
|
|
class BaseEventFetcher
|
|
|
|
include BaseQuery
|
2019-10-12 21:52:04 +05:30
|
|
|
include GroupProjectsProvider
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
attr_reader :projections, :query, :stage, :options
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
MAX_EVENTS = 50
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def initialize(stage:, options:)
|
2017-08-17 22:00:37 +05:30
|
|
|
@stage = stage
|
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch
|
|
|
|
update_author!
|
|
|
|
|
|
|
|
event_result.map do |event|
|
|
|
|
serialize(event) if has_permission?(event['id'])
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
def order
|
|
|
|
@order || default_order
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def update_author!
|
|
|
|
return unless event_result.any? && event_result.first['author_id']
|
|
|
|
|
|
|
|
Updater.update!(event_result, from: 'author_id', to: 'author', klass: User)
|
|
|
|
end
|
|
|
|
|
|
|
|
def event_result
|
|
|
|
@event_result ||= ActiveRecord::Base.connection.exec_query(events_query.to_sql).to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
def events_query
|
2019-09-30 21:07:59 +05:30
|
|
|
diff_fn = subtract_datetimes_diff(base_query, options[:start_time_attrs], options[:end_time_attrs])
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
base_query.project(extract_diff_epoch(diff_fn).as('total_time'), *projections).order(order.desc).take(MAX_EVENTS)
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_order
|
2019-09-30 21:07:59 +05:30
|
|
|
[options[:start_time_attrs]].flatten.first
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def serialize(_event)
|
|
|
|
raise NotImplementedError.new("Expected #{self.name} to implement serialize(event)")
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_permission?(id)
|
|
|
|
allowed_ids.nil? || allowed_ids.include?(id.to_i)
|
|
|
|
end
|
|
|
|
|
|
|
|
def allowed_ids
|
2018-03-17 18:26:18 +05:30
|
|
|
@allowed_ids ||= allowed_ids_finder_class
|
2019-09-30 21:07:59 +05:30
|
|
|
.new(options[:current_user], allowed_ids_source)
|
2018-03-17 18:26:18 +05:30
|
|
|
.execute.where(id: event_result_ids).pluck(:id)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def event_result_ids
|
|
|
|
event_result.map { |event| event['id'] }
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
|
|
|
|
def allowed_ids_source
|
2019-10-12 21:52:04 +05:30
|
|
|
group ? { group_id: group.id, include_subgroups: true } : { project_id: project.id }
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def serialization_context
|
|
|
|
{}
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|