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

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/usage/metrics/instrumentations/aggregated_metric.rb')
-rw-r--r--lib/gitlab/usage/metrics/instrumentations/aggregated_metric.rb71
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/gitlab/usage/metrics/instrumentations/aggregated_metric.rb b/lib/gitlab/usage/metrics/instrumentations/aggregated_metric.rb
new file mode 100644
index 00000000000..63ead5a8cb0
--- /dev/null
+++ b/lib/gitlab/usage/metrics/instrumentations/aggregated_metric.rb
@@ -0,0 +1,71 @@
+# 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 = parse_data_source_to_legacy_value(metric_definition)
+ @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
+
+ def suggested_name
+ Gitlab::Usage::Metrics::NameSuggestion.for(:alt)
+ end
+
+ private
+
+ attr_accessor :source, :aggregate
+
+ # TODO: This method is a temporary measure that
+ # handles backwards compatibility until
+ # point 5 from is resolved https://gitlab.com/gitlab-org/gitlab/-/issues/370963#implementation
+ def parse_data_source_to_legacy_value(metric_definition)
+ return 'redis' if metric_definition[:data_source] == 'redis_hll'
+
+ metric_definition[:data_source]
+ end
+
+ def aggregate_config
+ {
+ source: source,
+ events: options[:events],
+ operator: aggregate[:operator]
+ }
+ end
+ end
+ end
+ end
+ end
+end