2020-09-09 17:08:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
2020-09-16 02:09:24 -04:00
|
|
|
# This class is used by the `gitlab:usage_data:dump_sql` rake tasks to output SQL instead of running it.
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/-/merge_requests/41091
|
2020-09-09 17:08:33 -04:00
|
|
|
class UsageDataQueries < UsageData
|
|
|
|
class << self
|
2022-04-20 11:10:23 -04:00
|
|
|
def with_duration
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
2021-10-28 05:13:54 -04:00
|
|
|
def add_metric(metric, time_frame: 'none', options: {})
|
2021-07-21 11:08:52 -04:00
|
|
|
metric_class = "Gitlab::Usage::Metrics::Instrumentations::#{metric}".constantize
|
|
|
|
|
2021-10-28 05:13:54 -04:00
|
|
|
metric_class.new(time_frame: time_frame, options: options).instrumentation
|
2021-07-21 11:08:52 -04:00
|
|
|
end
|
|
|
|
|
2021-03-26 05:09:24 -04:00
|
|
|
def count(relation, column = nil, *args, **kwargs)
|
2021-05-31 11:11:12 -04:00
|
|
|
Gitlab::Usage::Metrics::Query.for(:count, relation, column)
|
2020-09-09 17:08:33 -04:00
|
|
|
end
|
|
|
|
|
2021-03-26 05:09:24 -04:00
|
|
|
def distinct_count(relation, column = nil, *args, **kwargs)
|
2021-05-31 11:11:12 -04:00
|
|
|
Gitlab::Usage::Metrics::Query.for(:distinct_count, relation, column)
|
2020-09-15 17:09:35 -04:00
|
|
|
end
|
|
|
|
|
2021-03-26 05:09:24 -04:00
|
|
|
def sum(relation, column, *args, **kwargs)
|
2021-05-31 11:11:12 -04:00
|
|
|
Gitlab::Usage::Metrics::Query.for(:sum, relation, column)
|
2020-09-16 02:09:24 -04:00
|
|
|
end
|
|
|
|
|
2021-04-21 11:09:35 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def histogram(relation, column, buckets:, bucket_size: buckets.size)
|
2021-05-31 11:11:12 -04:00
|
|
|
Gitlab::Usage::Metrics::Query.for(:histogram, relation, column, buckets: buckets, bucket_size: bucket_size)
|
2021-04-21 11:09:35 -04:00
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
2020-12-03 10:09:46 -05:00
|
|
|
# For estimated distinct count use exact query instead of hll
|
|
|
|
# buckets query, because it can't be used to obtain estimations without
|
2020-12-05 22:09:38 -05:00
|
|
|
# supplementary ruby code present in Gitlab::Database::PostgresHll::BatchDistinctCounter
|
2021-03-26 05:09:24 -04:00
|
|
|
def estimate_batch_distinct_count(relation, column = nil, *args, **kwargs)
|
2021-05-31 11:11:12 -04:00
|
|
|
Gitlab::Usage::Metrics::Query.for(:estimate_batch_distinct_count, relation, column)
|
2020-12-03 10:09:46 -05:00
|
|
|
end
|
|
|
|
|
2021-02-25 04:10:45 -05:00
|
|
|
def add(*args)
|
2021-05-31 11:11:12 -04:00
|
|
|
'SELECT ' + args.map { |arg| "(#{arg})" }.join(' + ')
|
2021-02-25 04:10:45 -05:00
|
|
|
end
|
|
|
|
|
2021-04-21 11:09:35 -04:00
|
|
|
def maximum_id(model, column = nil)
|
2022-03-07 01:15:33 -05:00
|
|
|
# no-op: shadowing super for performance reasons
|
2021-04-21 11:09:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def minimum_id(model, column = nil)
|
2022-03-07 01:15:33 -05:00
|
|
|
# no-op: shadowing super for performance reasons
|
|
|
|
end
|
|
|
|
|
|
|
|
def alt_usage_data(value = nil, fallback: FALLBACK, &block)
|
|
|
|
if block_given?
|
2022-04-07 05:08:40 -04:00
|
|
|
{ alt_usage_data_block: "non-SQL usage data block" }
|
2022-03-07 01:15:33 -05:00
|
|
|
else
|
|
|
|
{ alt_usage_data_value: value }
|
|
|
|
end
|
2021-04-21 11:09:35 -04:00
|
|
|
end
|
|
|
|
|
2021-05-31 11:11:12 -04:00
|
|
|
def redis_usage_data(counter = nil, &block)
|
|
|
|
if block_given?
|
2022-04-07 05:08:40 -04:00
|
|
|
{ redis_usage_data_block: "non-SQL usage data block" }
|
2021-05-31 11:11:12 -04:00
|
|
|
elsif counter.present?
|
2022-04-07 05:08:40 -04:00
|
|
|
{ redis_usage_data_counter: counter.to_s }
|
2021-05-31 11:11:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-18 11:10:16 -04:00
|
|
|
def jira_integration_data
|
2021-04-21 11:09:35 -04:00
|
|
|
{
|
|
|
|
projects_jira_server_active: 0,
|
|
|
|
projects_jira_cloud_active: 0
|
|
|
|
}
|
2021-04-09 02:09:30 -04:00
|
|
|
end
|
|
|
|
|
2021-04-21 11:09:35 -04:00
|
|
|
def epics_deepest_relationship_level
|
|
|
|
{ epics_deepest_relationship_level: 0 }
|
2021-04-09 02:09:30 -04:00
|
|
|
end
|
2022-04-07 05:08:40 -04:00
|
|
|
|
|
|
|
def topology_usage_data
|
|
|
|
{
|
|
|
|
duration_s: 0,
|
|
|
|
failures: []
|
|
|
|
}
|
|
|
|
end
|
2022-06-08 02:08:10 -04:00
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def sent_in_product_marketing_email_count(sent_emails, track, series)
|
|
|
|
count(Users::InProductMarketingEmail.where(track: track, series: series))
|
|
|
|
end
|
|
|
|
|
|
|
|
def clicked_in_product_marketing_email_count(clicked_emails, track, series)
|
|
|
|
count(Users::InProductMarketingEmail.where(track: track, series: series).where.not(cta_clicked_at: nil))
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2020-09-09 17:08:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|