Class: Generator

Inherits:
Object show all
Defined in:
lib/jirametrics/experimental/generator.rb

Instance Method Summary collapse

Constructor Details

#initializeGenerator

Returns a new instance of Generator.



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/jirametrics/experimental/generator.rb', line 122

def initialize
  @random = Random.new
  @file_prefix = 'fake'
  @target_path = 'target/'

  # @probability_work_will_be_pushed = 20
  @probability_unblocked_work_becomes_blocked = 20
  @probability_blocked_work_becomes_unblocked = 20
  @date_range = (Date.today - 500)..Date.today
  @issues = []
  @workers = []
  5.times { @workers << Worker.new }
end

Instance Method Details

#lucky?(probability) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/jirametrics/experimental/generator.rb', line 168

def lucky? probability
  @random.rand(1..100) <= probability
end

#next_issue_for(worker:, date:, type:) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/jirametrics/experimental/generator.rb', line 172

def next_issue_for worker:, date:, type:
  # First look for something I already started
  issue = @issues.find { |i| i.worker == worker && !i.done? && !i.blocked? }

  # Then look for something that someone else started
  issue = @issues.find { |i| i.worker != worker && !i.done? && !i.blocked? } if issue.nil? && lucky?(40)

  # Then start new work
  issue = FakeIssue.new(date: date, type: type, worker: worker) if issue.nil?

  issue
end

#process_date(date, _simulation_day) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/jirametrics/experimental/generator.rb', line 185

def process_date date, _simulation_day
  @issues.each do |issue|
    if issue.blocked?
      issue.unblock if lucky? @probability_blocked_work_becomes_unblocked
    elsif lucky? @probability_unblocked_work_becomes_blocked
      issue.block
    end
  end

  @workers.each do |worker|
    worker_capacity = [0, 1, 1, 1, 2].sample
    if worker.issue.nil? || worker.issue.done?
      type = lucky?(89) ? 'Story' : 'Bug'
      worker.issue = next_issue_for worker: worker, date: date, type: type
      @issues << worker.issue
    end

    worker.issue = next_issue_for worker: worker, date: date, type: type if worker.issue.blocked?
    worker.issue.do_work date: date, effort: worker_capacity
    worker.issue = nil if worker.issue.done?
  end
end

#remove_old_filesObject



158
159
160
161
162
163
164
165
166
# File 'lib/jirametrics/experimental/generator.rb', line 158

def remove_old_files
  path = "#{@target_path}#{@file_prefix}_issues"
  Dir.foreach path do |file|
    next unless file =~ /-\d+\.json$/

    filename = "#{path}/#{file}"
    File.unlink filename
  end
end

#runObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/jirametrics/experimental/generator.rb', line 136

def run
  remove_old_files
  @date_range.each_with_index do |date, day|
    yield date, day if block_given?
    process_date(date, day) if (1..5).include? date.wday # Weekday
  end

  @issues.each do |issue|
    issue.fix_change_timestamps
    File.open "target/fake_issues/#{issue.key}.json", 'w' do |file|
      file.puts JSON.pretty_generate(issue.raw)
    end
  end

  File.write 'target/fake_meta.json', JSON.pretty_generate({
    time_start: (@date_range.end - 90).to_time,
    time_end: @date_range.end.to_time,
    'no-download': true
  })
  puts "Created #{@issues.size} fake issues"
end