summaryrefslogtreecommitdiff
path: root/lib/tasks/gitlab/usage_data.rake
blob: 9f064ef4c0c817193eb499909dc8510767f00ab6 (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
# frozen_string_literal: true

namespace :gitlab do
  namespace :usage_data do
    desc 'GitLab | UsageData | Generate raw SQLs for usage ping in YAML'
    task dump_sql_in_yaml: :environment do
      puts Gitlab::Usage::ServicePingReport.for(output: :metrics_queries).to_yaml
    end

    desc 'GitLab | UsageData | Generate raw SQLs for usage ping in JSON'
    task dump_sql_in_json: :environment do
      puts Gitlab::Json.pretty_generate(Gitlab::Usage::ServicePingReport.for(output: :metrics_queries))
    end

    desc 'GitLab | UsageData | Generate usage ping in JSON'
    task generate: :environment do
      puts Gitlab::Json.pretty_generate(Gitlab::Usage::ServicePingReport.for(output: :all_metrics_values))
    end

    desc 'GitLab | UsageData | Generate usage ping and send it to Versions Application'
    task generate_and_send: :environment do
      result = ServicePing::SubmitService.new.execute

      puts Gitlab::Json.pretty_generate(result.attributes)
    end

    desc 'GitLab | UsageDataMetrics | Generate usage ping from metrics definition YAML files in JSON'
    task generate_from_yaml: :environment do
      puts Gitlab::Json.pretty_generate(Gitlab::UsageDataMetrics.uncached_data)
    end

    desc 'GitLab | UsageDataMetrics | Generate known_events/ci_templates.yml based on template definitions'
    task generate_ci_template_events: :environment do
      banner = <<~BANNER
          # This file is generated automatically by
          #   bin/rake gitlab:usage_data:generate_ci_template_events
          #
          # Do not edit it manually!
      BANNER

      repository_includes = ci_template_includes_hash(:repository_source)
      auto_devops_jobs_includes = ci_template_includes_hash(:auto_devops_source, 'Jobs')
      auto_devops_security_includes = ci_template_includes_hash(:auto_devops_source, 'Security')
      all_includes = [
        *repository_includes,
        ci_template_event('p_ci_templates_implicit_auto_devops'),
        *auto_devops_jobs_includes,
        *auto_devops_security_includes
      ]

      File.write(Gitlab::UsageDataCounters::CiTemplateUniqueCounter::KNOWN_EVENTS_FILE_PATH, banner + YAML.dump(all_includes).gsub(/ *$/m, ''))
    end

    def ci_template_includes_hash(source, template_directory = nil)
      Gitlab::UsageDataCounters::CiTemplateUniqueCounter.ci_templates("lib/gitlab/ci/templates/#{template_directory}").map do |template|
        expanded_template_name = Gitlab::UsageDataCounters::CiTemplateUniqueCounter.expand_template_name("#{template_directory}/#{template}")
        event_name = Gitlab::UsageDataCounters::CiTemplateUniqueCounter.ci_template_event_name(expanded_template_name, source)

        ci_template_event(event_name)
      end
    end

    def ci_template_event(event_name)
      {
        'name' => event_name,
        'category' => 'ci_templates',
        'redis_slot' => Gitlab::UsageDataCounters::CiTemplateUniqueCounter::REDIS_SLOT,
        'aggregation' => 'weekly'
      }
    end
  end
end