Class: AgingWorkBarChart

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

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, #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, #html_directory, #icon_span, #label_days, #label_issues, #link_to_issue, #next_id, #random_color, #render, #render_top_text, #status_category_color, #working_days_annotation, #wrap_and_render

Constructor Details

#initialize(block) ⇒ AgingWorkBarChart

Returns a new instance of AgingWorkBarChart.



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

def initialize block
  super()

  header_text 'Aging Work Bar Chart'
  description_text "    <p>\n      This chart shows all active (started but not completed) work, ordered from oldest at the top to\n      newest at the bottom.\n    </p>\n    <p>\n      There are potentially three bars for each issue, although a bar may be missing if the issue has no\n      information relevant to that. Hovering over any of the bars will provide more details.\n      <ol>\n        <li>The top bar tells you what status the issue is in at any time. The colour indicates the\n        status category, which will be one of \#{color_block '--status-category-todo-color'} To Do,\n        \#{color_block '--status-category-inprogress-color'} In Progress,\n        or \#{color_block '--status-category-done-color'} Done</li>\n        <li>The middle bar indicates \#{color_block '--blocked-color'} blocked\n        or \#{color_block '--stalled-color'} stalled.</li>\n        <li>The bottom bar indicated \#{color_block '--expedited-color'} expedited.</li>\n      </ol>\n    </p>\n    \#{describe_non_working_days}\n  HTML\n\n  # Because this one will size itself as needed, we start with a smaller default size\n  @canvas_height = 80\n\n  instance_eval(&block)\nend\n"

Instance Method Details

#blocked_data_sets(issue:, issue_label:, issue_start_time:, stack:) ⇒ Object



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

def blocked_data_sets issue:, issue_label:, issue_start_time:, stack:
  data_sets = []
  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

    if change.time >= issue_start_time
      data_sets << one_block_change_data_set(
        starting_change: starting_change, ending_time: change.time,
        issue_label: issue_label, stack: stack, issue_start_time: issue_start_time
      )
    end

    starting_change = change
  end

  data_sets
end

#calculate_percent_line(percentage: 85) ⇒ Object



252
253
254
255
256
257
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 252

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

#data_set_by_block(issue:, issue_label:, title_label:, stack:, color:, start_date:, end_date: date_range.end) ⇒ Object



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

def data_set_by_block(
  issue:, issue_label:, title_label:, stack:, color:, start_date:, end_date: date_range.end
)
  started = nil
  ended = nil
  data = []

  (start_date..end_date).each do |day|
    if yield(day)
      started = day if started.nil?
      ended = day
    elsif ended
      data << {
        x: [chart_format(started), chart_format(ended)],
        y: issue_label,
        title: "#{issue.type} : #{title_label} #{label_days (ended - started).to_i + 1}"
      }

      started = nil
      ended = nil
    end
  end

  if started
    data << {
      x: [chart_format(started), chart_format(ended)],
      y: issue_label,
      title: "#{issue.type} : #{title_label} #{label_days (end_date - started).to_i + 1}"
    }
  end

  return [] if data.empty?

  {
    type: 'bar',
    data: data,
    backgroundColor: color,
    stacked: true,
    stack: stack
  }
end

#data_sets_for_one_issue(issue:, today:) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 61

def data_sets_for_one_issue issue:, today:
  cycletime = issue.board.cycletime
  issue_start_time, _stopped_time = cycletime.started_stopped_times(issue)
  issue_start_date = issue_start_time.to_date
  issue_label = "[#{label_days cycletime.age(issue, today: today)}] #{issue.key}: #{issue.summary}"[0..60]
  [
    status_data_sets(issue: issue, label: issue_label, today: today),
    blocked_data_sets(
      issue: issue,
      issue_label: issue_label,
      stack: 'blocked',
      issue_start_time: issue_start_time
    ),
    data_set_by_block(
      issue: issue,
      issue_label: issue_label,
      title_label: 'Expedited',
      stack: 'expedited',
      color: CssVariable['--expedited-color'],
      start_date: issue_start_date
    ) { |day| issue.expedited_on_date?(day) }
  ]
end

#grow_chart_height_if_too_many_issues(aging_issue_count:) ⇒ Object



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

def grow_chart_height_if_too_many_issues aging_issue_count:
  px_per_bar = 8
  bars_per_issue = 3
  preferred_height = aging_issue_count * px_per_bar * bars_per_issue
  @canvas_height = preferred_height if @canvas_height.nil? || @canvas_height < preferred_height
end

#one_block_change_data_set(starting_change:, ending_time:, issue_label:, stack:, issue_start_time:) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 162

def one_block_change_data_set starting_change:, ending_time:, issue_label:, stack:, issue_start_time:
  if settings['blocked_color']
    file_system.deprecated message: 'blocked color should be set via css now', date: '2024-05-03'
  end
  if settings['stalled_color']
    file_system.deprecated message: 'stalled color should be set via css now', date: '2024-05-03'
  end

  color = settings['blocked_color'] || '--blocked-color'
  color = settings['stalled_color'] || '--stalled-color' if starting_change.stalled?
  {
    backgroundColor: CssVariable[color],
    data: [
      {
        title: starting_change.reasons,
        x: [chart_format([issue_start_time, starting_change.time].max), chart_format(ending_time)],
        y: issue_label
      }
    ],
    stack: stack,
    stacked: true,
    type: 'bar'
  }
end

#runObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 37

def run
  aging_issues = select_aging_issues issues: @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



91
92
93
94
95
96
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 91

def select_aging_issues issues:
  issues.select do |issue|
    started_time, stopped_time = issue.board.cycletime.started_stopped_times(issue)
    started_time && stopped_time.nil?
  end
end

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



85
86
87
88
89
# File 'lib/jirametrics/aging_work_bar_chart.rb', line 85

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

#status_data_sets(issue:, label:, today:) ⇒ Object



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

def status_data_sets issue:, label:, today:
  cycletime = issue.board.cycletime

  issue_started_time, _issue_stopped_time = cycletime.started_stopped_times(issue)

  previous_start = nil
  previous_status = nil

  data_sets = []
  issue.changes.each do |change|
    next unless change.status?

    status = issue.find_or_create_status id: change.value_id, name: change.value

    unless previous_start.nil? || previous_start < issue_started_time
      hash = {
        type: 'bar',
        data: [{
          x: [chart_format(previous_start), chart_format(change.time)],
          y: label,
          title: "#{issue.type} : #{change.value}"
        }],
        backgroundColor: status_category_color(status),
        borderColor: CssVariable['--aging-work-bar-chart-separator-color'],
        borderWidth: {
           top: 0,
           right: 1,
           bottom: 0,
           left: 0
        },
        stacked: true,
        stack: 'status'
      }
      data_sets << hash if date_range.include?(change.time.to_date)
    end

    previous_start = change.time
    previous_status = status
  end

  if previous_start
    data_sets << {
      type: 'bar',
      data: [{
        x: [chart_format(previous_start), chart_format("#{today}T00:00:00#{@timezone_offset}")],
        y: label,
        title: "#{issue.type} : #{previous_status.name}"
      }],
      backgroundColor: status_category_color(previous_status),
      stacked: true,
      stack: 'status'
    }
  end

  data_sets
end