summaryrefslogtreecommitdiff
path: root/lib/gitlab/github_import/object_counter.rb
blob: 8873db24118883ca47db8989cbb76647a0ed3254 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# frozen_string_literal: true

# Count objects fetched or imported from Github.
module Gitlab
  module GithubImport
    class ObjectCounter
      OPERATIONS = %w[fetched imported].freeze
      PROJECT_COUNTER_LIST_KEY = 'github-importer/object-counters-list/%{project}/%{operation}'
      PROJECT_COUNTER_KEY = 'github-importer/object-counter/%{project}/%{operation}/%{object_type}'

      GLOBAL_COUNTER_KEY = 'github_importer_%{operation}_%{object_type}'
      GLOBAL_COUNTER_DESCRIPTION = 'The number of %{operation} Github %{object_type}'

      CACHING = Gitlab::Cache::Import::Caching

      class << self
        # Increments the project and the global counters if the given value is >= 1
        def increment(project, object_type, operation, value: 1)
          integer = value.to_i

          return if integer <= 0

          validate_operation!(operation)

          increment_project_counter(project, object_type, operation, integer)
          increment_global_counter(object_type, operation, integer)

          project.import_state&.expire_etag_cache
        end

        def summary(project)
          OPERATIONS.each_with_object({}) do |operation, result|
            result[operation] = {}

            CACHING
              .values_from_set(counter_list_key(project, operation))
              .sort
              .each do |counter|
                object_type = counter.split('/').last
                result[operation][object_type] = CACHING.read_integer(counter)
              end
          end
        end

        private

        # Global counters are long lived, in Prometheus,
        # and it's used to report the health of the Github Importer
        # in the Grafana Dashboard
        # https://dashboards.gitlab.net/d/2zgM_rImz/github-importer?orgId=1
        def increment_global_counter(object_type, operation, value)
          key = GLOBAL_COUNTER_KEY % {
            operation: operation,
            object_type: object_type
          }
          description = GLOBAL_COUNTER_DESCRIPTION % {
            operation: operation,
            object_type: object_type.to_s.humanize
          }

          Gitlab::Metrics.counter(key.to_sym, description).increment(by: value)
        end

        # Project counters are short lived, in Redis,
        # and it's used to report how successful a project
        # import was with the #summary method.
        def increment_project_counter(project, object_type, operation, value)
          counter_key = PROJECT_COUNTER_KEY % {
            project: project.id,
            operation: operation,
            object_type: object_type
          }

          add_counter_to_list(project, operation, counter_key)

          CACHING.increment_by(counter_key, value)
        end

        def add_counter_to_list(project, operation, key)
          CACHING.set_add(counter_list_key(project, operation), key)
        end

        def counter_list_key(project, operation)
          PROJECT_COUNTER_LIST_KEY % { project: project.id, operation: operation }
        end

        def validate_operation!(operation)
          unless operation.to_s.presence_in(OPERATIONS)
            raise ArgumentError, "operation must be #{OPERATIONS.join(' or ')}"
          end
        end
      end
    end
  end
end