Class: AgingWorkBarChart

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

Constant Summary

Constants inherited from ChartBase

ChartBase::LABEL_POSITIONS, ChartBase::OKABE_ITO_PALETTE

Instance Attribute Summary

Attributes inherited from ChartBase

#aggregated_project, #all_boards, #atlassian_document_format, #board_id, #canvas_height, #canvas_width, #data_quality, #date_range, #file_system, #fix_versions, #holiday_dates, #issues, #settings, #time_range, #timezone_offset, #x_axis_title, #y_axis_title

Instance Method Summary collapse

Methods inherited from ChartBase

#aggregated_project?, #before_run, #call_before_run, #canvas, #canvas_responsive?, #chart_format, #collapsible_issues_panel, #color_block, #color_for, #completed_issues_in_range, #current_board, #cycletime, #cycletime_for_issue, #daily_chart_dataset, #date_annotation, #describe_non_working_days, #description_text, #format_integer, #format_status, #header_text, #holidays, #html_directory, #icon_span, #link_to_issue, #next_id, #non_working_day?, #normalize_annotation_datetime, #not_visible_icon, #not_visible_text, #random_color, #render, #render_axis_title, #render_top_text, #resolve_status, #stagger_label_positions, #status_category_color, #to_human_readable, #working_days_annotation, #wrap_and_render

Constructor Details

#initialize(block) ⇒ AgingWorkBarChart

Returns a new instance of AgingWorkBarChart.



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

def initialize block
  super()

  @age_cutoff = nil
  header_text 'Aging Work Bar Chart'
  description_text <<-HTML
    <p>
      This chart shows all active (started but not completed) work, ordered from oldest at the top to
      newest at the bottom.
    </p>
    <p>
      There are <%= (aggregated_project? || current_board.scrum?) ? 'four' : 'three' %> bars for each issue, and hovering over any of the bars will provide more details.
      <ol>
        <li>Status: The status the issue was in at any time. The colour indicates the
        status category, which will be one of #{color_block '--status-category-todo-color'} To Do,
        #{color_block '--status-category-inprogress-color'} In Progress,
        or #{color_block '--status-category-done-color'} Done</li>
        <li>Activity: This bar indicates #{color_block '--blocked-color'} blocked
        or #{color_block '--stalled-color'} stalled.</li>
        <li>Priority: This shows the priority over time. If one of these priorities is considered expedited
        then it will be drawn with diagonal lines.</li>
        <% if aggregated_project? || current_board.scrum? %>
          <li>Sprints: The sprints that the issue was in.</li>
        <% end %>
      </ol>
    </p>
    #{describe_non_working_days}
  HTML

  # Because this one will size itself as needed, we start with a smaller default size
  @canvas_height = 80

  instance_eval(&block)
end

Instance Method Details

#adjust_time_date_ranges_to_start_from_earliest_issue_start(aging_issues) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 67

def adjust_time_date_ranges_to_start_from_earliest_issue_start aging_issues
  earliest_start_time = aging_issues.collect do |issue|
    issue.started_stopped_times.first
  end.min
  return if earliest_start_time.nil? || earliest_start_time >= @time_range.begin

  @time_range = earliest_start_time..@time_range.end
  @date_range = @time_range.begin.to_date..@time_range.end.to_date
end

#age_cutoff(days) ⇒ Object



325
326
327
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 325

def age_cutoff days
  @age_cutoff = days
end

#bar_chart_range_to_data_set(y_value:, ranges:, stack:, issue_start_time:) ⇒ Object



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

def bar_chart_range_to_data_set y_value:, ranges:, stack:, issue_start_time:
  ranges.filter_map do |bar_chart_range|
    next if bar_chart_range.stop < issue_start_time

    background_color = bar_chart_range.color
    if bar_chart_range.highlight
      background_color = RawJavascript.new("createDiagonalPattern(#{background_color.to_json})")
    end

    {
      type: 'bar',
      data: [{
        x: [chart_format([bar_chart_range.start, issue_start_time].max), chart_format(bar_chart_range.stop)],
        y: y_value,
        title: bar_chart_range.title
      }],
      backgroundColor: background_color,
      borderColor: CssVariable['--aging-work-bar-chart-separator-color'],
      borderWidth: {
         top: 0,
         right: 1,
         bottom: 0,
         left: 0
      },
      stacked: true,
      stack: stack
    }
  end
end

#blocked_stalled_range(starting_change, change) ⇒ Object



209
210
211
212
213
214
215
216
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 209

def blocked_stalled_range starting_change, change
  color = '--blocked-color'
  color = '--stalled-color' if starting_change.stalled?

  BarChartRange.new(
    start: starting_change.time, stop: change.time, color: CssVariable[color], title: starting_change.reasons
  )
end

#calculate_percent_line(percentage: 85) ⇒ Object



318
319
320
321
322
323
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 318

def calculate_percent_line percentage: 85
  days = completed_issues_in_range.filter_map { |issue| issue.board.cycletime.cycletime(issue) }.sort
  return nil if days.empty?

  days[days.length * percentage / 100]
end

#clip_ranges_to_start_time(ranges:, issue_start_time:) ⇒ Object



122
123
124
125
126
127
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 122

def clip_ranges_to_start_time ranges:, issue_start_time:
  return if issue_start_time.nil?

  ranges.each { |range| range.start = issue_start_time if range.start < issue_start_time }
  ranges.reject! { |range| range.start >= range.stop }
end

#close_removed_sprints(change, open_sprints, results) ⇒ Object

For each sprint this change leaves, close its open range. The range stops when the issue left, or when the sprint completed if that came first.



272
273
274
275
276
277
278
279
280
281
282
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 272

def close_removed_sprints change, open_sprints, results
  removed_sprint_ids = change.old_value_id - change.value_id
  removed_sprint_ids.each do |id|
    data = open_sprints.delete(id)
    next unless data

    completed = data[:sprint].completed_time
    stop = completed ? [change.time, completed].min : change.time
    results << sprint_range(data, stop)
  end
end

#collect_blocked_stalled_ranges(issue:, issue_start_time:) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 193

def collect_blocked_stalled_ranges issue:, issue_start_time:
  results = []
  starting_change = nil

  issue.blocked_stalled_changes(end_time: time_range.end).each do |change|
    if starting_change.nil? || starting_change.active?
      starting_change = change
      next
    end

    results << blocked_stalled_range(starting_change, change) if change.time >= issue_start_time
    starting_change = change
  end
  results
end

#collect_priority_ranges(issue:) ⇒ Object



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

def collect_priority_ranges issue:
  expedited_priority_names = settings['expedited_priority_names']

  previous_change = nil
  results = []

  issue.changes.each do |change|
    next unless change.priority?

    if previous_change.nil?
      previous_change = change
      next
    end

    results << create_range_for_priority(
      previous_change: previous_change, stop_time: change.time,
      expedited_priority_names: expedited_priority_names
    )
    previous_change = change
  end

  if previous_change
    results << create_range_for_priority(
      previous_change: previous_change, stop_time: time_range.end,
      expedited_priority_names: expedited_priority_names
    )
  end
  results
end

#collect_sprint_ranges(issue:) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 248

def collect_sprint_ranges issue:
  results = []
  open_sprints = {}

  issue.changes.each do |change|
    next unless change.sprint?

    close_removed_sprints(change, open_sprints, results)
    open_added_sprints(change, issue, open_sprints)
  end

  # Anything still open at the end runs to the sprint's completion, or the end of the range if it's
  # still active.
  open_sprints.each_value do |data|
    next if data[:sprint].future?

    results << sprint_range(data, data[:sprint].completed_time || time_range.end)
  end

  results
end

#collect_status_ranges(issue:, now:) ⇒ Object



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

def collect_status_ranges issue:, now:
  ranges = []
  issue_started_time = issue.started_stopped_times.first
  previous_start = nil
  previous_status = nil
  issue.status_changes.each do |change|
    new_status = issue.find_or_create_status id: change.value_id, name: change.value
    if previous_start.nil?
      previous_start = change.time
      previous_status = new_status
      next
    end

    previous_start = issue_started_time if issue_started_time > previous_start

    ranges << BarChartRange.new(
      start: previous_start,
      stop: change.time,
      color: status_category_color(previous_status),
      title: previous_status.to_s
    )
    previous_start = change.time
    previous_status = new_status
  end

  ranges << BarChartRange.new(
    start: previous_start,
    stop: now,
    color: status_category_color(previous_status),
    title: previous_status.to_s
  )
  ranges
end

#create_range_for_priority(previous_change:, stop_time:, expedited_priority_names:) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 304

def create_range_for_priority previous_change:, stop_time:, expedited_priority_names:
  expedited = expedited_priority_names.include?(previous_change.value)
  title = "Priority: #{previous_change.value}"
  title << ' (expedited)' if expedited

  BarChartRange.new(
    start: previous_change.time,
    stop: stop_time,
    color: CssVariable["--priority-color-#{previous_change.value.downcase.gsub(/\s/, '')}"],
    title: title,
    highlight: expedited
  )
end

#data_sets_for_one_issue(issue:, today:) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 77

def data_sets_for_one_issue issue:, today:
  cycletime = issue.board.cycletime
  issue_start_time = cycletime.started_stopped_times(issue).first
  end_of_today = Time.parse("#{today}T23:59:59#{@timezone_offset}")

  bar_data = [
    ['status', collect_status_ranges(issue: issue, now: end_of_today)],
    ['blocked', collect_blocked_stalled_ranges(issue: issue, issue_start_time: issue_start_time)],
    ['priority', collect_priority_ranges(issue: issue)]
  ]
  bar_data << ['sprints', collect_sprint_ranges(issue: issue)] if aggregated_project? || current_board.scrum?

  bar_data.each { |entry| clip_ranges_to_start_time(ranges: entry.last, issue_start_time: issue_start_time) }

  issue_label = "[#{label_days cycletime.age(issue, today: today)}] #{issue.key}: #{issue.summary}"[0..60]
  bar_data.collect do |stack, ranges|
    bar_chart_range_to_data_set y_value: issue_label, ranges: ranges, stack: stack, issue_start_time: issue_start_time
  end
end

#grow_chart_height_if_too_many_issues(aging_issue_count:) ⇒ Object



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

def grow_chart_height_if_too_many_issues aging_issue_count:
  px_per_bar = 10
  bars_per_issue = 3
  bars_per_issue += 1 if aggregated_project? || current_board.scrum?

  preferred_height = aging_issue_count * px_per_bar * bars_per_issue
  @canvas_height = preferred_height if @canvas_height.nil? || @canvas_height < preferred_height
end

#open_added_sprints(change, issue, open_sprints) ⇒ Object

For each sprint this change joins (that exists and has already started), open a range. It starts when the issue joined, or when the sprint began if the issue was added beforehand.



286
287
288
289
290
291
292
293
294
295
296
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 286

def open_added_sprints change, issue, open_sprints
  added_sprint_ids = change.value_id - change.old_value_id
  added_sprint_ids.each do |id|
    sprint = issue.board.sprints.find { |s| s.id == id }
    next unless sprint
    next if sprint.future?

    start_time = [sprint.start_time, change.time].max
    open_sprints[id] = { start_time: start_time, sprint: sprint }
  end
end

#runObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 42

def run
  aging_issues = select_aging_issues issues: @issues
  adjust_time_date_ranges_to_start_from_earliest_issue_start(aging_issues)

  today = date_range.end
  sort_by_age! issues: aging_issues, today: today

  grow_chart_height_if_too_many_issues aging_issue_count: aging_issues.size

  data_sets = aging_issues
    .collect { |issue| data_sets_for_one_issue issue: issue, today: today }
    .flatten
    .compact

  percentage = calculate_percent_line
  percentage_line_x = date_range.end - calculate_percent_line if percentage

  if aging_issues.empty?
    @description_text = '<p>There is no aging work</p>'
    return render_top_text(binding)
  end

  wrap_and_render(binding, __FILE__)
end

#select_aging_issues(issues:) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 103

def select_aging_issues issues:
  issues.select do |issue|
    cycletime = issue.board.cycletime
    next false unless cycletime.in_progress?(issue)

    age = cycletime.age(issue, today: date_range.end)
    !(@age_cutoff && @age_cutoff >= age)
  end
end

#sort_by_age!(issues:, today:) ⇒ Object



97
98
99
100
101
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 97

def sort_by_age! issues:, today:
  issues.sort! do |a, b|
    b.board.cycletime.age(b, today: today) <=> a.board.cycletime.age(a, today: today)
  end
end

#sprint_range(data, stop) ⇒ Object



298
299
300
301
302
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 298

def sprint_range data, stop
  BarChartRange.new(
    start: data[:start_time], stop: stop, color: CssVariable['--sprint-color'], title: data[:sprint].name
  )
end