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
|
|
|
|
def count(relation, column = nil, *rest)
|
|
|
|
raw_sql(relation, column)
|
|
|
|
end
|
|
|
|
|
|
|
|
def distinct_count(relation, column = nil, *rest)
|
|
|
|
raw_sql(relation, column, :distinct)
|
|
|
|
end
|
|
|
|
|
2020-09-15 17:09:35 -04:00
|
|
|
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
|
|
|
|
|
2020-09-16 02:09:24 -04:00
|
|
|
def sum(relation, column, *rest)
|
2020-09-30 11:09:46 -04:00
|
|
|
relation.select(relation.all.table[column].sum).to_sql
|
2020-09-16 02:09:24 -04:00
|
|
|
end
|
|
|
|
|
2020-09-09 17:08:33 -04:00
|
|
|
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
|