gitlab-org--gitlab-foss/lib/gitlab/cycle_analytics/base_event_fetcher.rb

68 lines
1.6 KiB
Ruby
Raw Normal View History

module Gitlab
module CycleAnalytics
2016-12-02 11:09:29 -05:00
class BaseEventFetcher
include BaseQuery
attr_reader :projections, :query, :stage, :order
2017-02-06 10:23:55 -05:00
MAX_EVENTS = 50
def initialize(project:, stage:, options:)
@project = project
2016-12-02 11:09:29 -05:00
@stage = stage
@options = options
end
def fetch
2016-11-17 14:54:02 -05:00
update_author!
event_result.map do |event|
serialize(event) if has_permission?(event['id'])
end.compact
end
2016-12-09 09:23:09 -05:00
def order
@order || default_order
end
private
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)
end
def event_result
@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)
end
def default_order
[@options[:start_time_attrs]].flatten.first
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
nil
end
def event_result_ids
event_result.map { |event| event['id'] }
end
end
end
end