Class: Gitlab::Usage::Metrics::Aggregates::Sources::PostgresHll

Inherits:
Object
  • Object
show all
Extended by:
Calculations::Intersection
Defined in:
lib/gitlab/usage/metrics/aggregates/sources/postgres_hll.rb

Class Method Summary collapse

Methods included from Calculations::Intersection

calculate_metrics_intersections

Class Method Details

.calculate_metrics_union(metric_names:, start_date:, end_date:, recorded_at:) ⇒ Object


11
12
13
14
15
16
17
18
19
20
# File 'lib/gitlab/usage/metrics/aggregates/sources/postgres_hll.rb', line 11

def calculate_metrics_union(metric_names:, start_date:, end_date:, recorded_at:)
  time_period = start_date && end_date ? (start_date..end_date) : nil

  Array(metric_names).each_with_object(Gitlab::Database::PostgresHll::Buckets.new) do |event, buckets|
    json = read_aggregated_metric(metric_name: event, time_period: time_period, recorded_at: recorded_at)
    raise UnionNotAvailable, "Union data not available for #{metric_names}" unless json

    buckets.merge_hash!(Gitlab::Json.parse(json))
  end.estimated_distinct_count
end

.save_aggregated_metrics(metric_name:, time_period:, recorded_at_timestamp:, data:) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gitlab/usage/metrics/aggregates/sources/postgres_hll.rb', line 22

def save_aggregated_metrics(metric_name:, time_period:, recorded_at_timestamp:, data:)
  unless data.is_a? ::Gitlab::Database::PostgresHll::Buckets
    Gitlab::ErrorTracking.track_and_raise_for_dev_exception(StandardError.new("Unsupported data type: #{data.class}"))
    return
  end

  # Usage Ping report generation for gitlab.com is very long running process
  # to make sure that saved keys are available at the end of report generation process
  # lets use triple max generation time
  keys_expiration = ::Gitlab::UsageData::MAX_GENERATION_TIME_FOR_SAAS * 3

  Gitlab::Redis::SharedState.with do |redis|
    redis.set(
      redis_key(metric_name: metric_name, time_period: time_period&.values&.first, recorded_at: recorded_at_timestamp),
      data.to_json,
      ex: keys_expiration
    )
  end
rescue ::Redis::CommandError => e
  Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e)
end