summaryrefslogtreecommitdiff
path: root/lib/gitlab/analytics/cycle_analytics/aggregated/data_collector.rb
blob: 22d8874db5702f5ce7223e7170a1c373f16b2018 (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
58
59
60
# frozen_string_literal: true

module Gitlab
  module Analytics
    module CycleAnalytics
      module Aggregated
        # Arguments:
        #   stage - an instance of CycleAnalytics::ProjectStage or CycleAnalytics::GroupStage
        #   params:
        #     current_user: an instance of User
        #     from: DateTime
        #     to: DateTime
        class DataCollector
          include Gitlab::Utils::StrongMemoize

          MAX_COUNT = 10001

          delegate :serialized_records, to: :records_fetcher

          def initialize(stage:, params: {})
            @stage = stage
            @params = params
          end

          def median
            strong_memoize(:median) { Median.new(stage: stage, query: query, params: params) }
          end

          def count
            strong_memoize(:count) { limit_count }
          end

          def records_fetcher
            strong_memoize(:records_fetcher) do
              RecordsFetcher.new(stage: stage, query: query_builder.build_sorted_query, params: params)
            end
          end

          private

          attr_reader :stage, :params

          def query
            query_builder.build
          end

          def query_builder
            @query_builder = BaseQueryBuilder.new(stage: stage, params: params)
          end

          def limit_count
            query.limit(MAX_COUNT).count
          end
        end
      end
    end
  end
end

Gitlab::Analytics::CycleAnalytics::Aggregated::DataCollector.prepend_mod_with('Gitlab::Analytics::CycleAnalytics::Aggregated::DataCollector')