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
|
# frozen_string_literal: true
module Gitlab
class Seeder
extend ActionView::Helpers::NumberHelper
MASS_INSERT_PREFIX = 'mass_insert'
MASS_INSERT_PROJECT_START = "#{MASS_INSERT_PREFIX}_project_"
MASS_INSERT_GROUP_START = "#{MASS_INSERT_PREFIX}_group_"
MASS_INSERT_USER_START = "#{MASS_INSERT_PREFIX}_user_"
REPORTED_USER_START = 'reported_user_'
ESTIMATED_INSERT_PER_MINUTE = 250_000
MASS_INSERT_ENV = 'MASS_INSERT'
module NamespaceSeed
extend ActiveSupport::Concern
included do
scope :not_mass_generated, -> do
where.not("path LIKE '#{MASS_INSERT_GROUP_START}%'")
end
end
end
module ProjectSeed
extend ActiveSupport::Concern
included do
scope :not_mass_generated, -> do
where.not("path LIKE '#{MASS_INSERT_PROJECT_START}%'")
end
end
end
module UserSeed
extend ActiveSupport::Concern
included do
scope :not_mass_generated, -> do
where.not("username LIKE '#{MASS_INSERT_USER_START}%' OR username LIKE '#{REPORTED_USER_START}%'")
end
end
end
def self.log_message(message)
puts "#{Time.current}: #{message}"
end
def self.with_mass_insert(size, model)
humanized_model_name = model.is_a?(String) ? model : model.model_name.human.pluralize(size)
if !ENV[MASS_INSERT_ENV] && !ENV['CI']
puts "\nSkipping mass insertion for #{humanized_model_name}."
puts "Consider running the seed with #{MASS_INSERT_ENV}=1"
return
end
humanized_size = number_with_delimiter(size)
estimative = estimated_time_message(size)
puts "\nCreating #{humanized_size} #{humanized_model_name}."
puts estimative
yield
puts "\n#{number_with_delimiter(size)} #{humanized_model_name} created!"
end
def self.estimated_time_message(size)
estimated_minutes = (size.to_f / ESTIMATED_INSERT_PER_MINUTE).round
humanized_minutes = 'minute'.pluralize(estimated_minutes)
if estimated_minutes == 0
"Rough estimated time: less than a minute ⏰"
else
"Rough estimated time: #{estimated_minutes} #{humanized_minutes} ⏰"
end
end
def self.quiet
# Additional seed logic for models.
Namespace.include(NamespaceSeed)
Project.include(ProjectSeed)
User.include(UserSeed)
old_perform_deliveries = ActionMailer::Base.perform_deliveries
ActionMailer::Base.perform_deliveries = false
SeedFu.quiet = true
without_database_logging do
without_statement_timeout do
without_new_note_notifications do
yield
end
end
end
puts "\nOK".color(:green)
ensure
SeedFu.quiet = false
ActionMailer::Base.perform_deliveries = old_perform_deliveries
end
def self.without_gitaly_timeout
# Remove Gitaly timeout
old_timeout = Gitlab::CurrentSettings.current_application_settings.gitaly_timeout_default
Gitlab::CurrentSettings.current_application_settings.update_columns(gitaly_timeout_default: 0)
# Otherwise we still see the default value when running seed_fu
ApplicationSetting.expire
yield
ensure
Gitlab::CurrentSettings.current_application_settings.update_columns(gitaly_timeout_default: old_timeout)
ApplicationSetting.expire
end
def self.without_new_note_notifications
NotificationService.alias_method :original_new_note, :new_note
NotificationService.define_method(:new_note) { |note| }
yield
ensure
NotificationService.alias_method :new_note, :original_new_note
NotificationService.remove_method :original_new_note
end
def self.without_statement_timeout
Gitlab::Database::EachDatabase.each_database_connection do |connection|
connection.execute('SET statement_timeout=0')
end
yield
ensure
Gitlab::Database::EachDatabase.each_database_connection do |connection|
connection.execute('RESET statement_timeout')
end
end
def self.without_database_logging
old_loggers = Gitlab::Database.database_base_models.transform_values do |model|
model.logger
end
Gitlab::Database.database_base_models.each do |_, model|
model.logger = nil
end
yield
ensure
Gitlab::Database.database_base_models.each do |connection_name, model|
model.logger = old_loggers[connection_name]
end
end
module Ci
class DailyBuildGroupReportResult
DEFAULT_BRANCH = 'master'
COUNT_OF_DAYS = 5
def initialize(project)
@project = project
@last_pipeline = project.last_pipeline
end
def seed
COUNT_OF_DAYS.times do |count|
date = Time.now.utc - count.day
create_report(date)
end
end
private
attr_reader :project, :last_pipeline
def create_report(date)
last_pipeline.builds.uniq(&:group_name).each do |build|
::Ci::DailyBuildGroupReportResult.create(
project: project,
last_pipeline: last_pipeline,
date: date,
ref_path: last_pipeline.source_ref_path,
group_name: build.group_name,
data: {
'coverage' => rand(20..99)
},
group: project.group,
default_branch: last_pipeline.default_branch?
)
rescue ActiveRecord::RecordNotUnique
return false
end
end
end
end
end
end
# :nocov:
|