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
|
2016-11-17 07:22:27 -05:00
|
|
|
|
2016-12-05 03:47:10 -05:00
|
|
|
attr_reader :projections, :query, :stage, :order
|
2016-11-17 07:22:27 -05:00
|
|
|
|
2017-02-06 10:23:55 -05:00
|
|
|
MAX_EVENTS = 50
|
|
|
|
|
2016-12-09 06:41:15 -05:00
|
|
|
def initialize(project:, stage:, options:)
|
|
|
|
@project = project
|
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
|
|
|
|
diff_fn = subtract_datetimes_diff(base_query, @options[:start_time_attrs], @options[:end_time_attrs])
|
|
|
|
|
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
|
2017-01-12 06:48:01 -05: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
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def event_result_ids
|
|
|
|
event_result.map { |event| event['id'] }
|
2016-11-17 07:22:27 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|