Class: SprintBurndown

Inherits:
ChartBase show all
Defined in:
lib/jirametrics/sprint_burndown.rb

Instance Attribute Summary collapse

Attributes inherited from ChartBase

#aggregated_project, #all_boards, #canvas_height, #canvas_width, #data_quality, #date_range, #file_system, #holiday_dates, #issues, #settings, #time_range, #timezone_offset

Instance Method Summary collapse

Methods inherited from ChartBase

#aggregated_project?, #canvas, #canvas_responsive?, #chart_format, #collapsible_issues_panel, #color_block, #color_for, #completed_issues_in_range, #current_board, #daily_chart_dataset, #describe_non_working_days, #description_text, #format_integer, #format_status, #header_text, #holidays, #icon_span, #label_days, #label_issues, #link_to_issue, #next_id, #random_color, #render, #render_top_text, #status_category_color, #wrap_and_render

Constructor Details

#initializeSprintBurndown

Returns a new instance of SprintBurndown.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jirametrics/sprint_burndown.rb', line 20

def initialize
  super()

  @summary_stats = {}
  header_text 'Sprint burndown'
  description_text <<-TEXT
    <div class="p">
      Burndowns for all sprints in this time period. The different colours are only to
      differentiate one sprint from another as they may overlap time periods.
    </div>
    #{describe_non_working_days}
  TEXT
end

Instance Attribute Details

#board_idObject

Returns the value of attribute board_id.



18
19
20
# File 'lib/jirametrics/sprint_burndown.rb', line 18

def board_id
  @board_id
end

#use_story_countsObject (readonly)

Returns the value of attribute use_story_counts.



17
18
19
# File 'lib/jirametrics/sprint_burndown.rb', line 17

def use_story_counts
  @use_story_counts
end

#use_story_pointsObject (readonly)

Returns the value of attribute use_story_points.



17
18
19
# File 'lib/jirametrics/sprint_burndown.rb', line 17

def use_story_points
  @use_story_points
end

Instance Method Details

#changes_for_one_issue(issue:, sprint:) ⇒ Object

select all the changes that are relevant for the sprint. If this issue never appears in this sprint then return [].



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
# File 'lib/jirametrics/sprint_burndown.rb', line 123

def changes_for_one_issue issue:, sprint:
  story_points = 0.0
  ever_in_sprint = false
  currently_in_sprint = false
  change_data = []

  issue_completed_time = issue.board.cycletime.stopped_time(issue)
  completed_has_been_tracked = false

  issue.changes.each do |change|
    action = nil
    value = nil

    if change.sprint?
      # We can get two sprint changes in a row that tell us the same thing so we have to verify
      # that something actually changed.
      in_change_item = sprint_in_change_item(sprint, change)
      if currently_in_sprint == false && in_change_item
        action = :enter_sprint
        ever_in_sprint = true
        value = story_points
      elsif currently_in_sprint && in_change_item == false
        action = :leave_sprint
        value = -story_points
      end
      currently_in_sprint = in_change_item
    elsif change.story_points? && (issue_completed_time.nil? || change.time < issue_completed_time)
      action = :story_points
      story_points = change.value.to_f
      value = story_points - change.old_value.to_f
    elsif completed_has_been_tracked == false && change.time == issue_completed_time
      completed_has_been_tracked = true
      action = :issue_stopped
      value = -story_points
    end

    next unless action

    change_data << SprintIssueChangeData.new(
      time: change.time, issue: issue, action: action, value: value, story_points: story_points
    )
  end

  return [] unless ever_in_sprint

  change_data
end

#data_set_by_story_counts(sprint:, change_data_for_sprint:) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/jirametrics/sprint_burndown.rb', line 272

def data_set_by_story_counts sprint:, change_data_for_sprint:
  summary_stats = SprintSummaryStats.new

  data_set = []
  issues_currently_in_sprint = []
  start_data_written = false

  change_data_for_sprint.each do |change_data|
    if start_data_written == false && change_data.time >= sprint.start_time
      data_set << {
        y: issues_currently_in_sprint.size,
        x: chart_format(sprint.start_time),
        title: "Sprint started with #{issues_currently_in_sprint.size} stories"
      }
      summary_stats.started = issues_currently_in_sprint.size
      start_data_written = true
    end

    break if sprint.completed_time && change_data.time > sprint.completed_time

    case change_data.action
    when :enter_sprint
      issues_currently_in_sprint << change_data.issue.key
    when :leave_sprint, :issue_stopped
      issues_currently_in_sprint.delete change_data.issue.key
    end

    next unless change_data.time >= sprint.start_time

    message = nil
    case change_data.action
    when :enter_sprint
      message = 'Added to sprint'
      summary_stats.added += 1
    when :issue_stopped
      message = 'Completed'
      summary_stats.completed += 1
    when :leave_sprint
      message = 'Removed from sprint'
      summary_stats.removed += 1
    end

    next unless message

    data_set << {
      y: issues_currently_in_sprint.size,
      x: chart_format(change_data.time),
      title: "#{change_data.issue.key} #{message}"
    }
  end

  unless start_data_written
    # There was nothing that triggered us to write the sprint started block so do it now.
    data_set << {
      y: issues_currently_in_sprint.size,
      x: chart_format(sprint.start_time),
      title: "Sprint started with #{issues_currently_in_sprint.size || 'no'} stories"
    }
  end

  if sprint.completed_time
    data_set << {
      y: issues_currently_in_sprint.size,
      x: chart_format(sprint.completed_time),
      title: "Sprint ended with #{issues_currently_in_sprint.size} stories unfinished"
    }
    summary_stats.remaining = issues_currently_in_sprint.size
  end

  unless sprint.completed_at?(time_range.end)
    # If the sprint is still active then we draw one final line to the end of the time range
    data_set << {
      y: issues_currently_in_sprint.size,
      x: chart_format(time_range.end),
      title: "Sprint still active. #{issues_currently_in_sprint.size} issues in progress."
    }
  end

  @summary_stats[sprint] = summary_stats
  data_set
end

#data_set_by_story_points(sprint:, change_data_for_sprint:) ⇒ Object



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
# File 'lib/jirametrics/sprint_burndown.rb', line 175

def data_set_by_story_points sprint:, change_data_for_sprint:
  summary_stats = SprintSummaryStats.new
  summary_stats.completed = 0.0

  story_points = 0.0
  start_data_written = false
  data_set = []

  issues_currently_in_sprint = []

  change_data_for_sprint.each do |change_data|
    if start_data_written == false && change_data.time >= sprint.start_time
      data_set << {
        y: story_points,
        x: chart_format(sprint.start_time),
        title: "Sprint started with #{story_points} points"
      }
      summary_stats.started = story_points
      start_data_written = true
    end

    break if sprint.completed_time && change_data.time > sprint.completed_time

    case change_data.action
    when :enter_sprint
      issues_currently_in_sprint << change_data.issue.key
      story_points += change_data.story_points
    when :leave_sprint
      issues_currently_in_sprint.delete change_data.issue.key
      story_points -= change_data.story_points
    when :story_points
      story_points += change_data.value if issues_currently_in_sprint.include? change_data.issue.key
    end

    next unless change_data.time >= sprint.start_time

    message = nil
    case change_data.action
    when :story_points
      next unless issues_currently_in_sprint.include? change_data.issue.key

      old_story_points = change_data.story_points - change_data.value
      message = "Story points changed from #{old_story_points} points to #{change_data.story_points} points"
      summary_stats.points_values_changed = true
    when :enter_sprint
      message = "Added to sprint with #{change_data.story_points || 'no'} points"
      summary_stats.added += change_data.story_points
    when :issue_stopped
      story_points -= change_data.story_points
      message = "Completed with #{change_data.story_points || 'no'} points"
      issues_currently_in_sprint.delete change_data.issue.key
      summary_stats.completed += change_data.story_points
    when :leave_sprint
      message = "Removed from sprint with #{change_data.story_points || 'no'} points"
      summary_stats.removed += change_data.story_points
    else
      raise "Unexpected action: #{change_data.action}"
    end

    data_set << {
      y: story_points,
      x: chart_format(change_data.time),
      title: "#{change_data.issue.key} #{message}"
    }
  end

  unless start_data_written
    # There was nothing that triggered us to write the sprint started block so do it now.
    data_set << {
      y: story_points,
      x: chart_format(sprint.start_time),
      title: "Sprint started with #{story_points} points"
    }
    summary_stats.started = story_points
  end

  if sprint.completed_time
    data_set << {
      y: story_points,
      x: chart_format(sprint.completed_time),
      title: "Sprint ended with #{story_points} points unfinished"
    }
    summary_stats.remaining = story_points
  end

  unless sprint.completed_at?(time_range.end)
    data_set << {
      y: story_points,
      x: chart_format(time_range.end),
      title: "Sprint still active. #{story_points} points still in progress."
    }
  end

  @summary_stats[sprint] = summary_stats
  data_set
end

#options=(arg) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jirametrics/sprint_burndown.rb', line 34

def options= arg
  case arg
  when :points_only
    @use_story_points = true
    @use_story_counts = false
  when :counts_only
    @use_story_points = false
    @use_story_counts = true
  when :points_and_counts
    @use_story_points = true
    @use_story_counts = true
  else
    raise "Unexpected option: #{arg}"
  end
end

#runObject



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
# File 'lib/jirametrics/sprint_burndown.rb', line 50

def run
  sprints = sprints_in_time_range all_boards[board_id]
  return nil if sprints.empty?

  change_data_by_sprint = {}
  sprints.each do |sprint|
    change_data = []
    issues.each do |issue|
      change_data += changes_for_one_issue(issue: issue, sprint: sprint)
    end
    change_data_by_sprint[sprint] = change_data.sort_by(&:time)
  end

  result = +''
  result << render_top_text(binding)

  possible_colours = (1..5).collect { |i| CssVariable["--sprint-burndown-sprint-color-#{i}"] }
  charts_to_generate = []
  charts_to_generate << [:data_set_by_story_points, 'Story Points'] if @use_story_points
  charts_to_generate << [:data_set_by_story_counts, 'Story Count'] if @use_story_counts
  charts_to_generate.each do |data_method, y_axis_title| # rubocop:disable Style/HashEachMethods
    @summary_stats.clear
    data_sets = []
    sprints.each_with_index do |sprint, index|
      color = possible_colours[index % possible_colours.size]
      label = sprint.name
      data = send(data_method, sprint: sprint, change_data_for_sprint: change_data_by_sprint[sprint])
      data_sets << {
        label: label,
        data: data,
        fill: false,
        showLine: true,
        borderColor: color,
        backgroundColor: color,
        stepped: true,
        pointStyle: %w[rect circle] # First dot is visually different from the rest
      }
    end

    legend = []
    case data_method
    when :data_set_by_story_counts
      legend << '<b>Started</b>: Number of issues already in the sprint, when the sprint was started.'
      legend << '<b>Completed</b>: Number of issues, completed during the sprint'
      legend << '<b>Added</b>: Number of issues added in the middle of the sprint'
      legend << '<b>Removed</b>: Number of issues removed while the sprint was in progress'
    when :data_set_by_story_points
      legend << '<b>Started</b>: Total count of story points when the sprint was started'
      legend << '<b>Completed</b>: Count of story points completed during the sprint'
      legend << '<b>Added</b>: Count of story points added in the middle of the sprint'
      legend << '<b>Removed</b>: Count of story points removed while the sprint was in progress'
    else
      raise "Unexpected method #{data_method}"
    end

    result << render(binding, __FILE__)
  end

  result
end

#sprint_in_change_item(sprint, change_item) ⇒ Object



171
172
173
# File 'lib/jirametrics/sprint_burndown.rb', line 171

def sprint_in_change_item sprint, change_item
  change_item.raw['to'].split(/\s*,\s*/).any? { |id| id.to_i == sprint.id }
end

#sprints_in_time_range(board) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/jirametrics/sprint_burndown.rb', line 111

def sprints_in_time_range board
  board.sprints.select do |sprint|
    sprint_end_time = sprint.completed_time || sprint.end_time
    sprint_start_time = sprint.start_time
    next false if sprint_start_time.nil?

    time_range.include?(sprint_start_time) || time_range.include?(sprint_end_time) ||
      (sprint_start_time < time_range.begin && sprint_end_time > time_range.end)
  end || []
end