Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ci_template_unique_counter.rb « usage_data_counters « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: eb141a2e2f657f3d0f619532d78dcfd25f4e9cf5 (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
# frozen_string_literal: true

module Gitlab::UsageDataCounters
  class CiTemplateUniqueCounter
    PREFIX = 'ci_templates'

    class << self
      def track_unique_project_event(project:, template:, config_source:, user:)
        expanded_template_name = expand_template_name(template)
        return unless expanded_template_name

        event_name = ci_template_event_name(expanded_template_name, config_source)
        Gitlab::UsageDataCounters::HLLRedisCounter.track_event(event_name, values: project.id)

        namespace = project.namespace
        context = Gitlab::Tracking::ServicePingContext.new(data_source: :redis_hll,
                                                           event: event_name).to_context
        label = 'redis_hll_counters.ci_templates.ci_templates_total_unique_counts_monthly'
        Gitlab::Tracking.event(name, 'ci_templates_unique', namespace: namespace,
                               project: project, context: [context], user: user, label: label)
      end

      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'

        "p_#{PREFIX}_#{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
      end

      def all_included_templates(template_name)
        expanded_template_name = expand_template_name(template_name)
        results = [expanded_template_name].tap do |result|
          template = Gitlab::Template::GitlabCiYmlTemplate.find(template_name.chomp('.gitlab-ci.yml'))
          data = Gitlab::Ci::Config::Yaml::Loader.new(template.content).load.content
          [data[:include]].compact.flatten.each do |ci_include|
            if ci_include_template = ci_include[:template]
              result.concat(all_included_templates(ci_include_template))
            end
          end
        end

        results.uniq.sort_by { _1['name'] }
      end

      private

      def template_to_event_name(template)
        ActiveSupport::Inflector.parameterize(template.chomp('.gitlab-ci.yml'), separator: '_').underscore
      end
    end
  end
end