File: batch_count_service.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (39 lines) | stat: -rw-r--r-- 1,275 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen_string_literal: true

# 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

    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
        end
      end

      results.transform_keys! { |cache_key| services_by_cache_key[cache_key].project }
    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