2021-06-21 05:10:07 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-07-13 11:08:38 -04:00
|
|
|
# Count objects fetched or imported from Github.
|
2021-06-21 05:10:07 -04:00
|
|
|
module Gitlab
|
|
|
|
module GithubImport
|
|
|
|
class ObjectCounter
|
|
|
|
OPERATIONS = %w[fetched imported].freeze
|
2021-07-13 11:08:38 -04:00
|
|
|
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}'
|
|
|
|
|
2021-06-21 05:10:07 -04:00
|
|
|
CACHING = Gitlab::Cache::Import::Caching
|
|
|
|
|
|
|
|
class << self
|
2021-07-22 14:08:29 -04:00
|
|
|
# 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
|
|
|
|
|
2021-06-21 05:10:07 -04:00
|
|
|
validate_operation!(operation)
|
|
|
|
|
2021-07-22 14:08:29 -04:00
|
|
|
increment_project_counter(project, object_type, operation, integer)
|
|
|
|
increment_global_counter(object_type, operation, integer)
|
2022-03-31 14:08:39 -04:00
|
|
|
|
|
|
|
project.import_state&.expire_etag_cache
|
2021-06-21 05:10:07 -04:00
|
|
|
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
|
|
|
|
|
2021-07-13 11:08:38 -04:00
|
|
|
# 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
|
2021-07-22 14:08:29 -04:00
|
|
|
def increment_global_counter(object_type, operation, value)
|
2021-07-13 11:08:38 -04:00
|
|
|
key = GLOBAL_COUNTER_KEY % {
|
|
|
|
operation: operation,
|
|
|
|
object_type: object_type
|
|
|
|
}
|
|
|
|
description = GLOBAL_COUNTER_DESCRIPTION % {
|
|
|
|
operation: operation,
|
|
|
|
object_type: object_type.to_s.humanize
|
|
|
|
}
|
|
|
|
|
2021-07-22 14:08:29 -04:00
|
|
|
Gitlab::Metrics.counter(key.to_sym, description).increment(by: value)
|
2021-07-13 11:08:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Project counters are short lived, in Redis,
|
|
|
|
# and it's used to report how successful a project
|
|
|
|
# import was with the #summary method.
|
2021-07-22 14:08:29 -04:00
|
|
|
def increment_project_counter(project, object_type, operation, value)
|
|
|
|
counter_key = PROJECT_COUNTER_KEY % {
|
|
|
|
project: project.id,
|
|
|
|
operation: operation,
|
|
|
|
object_type: object_type
|
|
|
|
}
|
2021-07-13 11:08:38 -04:00
|
|
|
|
|
|
|
add_counter_to_list(project, operation, counter_key)
|
|
|
|
|
2022-01-26 07:18:17 -05:00
|
|
|
CACHING.increment_by(counter_key, value)
|
2021-07-13 11:08:38 -04:00
|
|
|
end
|
|
|
|
|
2021-06-21 05:10:07 -04:00
|
|
|
def add_counter_to_list(project, operation, key)
|
|
|
|
CACHING.set_add(counter_list_key(project, operation), key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def counter_list_key(project, operation)
|
2021-07-13 11:08:38 -04:00
|
|
|
PROJECT_COUNTER_LIST_KEY % { project: project.id, operation: operation }
|
2021-06-21 05:10:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def validate_operation!(operation)
|
|
|
|
unless operation.to_s.presence_in(OPERATIONS)
|
2021-07-22 14:08:29 -04:00
|
|
|
raise ArgumentError, "operation must be #{OPERATIONS.join(' or ')}"
|
2021-06-21 05:10:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|