2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
# Service class for getting and caching the number of elements of several projects
|
|
|
|
# Warning: do not user this service with a really large set of projects
|
|
|
|
# because the service use maps to retrieve the project ids.
|
|
|
|
module Projects
|
|
|
|
class BatchCountService
|
|
|
|
def initialize(projects)
|
|
|
|
@projects = projects
|
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
def refresh_cache_and_retrieve_data
|
|
|
|
count_services = @projects.map { |project| count_service.new(project) }
|
|
|
|
services_by_cache_key = count_services.index_by(&:cache_key)
|
|
|
|
|
|
|
|
results = Gitlab::Instrumentation::RedisClusterValidator.allow_cross_slot_commands do
|
|
|
|
Rails.cache.fetch_multi(*services_by_cache_key.keys) do |key|
|
|
|
|
service = services_by_cache_key[key]
|
|
|
|
|
|
|
|
global_count[service.project.id].to_i
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
2021-11-11 11:23:49 +05:30
|
|
|
|
|
|
|
results.transform_keys! { |cache_key| services_by_cache_key[cache_key].project }
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def project_ids
|
|
|
|
@projects.map(&:id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def global_count(project)
|
|
|
|
raise NotImplementedError, 'global_count must be implemented and return an hash indexed by the project id'
|
|
|
|
end
|
|
|
|
|
|
|
|
def count_service
|
|
|
|
raise NotImplementedError, 'count_service must be implemented and return a Projects::CountService object'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|