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

aggregated_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: de8726f71b5c43ec6b214286f8204075c0490248 (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
# frozen_string_literal: true

module Gitlab
  module Usage
    module Metrics
      module Instrumentations
        # Usage example
        #
        # In metric YAML definition:
        #
        # instrumentation_class: AggregatedMetric
        # data_source: redis_hll
        # options:
        #   aggregate:
        #     operator: OR
        #     attribute: user_id
        #   events:
        #     - 'incident_management_alert_status_changed'
        #     - 'incident_management_alert_assigned'
        #     - 'incident_management_alert_todo'
        #     - 'incident_management_alert_create_incident'

        class AggregatedMetric < BaseMetric
          FALLBACK = -1

          def initialize(metric_definition)
            super
            @source = metric_definition[:data_source]
            @aggregate = options.fetch(:aggregate, {})
          end

          def value
            alt_usage_data(fallback: FALLBACK) do
              Aggregates::Aggregate
                .new(Time.current)
                .calculate_count_for_aggregation(
                  aggregation: aggregate_config,
                  time_frame: time_frame
                )
            end
          end

          private

          attr_accessor :source, :aggregate

          def aggregate_config
            {
              source: source,
              events: options[:events],
              operator: aggregate[:operator]
            }
          end
        end
      end
    end
  end
end