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

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

module Gitlab
  module Usage
    class Metric
      attr_reader :definition

      def initialize(definition)
        @definition = definition
      end

      class << self
        def all
          @all ||= Gitlab::Usage::MetricDefinition.with_instrumentation_class.map do |definition|
            self.new(definition)
          end
        end
      end

      def with_value
        with_availability(proc { instrumentation_object.value })
      end

      def with_instrumentation
        with_availability(proc { instrumentation_object.instrumentation })
      end

      def with_suggested_name
        with_availability(proc { instrumentation_object.suggested_name })
      end

      private

      def with_availability(value_proc)
        return {} unless instrumentation_object.available?

        unflatten_key_path(value_proc.call)
      end

      def unflatten_key_path(value)
        ::Gitlab::Usage::Metrics::KeyPathProcessor.process(definition.key_path, value)
      end

      def instrumentation_class
        "Gitlab::Usage::Metrics::Instrumentations::#{definition.instrumentation_class}"
      end

      def instrumentation_object
        @instrumentation_object ||= instrumentation_class.constantize.new(definition.attributes)
      end
    end
  end
end