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