debian-mirror-gitlab/lib/gitlab/usage/metrics/instrumentations/database_metric.rb

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

149 lines
4.2 KiB
Ruby
Raw Normal View History

2021-06-08 01:23:25 +05:30
# frozen_string_literal: true
module Gitlab
module Usage
module Metrics
module Instrumentations
class DatabaseMetric < BaseMetric
# Usage Example
#
# class CountUsersCreatingIssuesMetric < DatabaseMetric
# operation :distinct_count, column: :author_id
#
# relation do |database_time_constraints|
# ::Issue.where(database_time_constraints)
# end
# end
2022-07-16 23:28:13 +05:30
UnimplementedOperationError = Class.new(StandardError) # rubocop:disable UsageData/InstrumentationSuperclass
2021-06-08 01:23:25 +05:30
class << self
2022-07-23 23:45:48 +05:30
IMPLEMENTED_OPERATIONS = %i(count distinct_count estimate_batch_distinct_count sum average).freeze
2022-07-16 23:28:13 +05:30
private_constant :IMPLEMENTED_OPERATIONS
2021-06-08 01:23:25 +05:30
def start(&block)
2022-08-27 11:52:29 +05:30
return @metric_start&.call unless block
2021-09-30 23:02:18 +05:30
2021-06-08 01:23:25 +05:30
@metric_start = block
end
def finish(&block)
2022-08-27 11:52:29 +05:30
return @metric_finish&.call unless block
2021-09-30 23:02:18 +05:30
2021-06-08 01:23:25 +05:30
@metric_finish = block
end
2023-01-13 00:05:48 +05:30
def relation(relation_proc = nil, &block)
return unless relation_proc || block
2021-09-30 23:02:18 +05:30
2023-01-13 00:05:48 +05:30
@metric_relation = (relation_proc || block)
2021-06-08 01:23:25 +05:30
end
2022-05-07 20:08:51 +05:30
def metric_options(&block)
2022-08-27 11:52:29 +05:30
return @metric_options&.call.to_h unless block
2022-05-07 20:08:51 +05:30
@metric_options = block
end
2022-08-27 11:52:29 +05:30
def timestamp_column(symbol)
@metric_timestamp_column = symbol
end
2021-10-27 15:23:28 +05:30
def operation(symbol, column: nil, &block)
2022-07-16 23:28:13 +05:30
raise UnimplementedOperationError unless symbol.in?(IMPLEMENTED_OPERATIONS)
2021-06-08 01:23:25 +05:30
@metric_operation = symbol
@column = column
2022-08-27 11:52:29 +05:30
@metric_operation_block = block if block
2021-06-08 01:23:25 +05:30
end
2021-09-30 23:02:18 +05:30
def cache_start_and_finish_as(cache_key)
@cache_key = cache_key
end
2022-08-27 11:52:29 +05:30
attr_reader :metric_operation, :metric_relation, :metric_start,
:metric_finish, :metric_operation_block,
:column, :cache_key, :metric_timestamp_column
2021-06-08 01:23:25 +05:30
end
def value
2021-09-30 23:02:18 +05:30
start, finish = get_or_cache_batch_ids
2021-06-08 01:23:25 +05:30
method(self.class.metric_operation)
.call(relation,
self.class.column,
2021-09-30 23:02:18 +05:30
start: start,
2021-10-27 15:23:28 +05:30
finish: finish,
2022-05-07 20:08:51 +05:30
**self.class.metric_options,
2021-10-27 15:23:28 +05:30
&self.class.metric_operation_block)
2021-06-08 01:23:25 +05:30
end
2021-09-04 01:27:46 +05:30
def to_sql
Gitlab::Usage::Metrics::Query.for(self.class.metric_operation, relation, self.class.column)
end
2021-10-27 15:23:28 +05:30
def instrumentation
to_sql
end
2021-09-04 01:27:46 +05:30
def suggested_name
Gitlab::Usage::Metrics::NameSuggestion.for(
self.class.metric_operation,
relation: relation,
column: self.class.column
)
2021-06-08 01:23:25 +05:30
end
private
2022-07-16 23:28:13 +05:30
def start
self.class.metric_start&.call(time_constraints)
end
def finish
self.class.metric_finish&.call(time_constraints)
end
2021-09-04 01:27:46 +05:30
def relation
2023-01-13 00:05:48 +05:30
if self.class.metric_relation.arity == 1
self.class.metric_relation.call(options)
else
self.class.metric_relation.call
end.where(time_constraints)
2021-09-04 01:27:46 +05:30
end
2021-06-08 01:23:25 +05:30
def time_constraints
case time_frame
when '28d'
2022-08-27 11:52:29 +05:30
monthly_time_range_db_params(column: self.class.metric_timestamp_column)
2021-06-08 01:23:25 +05:30
when 'all'
{}
when 'none'
nil
else
raise "Unknown time frame: #{time_frame} for DatabaseMetric"
end
end
2021-09-30 23:02:18 +05:30
def get_or_cache_batch_ids
2022-07-16 23:28:13 +05:30
return [start, finish] unless self.class.cache_key.present?
2021-09-30 23:02:18 +05:30
key_name = "metric_instrumentation/#{self.class.cache_key}"
2022-07-16 23:28:13 +05:30
cached_start = Gitlab::Cache.fetch_once("#{key_name}_minimum_id", expires_in: 1.day) do
start
2021-09-30 23:02:18 +05:30
end
2022-07-16 23:28:13 +05:30
cached_finish = Gitlab::Cache.fetch_once("#{key_name}_maximum_id", expires_in: 1.day) do
finish
2021-09-30 23:02:18 +05:30
end
2022-07-16 23:28:13 +05:30
[cached_start, cached_finish]
2021-09-30 23:02:18 +05:30
end
2021-06-08 01:23:25 +05:30
end
end
end
end
end