BigW Consortium Gitlab

base_event.rb 1.26 KB
Newer Older
1 2 3
module Gitlab
  module CycleAnalytics
    class BaseEvent
4
      include MetricsTables
5

6
      attr_reader :stage, :start_time_attrs, :end_time_attrs, :projections, :query
7

8 9 10 11 12 13 14
      def initialize(project:, options:)
        @query = EventsQuery.new(project: project, options: options)
        @project = project
        @options = options
      end

      def fetch
James Lopez committed
15
        update_author!
16 17

        event_result.map do |event|
18
          serialize(event) if has_permission?(event['id'])
19
        end
20
      end
21

22
      def custom_query(_base_query); end
23

24 25 26
      def order
        @order || @start_time_attrs
      end
27

28
      private
29

30
      def update_author!
James Lopez committed
31 32
        return unless event_result.any? && event_result.first['author_id']

33
        Updater.update!(event_result, from: 'author_id', to: 'author', klass: User)
34 35 36 37 38 39
      end

      def event_result
        @event_result ||= @query.execute(self).to_a
      end

40 41 42 43
      def serialize(_event)
        raise NotImplementedError.new("Expected #{self.name} to implement serialize(event)")
      end

44 45 46 47 48 49 50 51 52 53
      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'] }
54 55 56 57
      end
    end
  end
end