2021-03-08 18:12:59 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab::UsageDataCounters
|
|
|
|
class CiTemplateUniqueCounter
|
2021-04-29 21:17:54 +05:30
|
|
|
REDIS_SLOT = 'ci_templates'
|
2021-11-11 11:23:49 +05:30
|
|
|
KNOWN_EVENTS_FILE_PATH = File.expand_path('known_events/ci_templates.yml', __dir__)
|
2021-03-08 18:12:59 +05:30
|
|
|
|
|
|
|
class << self
|
2021-03-11 19:13:27 +05:30
|
|
|
def track_unique_project_event(project_id:, template:, config_source:)
|
2021-11-18 22:05:49 +05:30
|
|
|
expanded_template_name = expand_template_name(template)
|
|
|
|
return unless expanded_template_name
|
|
|
|
|
|
|
|
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(
|
|
|
|
ci_template_event_name(expanded_template_name, config_source), values: project_id
|
|
|
|
)
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
def ci_templates(relative_base = 'lib/gitlab/ci/templates')
|
|
|
|
Dir.glob('**/*.gitlab-ci.yml', base: Rails.root.join(relative_base))
|
|
|
|
end
|
|
|
|
|
|
|
|
def ci_template_event_name(template_name, config_source)
|
|
|
|
prefix = 'implicit_' if config_source.to_s == 'auto_devops_source'
|
2021-03-08 18:12:59 +05:30
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
"p_#{REDIS_SLOT}_#{prefix}#{template_to_event_name(template_name)}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def expand_template_name(template_name)
|
|
|
|
Gitlab::Template::GitlabCiYmlTemplate.find(template_name.chomp('.gitlab-ci.yml'))&.full_name
|
2021-11-11 11:23:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
def template_to_event_name(template)
|
|
|
|
ActiveSupport::Inflector.parameterize(template.chomp('.gitlab-ci.yml'), separator: '_').underscore
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|