Class: ExpeditedChart

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

Constant Summary collapse

EXPEDITED_SEGMENT =
ChartBase.new.tap do |segment|
  def segment.to_json *_args
    expedited = CssVariable.new('--expedited-color').to_json
    not_expedited = CssVariable.new('--expedited-chart-no-longer-expedited').to_json

    <<~SNIPPET
      {
        borderColor: ctx => expedited(ctx, #{expedited}) || notExpedited(ctx, #{not_expedited}),
        borderDash: ctx => notExpedited(ctx, [6, 6])
      }
    SNIPPET
  end
end

Constants inherited from ChartBase

ChartBase::LABEL_POSITIONS, ChartBase::OKABE_ITO_PALETTE

Instance Attribute Summary collapse

Attributes inherited from ChartBase

#aggregated_project, #all_boards, #atlassian_document_format, #board_id, #canvas_height, #canvas_width, #data_quality, #file_system, #fix_versions, #holiday_dates, #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_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) ⇒ ExpeditedChart

Returns a new instance of ExpeditedChart.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jirametrics/expedited_chart.rb', line 23

def initialize block
  super()

  header_text 'Expedited work'
  description_text <<-HTML
    <div class="p">
      This chart only shows issues that have been expedited at some point. We care about these as
      any form of expedited work will affect the entire system and will slow down non-expedited work.
      Refer to this article on
      <a href="https://improvingflow.com/2021/06/16/classes-of-service.html">classes of service</a>
      for a longer explanation on why we want to avoid expedited work.
    </div>
    <div class="p">
      The colour of the line indicates time that this issue was #{color_block '--expedited-color'} expedited
      or #{color_block '--expedited-chart-no-longer-expedited'} not expedited.
    </div>
    #{describe_non_working_days}
  HTML
  @x_axis_title = 'Date'
  @y_axis_title = 'Age in days'

  instance_eval(&block)
end

Instance Attribute Details

#cycletimeObject

Returns the value of attribute cycletime.



20
21
22
# File 'lib/jirametrics/expedited_chart.rb', line 20

def cycletime
  @cycletime
end

#date_rangeObject

Returns the value of attribute date_range.



20
21
22
# File 'lib/jirametrics/expedited_chart.rb', line 20

def date_range
  @date_range
end

#expedited_labelObject (readonly)

Returns the value of attribute expedited_label.



21
22
23
# File 'lib/jirametrics/expedited_chart.rb', line 21

def expedited_label
  @expedited_label
end

#issuesObject

Returns the value of attribute issues.



20
21
22
# File 'lib/jirametrics/expedited_chart.rb', line 20

def issues
  @issues
end

#possible_statusesObject

Returns the value of attribute possible_statuses.



20
21
22
# File 'lib/jirametrics/expedited_chart.rb', line 20

def possible_statuses
  @possible_statuses
end

Instance Method Details

#expedite_point(issue, time, action, expedited) ⇒ Object

Returns [point, dot_color, point_style, expedited] for one timeline event. The returned expedited flag carries the (possibly changed) state forward to the next event.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/jirametrics/expedited_chart.rb', line 157

def expedite_point issue, time, action, expedited
  case action
  when :issue_started
    [make_point(issue: issue, time: time, label: 'Started', expedited: expedited),
     CssVariable['--expedited-chart-dot-issue-started-color'], 'rect', expedited]
  when :issue_stopped
    [make_point(issue: issue, time: time, label: 'Completed', expedited: expedited),
     CssVariable['--expedited-chart-dot-issue-stopped-color'], 'rect', expedited]
  when :expedite_start
    [make_point(issue: issue, time: time, label: 'Expedited', expedited: true),
     CssVariable['--expedited-chart-dot-expedite-started-color'], 'circle', true]
  when :expedite_stop
    [make_point(issue: issue, time: time, label: 'Not expedited', expedited: false),
     CssVariable['--expedited-chart-dot-expedite-stopped-color'], 'circle', false]
  else
    raise "Unexpected action: #{action}"
  end
end

#expedite_visible?(start_date, stop_date) ⇒ Boolean

True when an expedite span overlaps the chart's date range: either endpoint falls inside it, or it starts before and ends after (spanning the whole range).

Returns:

  • (Boolean)


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

def expedite_visible? start_date, stop_date
  date_range.include?(start_date) || date_range.include?(stop_date) ||
    (start_date < date_range.begin && stop_date > date_range.end)
end

#find_expedited_issuesObject



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

def find_expedited_issues
  expedited_issues = @issues.reject do |issue|
    prepare_expedite_data(issue).empty?
  end

  expedited_issues.sort_by(&:key_as_i)
end

#later_date(date1, date2) ⇒ Object



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

def later_date date1, date2
  return date1 if date2.nil?
  return date2 if date1.nil?

  [date1, date2].max
end

#make_expedite_lines_data_set(issue:, expedite_data:) ⇒ Object



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

def make_expedite_lines_data_set issue:, expedite_data:
  started_date, stopped_date = issue.board.cycletime.started_stopped_dates(issue)

  expedite_data << [started_date, :issue_started] if started_date
  expedite_data << [stopped_date, :issue_stopped] if stopped_date
  expedite_data.sort_by!(&:first)

  # If none of the data would be visible on the chart then skip it.
  return nil unless expedite_data.any? { |time, _action| time.to_date >= date_range.begin }

  data = []
  dot_colors = []
  point_styles = []
  expedited = false

  expedite_data.each do |time, action|
    point, color, style, expedited = expedite_point(issue, time, action, expedited)
    data << point
    dot_colors << color
    point_styles << style
  end

  if still_ongoing?(expedite_data, stopped_date)
    data << make_point(issue: issue, time: date_range.end, label: 'Still ongoing', expedited: expedited)
    dot_colors << '' # It won't be visible so it doesn't matter
    point_styles << 'dash'
  end

  {
    type: 'line',
    label: issue.key,
    data: data,
    fill: false,
    showLine: true,
    backgroundColor: dot_colors,
    pointBorderColor: 'black',
    pointStyle: point_styles,
    segment: EXPEDITED_SEGMENT
  }
end

#make_point(issue:, time:, label:, expedited:) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/jirametrics/expedited_chart.rb', line 105

def make_point issue:, time:, label:, expedited:
  {
    y: (time.to_date - issue.created.to_date).to_i + 1,
    x: time.to_date.to_s,
    title: ["#{issue.key} #{label} : #{issue.summary}"],
    expedited: (expedited ? 1 : 0)
  }
end

#prepare_expedite_data(issue) ⇒ Object



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

def prepare_expedite_data issue
  expedite_start = nil
  result = []
  expedited_priority_names = issue.board.project_config.settings['expedited_priority_names']

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

    if expedited_priority_names.include? change.value
      expedite_start = change.time.to_date
    elsif expedite_start
      if expedite_visible?(expedite_start, change.time.to_date)
        result << [expedite_start, :expedite_start]
        result << [change.time.to_date, :expedite_stop]
      end
      expedite_start = nil
    end
  end

  # If expedite_start is still set then we never ended.
  result << [expedite_start, :expedite_start] if expedite_start
  result
end

#runObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jirametrics/expedited_chart.rb', line 47

def run
  data_sets = find_expedited_issues.filter_map do |issue|
    make_expedite_lines_data_set(issue: issue, expedite_data: prepare_expedite_data(issue))
  end

  if data_sets.empty?
    '<h1 class="foldable">Expedited work</h1><div>There is no expedited work in this time period.</div>'
  else
    wrap_and_render(binding, __FILE__)
  end
end

#still_ongoing?(expedite_data, stopped_date) ⇒ Boolean

The issue is still expedited/open at the end of the chart: it hasn't stopped and its last recorded change is on or before the end of the range.

Returns:

  • (Boolean)


178
179
180
181
182
183
# File 'lib/jirametrics/expedited_chart.rb', line 178

def still_ongoing? expedite_data, stopped_date
  return false if expedite_data.empty?

  last_change_time = expedite_data[-1][0].to_date
  last_change_time && last_change_time <= date_range.end && stopped_date.nil?
end