debian-mirror-gitlab/lib/gitlab/usage_data_queries.rb

86 lines
2.6 KiB
Ruby
Raw Normal View History

2020-11-24 15:15:51 +05:30
# frozen_string_literal: true
module Gitlab
# 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
class UsageDataQueries < UsageData
class << self
2021-04-29 21:17:54 +05:30
def count(relation, column = nil, *args, **kwargs)
2020-11-24 15:15:51 +05:30
raw_sql(relation, column)
end
2021-04-29 21:17:54 +05:30
def distinct_count(relation, column = nil, *args, **kwargs)
2020-11-24 15:15:51 +05:30
raw_sql(relation, column, :distinct)
end
def redis_usage_data(counter = nil, &block)
if block_given?
{ redis_usage_data_block: block.to_s }
elsif counter.present?
{ redis_usage_data_counter: counter }
end
end
2021-04-29 21:17:54 +05:30
def sum(relation, column, *args, **kwargs)
2021-01-03 14:25:43 +05:30
relation.select(relation.all.table[column].sum).to_sql
2020-11-24 15:15:51 +05:30
end
2021-06-08 01:23:25 +05:30
# rubocop: disable CodeReuse/ActiveRecord
def histogram(relation, column, buckets:, bucket_size: buckets.size)
count_grouped = relation.group(column).select(Arel.star.count.as('count_grouped'))
cte = Gitlab::SQL::CTE.new(:count_cte, count_grouped)
bucket_segments = bucket_size - 1
width_bucket = Arel::Nodes::NamedFunction
.new('WIDTH_BUCKET', [cte.table[:count_grouped], buckets.first, buckets.last, bucket_segments])
.as('buckets')
query = cte
.table
.project(width_bucket, cte.table[:count])
.group('buckets')
.order('buckets')
.with(cte.to_arel)
query.to_sql
end
# rubocop: enable CodeReuse/ActiveRecord
2021-02-22 17:27:13 +05:30
# For estimated distinct count use exact query instead of hll
# buckets query, because it can't be used to obtain estimations without
# supplementary ruby code present in Gitlab::Database::PostgresHll::BatchDistinctCounter
2021-04-29 21:17:54 +05:30
def estimate_batch_distinct_count(relation, column = nil, *args, **kwargs)
2021-02-22 17:27:13 +05:30
raw_sql(relation, column, :distinct)
end
2021-04-17 20:07:23 +05:30
def add(*args)
'SELECT ' + args.map {|arg| "(#{arg})" }.join(' + ')
end
2021-06-08 01:23:25 +05:30
def maximum_id(model, column = nil)
end
def minimum_id(model, column = nil)
end
def jira_service_data
{
projects_jira_server_active: 0,
projects_jira_cloud_active: 0
}
2021-04-29 21:17:54 +05:30
end
2021-06-08 01:23:25 +05:30
def epics_deepest_relationship_level
{ epics_deepest_relationship_level: 0 }
2021-04-29 21:17:54 +05:30
end
2020-11-24 15:15:51 +05:30
private
def raw_sql(relation, column, distinct = nil)
column ||= relation.primary_key
relation.select(relation.all.table[column].count(distinct)).to_sql
end
end
end
end