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