Welcome to mirror list, hosted at ThFree Co, Russian Federation.

numbers_metric.rb « instrumentations « metrics « usage « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f168837bd503ca4fa70332642e482faea63d1b07 (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
# frozen_string_literal: true

module Gitlab
  module Usage
    module Metrics
      module Instrumentations
        class NumbersMetric < BaseMetric
          # Usage Example
          #
          # class BoardsCountMetric < NumbersMetric
          #   operation :add
          #
          #   data do |time_frame|
          #     [
          #        CountIssuesMetric.new(time_frame: time_frame).value,
          #        CountBoardsMetric.new(time_frame: time_frame).value,
          #     ]
          #   end
          # end

          UnimplementedOperationError = Class.new(StandardError) # rubocop:disable UsageData/InstrumentationSuperclass

          class << self
            IMPLEMENTED_OPERATIONS = %i[add].freeze

            private_constant :IMPLEMENTED_OPERATIONS

            def data(&block)
              return @metric_data&.call unless block

              @metric_data = block
            end

            def operation(symbol)
              raise UnimplementedOperationError unless symbol.in?(IMPLEMENTED_OPERATIONS)

              @metric_operation = symbol
            end

            attr_reader :metric_operation, :metric_data
          end

          def value
            method(self.class.metric_operation).call(*data)
          end

          private

          def data
            self.class.metric_data.call(time_frame)
          end
        end
      end
    end
  end
end