summaryrefslogtreecommitdiff
path: root/lib/gitlab/cycle_analytics/base_event.rb
blob: 53a148ad703504cb70c19bd2894dc6974ca09761 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module Gitlab
  module CycleAnalytics
    class BaseEvent
      include MetricsTables

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

      def initialize(project:, options:)
        @query = EventsQuery.new(project: project, options: options)
        @project = project
        @options = options
      end

      def fetch
        update_author!

        event_result.map do |event|
          serialize(event) if has_permission?(event['id'])
        end.compact
      end

      def custom_query(_base_query); end

      def order
        @order || @start_time_attrs
      end

      private

      def update_author!
        return unless event_result.any? && event_result.first['author_id']

        Updater.update!(event_result, from: 'author_id', to: 'author', klass: User)
      end

      def event_result
        @event_result ||= @query.execute(self).to_a
      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