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

17_cycle_analytics.rb « development « fixtures « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5442c6eb00bbe52f3002dca873a8394f35dd2419 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# frozen_string_literal: true

require './spec/support/sidekiq_middleware'
require './spec/support/helpers/test_env'
require 'active_support/testing/time_helpers'
require './spec/support/helpers/cycle_analytics_helpers'
require './ee/db/seeds/shared/dora_metrics' if Gitlab.ee?

# Usage:
#
# Simple invocation always creates a new project:
#
# FILTER=cycle_analytics SEED_VSA=1 bundle exec rake db:seed_fu
#
# Create more issues/MRs:
#
# VSA_ISSUE_COUNT=100 FILTER=cycle_analytics SEED_VSA=1 bundle exec rake db:seed_fu
#
# Run for an existing project
#
# VSA_SEED_PROJECT_ID=10 FILTER=cycle_analytics SEED_VSA=1 bundle exec rake db:seed_fu

# rubocop:disable Rails/Output
class Gitlab::Seeder::CycleAnalytics # rubocop:disable Style/ClassAndModuleChildren
  include ActiveSupport::Testing::TimeHelpers
  include CycleAnalyticsHelpers

  attr_reader :project, :issues, :merge_requests, :developers

  FLAG = 'SEED_VSA'
  PERF_TEST = 'VSA_PERF_TEST'

  MAX_DURATIONS = { # in hours
    issue: 72,
    plan: 48,
    code: 72,
    test: 5,
    review: 72,
    deployment: 48,
    lead_time: 32
  }.freeze

  def self.seeder_based_on_env(project)
    if ENV[FLAG]
      new(project: project)
    elsif ENV[PERF_TEST]
      new(project: project, perf: true)
    end
  end

  def initialize(project: nil, perf: false)
    @project = project || create_new_vsm_project
    @issue_count = perf ? 1000 : ENV.fetch('VSA_ISSUE_COUNT', 5).to_i
    @issues = []
    @merge_requests = []
    @developers = []
  end

  def seed!
    unless project.repository_exists?
      puts
      puts 'WARNING'
      puts '======='
      puts "Seeding #{self.class} is not possible because the given project " \
           "(#{project.full_path}) doesn't have a repository."
      puts 'Try specifying a project with working repository or omit the VSA_SEED_PROJECT_ID parameter ' \
           'so the seed script will automatically create one.'
      puts

      return
    end

    seed_data!
  end

  private

  def seed_data!
    Sidekiq::Worker.skipping_transaction_check do
      create_developers!
      create_issues!

      seed_lead_time!
      seed_issue_stage!
      seed_plan_stage!
      seed_code_stage!
      seed_test_stage!
      seed_review_stage!
      seed_staging_stage!

      if Gitlab.ee?
        create_vulnerabilities_count_report!
        seed_dora_metrics!
        create_custom_value_stream!
        create_value_stream_aggregation(project.group)
      end

      puts "Successfully seeded '#{project.full_path}' for Value Stream Management!"
      puts "URL: #{Rails.application.routes.url_helpers.project_url(project)}"
    end
  end

  def create_custom_value_stream!
    [project.project_namespace.reload, project.group].each do |parent|
      Analytics::CycleAnalytics::ValueStreams::CreateService.new(
        current_user: admin,
        namespace: parent,
        params: { name: "vs #{suffix}", stages: Gitlab::Analytics::CycleAnalytics::DefaultStages.all }
      ).execute
    end
  end

  def seed_dora_metrics!
    Gitlab::Seeder::DoraMetrics.new(project: project).execute
  end

  def seed_issue_stage!
    issues.each do |issue|
      time = within_end_time(issue.created_at + rand(MAX_DURATIONS[:issue]).hours)

      if issue.id.even?
        issue.metrics.update!(first_associated_with_milestone_at: time)
      else
        issue.metrics.update!(first_added_to_board_at: time)
      end
    end
  end

  def seed_plan_stage!
    issues.each do |issue|
      plan_stage_start = issue.metrics.first_associated_with_milestone_at || issue.metrics.first_added_to_board_at

      first_mentioned_in_commit_at = within_end_time(plan_stage_start + rand(MAX_DURATIONS[:plan]).hours)
      issue.metrics.update!(first_mentioned_in_commit_at: first_mentioned_in_commit_at)
    end
  end

  def seed_code_stage!
    issues.each do |issue|
      merge_request = FactoryBot.create(
        :merge_request,
        target_project: project,
        source_project: project,
        source_branch: "#{issue.iid}-feature-branch",
        target_branch: 'master',
        author: developers.sample,
        created_at: within_end_time(issue.metrics.first_mentioned_in_commit_at + rand(MAX_DURATIONS[:code]).hours)
      )

      @merge_requests << merge_request

      MergeRequestsClosingIssues.create!(issue: issue, merge_request: merge_request)
    end
  end

  def seed_test_stage!
    merge_requests.each do |merge_request|
      pipeline = FactoryBot.create(:ci_pipeline, :success, project: project,
        partition_id: Ci::Pipeline.current_partition_value)
      build = FactoryBot.create(:ci_build, pipeline: pipeline, project: project, user: developers.sample)

      # Required because seeds run in a transaction and these are now
      # created in an `after_commit` hook.
      merge_request.ensure_metrics!

      merge_request.metrics.update!(
        latest_build_started_at: merge_request.created_at,
        latest_build_finished_at: within_end_time(merge_request.created_at + MAX_DURATIONS[:test].hours),
        pipeline_id: build.commit_id
      )
    end
  end

  def seed_review_stage!
    merge_requests.each do |merge_request|
      merge_request.metrics.update!(
        merged_at: within_end_time(merge_request.created_at + MAX_DURATIONS[:review].hours)
      )
    end
  end

  def seed_staging_stage!
    merge_requests.each do |merge_request|
      first_deployed_to_production_at = merge_request.metrics.merged_at + MAX_DURATIONS[:deployment].hours
      merge_request.metrics.update!(
        first_deployed_to_production_at: within_end_time(first_deployed_to_production_at)
      )
    end
  end

  def seed_lead_time!
    issues.each do |issue|
      created_at = issue.created_at - MAX_DURATIONS[:lead_time].hours
      issue.update!(created_at: created_at, closed_at: Time.now)
    end
  end

  def create_issues!
    @issue_count.times do
      travel_to(start_time + rand(5).days) do
        title = "#{FFaker::Product.brand}-#{suffix}"
        @issues << Issue.create!(project: project, title: title, author: developers.sample)
      end
    end
  end

  def create_vulnerabilities_count_report!
    4.times do |i|
      critical_count = rand(5..10)
      high_count = rand(5..10)

      [i.months.ago.end_of_month, i.months.ago.beginning_of_month].each do |date|
        FactoryBot.create(:vulnerability_historical_statistic,
          date: date,
          total: critical_count + high_count,
          critical: critical_count,
          high: high_count,
          project: project
        )
      end
    end
  end

  def create_developers!
    5.times do |i|
      user = FactoryBot.create(
        :user,
        name: "VSM User#{i}",
        username: "vsm-user-#{i}-#{suffix}",
        email: "vsm-user-#{i}@#{suffix}.com"
      )

      project.group&.add_developer(user)
      project.add_developer(user)

      @developers << user
    end

    AuthorizedProjectUpdate::ProjectRecalculateService.new(project).execute
  end

  def create_new_vsm_project
    namespace = FactoryBot.create(
      :group,
      name: "Value Stream Management Group #{suffix}",
      path: "vsmg-#{suffix}"
    )
    project = FactoryBot.create(
      :project,
      :repository,
      name: "Value Stream Management Project #{suffix}",
      path: "vsmp-#{suffix}",
      creator: admin,
      namespace: namespace
    )

    project.create_repository
    project
  end

  def admin
    @admin ||= User.admins.first
  end

  def suffix
    @suffix ||= Time.now.to_i
  end

  def start_time
    @start_time ||= 25.days.ago
  end

  def end_time
    @end_time ||= Time.now
  end

  def within_end_time(time)
    [time, end_time].min
  end
end

Gitlab::Seeder.quiet do
  project_id = ENV['VSA_SEED_PROJECT_ID']
  project = Project.find(project_id) if project_id

  seeder = Gitlab::Seeder::CycleAnalytics.seeder_based_on_env(project)

  if seeder
    seeder.seed!
  else
    puts "Skipped. Use the `#{Gitlab::Seeder::CycleAnalytics::FLAG}` environment variable to enable."
  end
end
# rubocop:enable Rails/Output