debian-mirror-gitlab/lib/gitlab/usage/metrics/aggregates/aggregate.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

97 lines
3.5 KiB
Ruby
Raw Normal View History

2021-03-11 19:13:27 +05:30
# frozen_string_literal: true
module Gitlab
module Usage
module Metrics
module Aggregates
class Aggregate
2021-09-04 01:27:46 +05:30
include Gitlab::Usage::TimeFrame
2021-03-11 19:13:27 +05:30
2023-03-17 16:20:25 +05:30
# TODO: define this missing event https://gitlab.com/gitlab-org/gitlab/-/issues/385080
EVENTS_NOT_DEFINED_YET = %w[
i_code_review_merge_request_widget_license_compliance_warning
].freeze
2021-03-11 19:13:27 +05:30
def initialize(recorded_at)
@recorded_at = recorded_at
end
2022-11-25 23:54:43 +05:30
def calculate_count_for_aggregation(aggregation:, time_frame:)
with_validate_configuration(aggregation, time_frame) do
source = SOURCES[aggregation[:source]]
2023-03-17 16:20:25 +05:30
events = select_defined_events(aggregation[:events], aggregation[:source])
2022-11-25 23:54:43 +05:30
if aggregation[:operator] == UNION_OF_AGGREGATED_METRICS
2023-03-17 16:20:25 +05:30
source.calculate_metrics_union(**time_constraints(time_frame).merge(metric_names: events, recorded_at: recorded_at))
2022-11-25 23:54:43 +05:30
else
2023-03-17 16:20:25 +05:30
source.calculate_metrics_intersections(**time_constraints(time_frame).merge(metric_names: events, recorded_at: recorded_at))
2022-11-25 23:54:43 +05:30
end
end
rescue Gitlab::UsageDataCounters::HLLRedisCounter::EventError, AggregatedMetricError => error
failure(error)
2021-03-11 19:13:27 +05:30
end
private
2023-01-13 00:05:48 +05:30
attr_accessor :recorded_at
2021-03-11 19:13:27 +05:30
2022-11-25 23:54:43 +05:30
def with_validate_configuration(aggregation, time_frame)
source = aggregation[:source]
unless ALLOWED_METRICS_AGGREGATIONS.include?(aggregation[:operator])
return failure(
UnknownAggregationOperator
.new("Events should be aggregated with one of operators #{ALLOWED_METRICS_AGGREGATIONS}")
)
2021-03-11 19:13:27 +05:30
end
2022-11-25 23:54:43 +05:30
unless SOURCES[source]
return failure(
UnknownAggregationSource
.new("Aggregation source: '#{source}' must be included in #{SOURCES.keys}")
)
end
if time_frame == Gitlab::Usage::TimeFrame::ALL_TIME_TIME_FRAME_NAME && source == REDIS_SOURCE
return failure(
DisallowedAggregationTimeFrame
.new("Aggregation time frame: 'all' is not allowed for aggregation with source: '#{REDIS_SOURCE}'")
)
end
yield
end
def failure(error)
2021-03-11 19:13:27 +05:30
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(error)
Gitlab::Utils::UsageData::FALLBACK
end
2022-11-25 23:54:43 +05:30
def time_constraints(time_frame)
case time_frame
when Gitlab::Usage::TimeFrame::TWENTY_EIGHT_DAYS_TIME_FRAME_NAME
monthly_time_range
when Gitlab::Usage::TimeFrame::SEVEN_DAYS_TIME_FRAME_NAME
weekly_time_range
when Gitlab::Usage::TimeFrame::ALL_TIME_TIME_FRAME_NAME
{ start_date: nil, end_date: nil }
end
end
2023-03-17 16:20:25 +05:30
def select_defined_events(events, source)
# Database source metrics get validated inside the PostgresHll class:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/usage/metrics/aggregates/sources/postgres_hll.rb#L16
return events if source != ::Gitlab::Usage::Metrics::Aggregates::REDIS_SOURCE
events.select do |event|
::Gitlab::UsageDataCounters::HLLRedisCounter.known_event?(event) || EVENTS_NOT_DEFINED_YET.include?(event)
end
end
2021-03-11 19:13:27 +05:30
end
end
end
end
end
2023-03-04 22:38:38 +05:30
Gitlab::Usage::Metrics::Aggregates::Aggregate.prepend_mod