Class: ChartBase

Inherits:
Object
  • Object
show all
Defined in:
lib/jirametrics/chart_base.rb

Constant Summary collapse

@@chart_counter =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChartBase

Returns a new instance of ChartBase.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/jirametrics/chart_base.rb', line 11

def initialize
  @chart_colors = {
    'Story'  => CssVariable['--type-story-color'],
    'Task'   => CssVariable['--type-task-color'],
    'Bug'    => CssVariable['--type-bug-color'],
    'Defect' => CssVariable['--type-bug-color'],
    'Spike'  => CssVariable['--type-spike-color']
  }
  @canvas_width = 800
  @canvas_height = 200
  @canvas_responsive = true
end

Instance Attribute Details

#aggregated_project=(value) ⇒ Object (writeonly)

Sets the attribute aggregated_project

Parameters:

  • value

    the value to set the attribute aggregated_project to.



6
7
8
# File 'lib/jirametrics/chart_base.rb', line 6

def aggregated_project=(value)
  @aggregated_project = value
end

#all_boardsObject

Returns the value of attribute all_boards.



4
5
6
# File 'lib/jirametrics/chart_base.rb', line 4

def all_boards
  @all_boards
end

#board_idObject

Returns the value of attribute board_id.



4
5
6
# File 'lib/jirametrics/chart_base.rb', line 4

def board_id
  @board_id
end

#canvas_heightObject (readonly)

Returns the value of attribute canvas_height.



7
8
9
# File 'lib/jirametrics/chart_base.rb', line 7

def canvas_height
  @canvas_height
end

#canvas_widthObject (readonly)

Returns the value of attribute canvas_width.



7
8
9
# File 'lib/jirametrics/chart_base.rb', line 7

def canvas_width
  @canvas_width
end

#data_qualityObject

Returns the value of attribute data_quality.



4
5
6
# File 'lib/jirametrics/chart_base.rb', line 4

def data_quality
  @data_quality
end

#date_rangeObject

Returns the value of attribute date_range.



4
5
6
# File 'lib/jirametrics/chart_base.rb', line 4

def date_range
  @date_range
end

#file_systemObject

Returns the value of attribute file_system.



4
5
6
# File 'lib/jirametrics/chart_base.rb', line 4

def file_system
  @file_system
end

#holiday_datesObject

Returns the value of attribute holiday_dates.



4
5
6
# File 'lib/jirametrics/chart_base.rb', line 4

def holiday_dates
  @holiday_dates
end

#issuesObject

Returns the value of attribute issues.



4
5
6
# File 'lib/jirametrics/chart_base.rb', line 4

def issues
  @issues
end

#settingsObject

Returns the value of attribute settings.



4
5
6
# File 'lib/jirametrics/chart_base.rb', line 4

def settings
  @settings
end

#time_rangeObject

Returns the value of attribute time_range.



4
5
6
# File 'lib/jirametrics/chart_base.rb', line 4

def time_range
  @time_range
end

#timezone_offsetObject

Returns the value of attribute timezone_offset.



4
5
6
# File 'lib/jirametrics/chart_base.rb', line 4

def timezone_offset
  @timezone_offset
end

#usersObject

Returns the value of attribute users.



4
5
6
# File 'lib/jirametrics/chart_base.rb', line 4

def users
  @users
end

Instance Method Details

#aggregated_project?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/jirametrics/chart_base.rb', line 24

def aggregated_project?
  @aggregated_project
end

#canvas(width:, height:, responsive: true) ⇒ Object



251
252
253
254
255
# File 'lib/jirametrics/chart_base.rb', line 251

def canvas width:, height:, responsive: true
  @canvas_width = width
  @canvas_height = height
  @canvas_responsive = responsive
end

#canvas_responsive?Boolean

Returns:

  • (Boolean)


257
258
259
# File 'lib/jirametrics/chart_base.rb', line 257

def canvas_responsive?
  @canvas_responsive
end

#chart_format(object) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/jirametrics/chart_base.rb', line 175

def chart_format object
  if object.is_a? Time
    # "2022-04-09T11:38:30-07:00"
    object.strftime '%Y-%m-%dT%H:%M:%S%z'
  else
    object.to_s
  end
end

#collapsible_issues_panel(issue_descriptions, *args) ⇒ Object



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

def collapsible_issues_panel issue_descriptions, *args
  link_id = next_id
  issues_id = next_id

  issue_descriptions.sort! { |a, b| a[0].key_as_i <=> b[0].key_as_i }
  erb = ERB.new file_system.load File.join(html_directory, 'collapsible_issues_panel.erb')
  erb.result(binding)
end

#color_block(color, title: nil) ⇒ Object



261
262
263
264
265
266
267
268
269
270
# File 'lib/jirametrics/chart_base.rb', line 261

def color_block color, title: nil
  result = +''
  result << "<div class='color_block' style='"
  result << "background: #{CssVariable[color]};" if color
  result << 'visibility: hidden;' unless color
  result << "'"
  result << " title=#{title.inspect}" if title
  result << '></div>'
  result
end

#color_for(type:) ⇒ Object



65
66
67
# File 'lib/jirametrics/chart_base.rb', line 65

def color_for type:
  @chart_colors[type] ||= random_color
end

#completed_issues_in_range(include_unstarted: false) ⇒ Object



164
165
166
167
168
169
170
171
172
173
# File 'lib/jirametrics/chart_base.rb', line 164

def completed_issues_in_range include_unstarted: false
  issues.select do |issue|
    cycletime = issue.board.cycletime
    started_time, stopped_time = cycletime.started_stopped_times(issue)

    stopped_time &&
      date_range.include?(stopped_time.to_date) && # Remove outside range
      (include_unstarted || (started_time && (stopped_time >= started_time)))
  end
end

#current_boardObject

Return only the board columns for the current board.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/jirametrics/chart_base.rb', line 149

def current_board
  if @board_id.nil?
    case @all_boards.size
    when 0
      raise 'Couldn\'t find any board configurations. Ensure one is set'
    when 1
      return @all_boards.values[0]
    else
      raise "Must set board_id so we know which to use. Multiple boards found: #{@all_boards.keys.inspect}"
    end
  end

  @all_boards[@board_id]
end

#daily_chart_dataset(date_issues_list:, color:, label:, positive: true) ⇒ Object



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

def daily_chart_dataset date_issues_list:, color:, label:, positive: true
  {
    type: 'bar',
    label: label,
    data: date_issues_list.collect do |date, issues|
      issues.sort_by!(&:key_as_i)
      title = "#{label} (#{label_issues issues.size})"
      {
        x: date,
        y: positive ? issues.size : -issues.size,
        title: [title] + issues.collect { |i| "#{i.key} : #{i.summary.strip}#{" #{yield date, i}" if block_given?}" }
      }
    end,
    backgroundColor: color,
    borderRadius: positive ? 0 : 5
  }
end

#describe_non_working_daysObject



272
273
274
275
276
277
278
279
# File 'lib/jirametrics/chart_base.rb', line 272

def describe_non_working_days
  <<-TEXT
    <div class='p'>
      The #{color_block '--non-working-days-color'} vertical bars indicate non-working days; weekends
      and any other holidays mentioned in the configuration.
    </div>
  TEXT
end

#description_text(text = :none) ⇒ Object



189
190
191
192
# File 'lib/jirametrics/chart_base.rb', line 189

def description_text text = :none
  @description_text = text unless text == :none
  @description_text
end

#format_integer(number) ⇒ Object

Convert a number like 1234567 into the string “1,234,567”



195
196
197
# File 'lib/jirametrics/chart_base.rb', line 195

def format_integer number
  number.to_s.reverse.scan(/.{1,3}/).join(',').reverse
end

#format_status(object, board:, is_category: false, use_old_status: false) ⇒ Object

object will be either a Status or a ChangeItem if it’s a ChangeItem then use_old_status will specify whether we’re using the new or old Either way, is_category will format the category rather than the status



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

def format_status object, board:, is_category: false, use_old_status: false
  status = nil
  error_message = nil

  case object
  when ChangeItem
    id = use_old_status ? object.old_value_id : object.value_id
    status = board.possible_statuses.find_by_id(id)
    if status.nil?
      error_message = use_old_status ? object.old_value : object.value
    end
  when Status
    status = object
  else
    raise "Unexpected type: #{object.class}"
  end

  return "<span style='color: red'>#{error_message}</span>" if error_message

  color = status_category_color status

  visibility = ''
  if is_category == false && board.visible_columns.none? { |column| column.status_ids.include? status.id }
    visibility = icon_span(
      title: "Not visible: The status #{status.name.inspect} is not mapped to any column and will not be visible",
      icon: ' 👀'
    )
  end
  text = is_category ? status.category.name : status.name
  "<span title='Category: #{status.category.name}'>#{color_block color.name} #{text}</span>#{visibility}"
end

#header_text(text = :none) ⇒ Object



184
185
186
187
# File 'lib/jirametrics/chart_base.rb', line 184

def header_text text = :none
  @header_text = text unless text == :none
  @header_text
end

#holidays(date_range: @date_range) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/jirametrics/chart_base.rb', line 112

def holidays date_range: @date_range
  result = []
  start_date = nil
  end_date = nil

  date_range.each do |date|
    if date.saturday? || date.sunday? || holiday_dates.include?(date)
      if start_date.nil?
        start_date = date
      else
        end_date = date
      end
    elsif start_date
      result << (start_date..(end_date || start_date))
      start_date = nil
      end_date = nil
    end
  end
  result
end

#html_directoryObject



28
29
30
31
# File 'lib/jirametrics/chart_base.rb', line 28

def html_directory
  pathname = Pathname.new(File.realpath(__FILE__))
  "#{pathname.dirname}/html"
end

#icon_span(title:, icon:) ⇒ Object



234
235
236
# File 'lib/jirametrics/chart_base.rb', line 234

def icon_span title:, icon:
  "<span title='#{title}' style='font-size: 0.8em;'>#{icon}</span>"
end

#label_days(days) ⇒ Object



69
70
71
# File 'lib/jirametrics/chart_base.rb', line 69

def label_days days
  "#{days} day#{'s' unless days == 1}"
end

#label_issues(count) ⇒ Object



73
74
75
# File 'lib/jirametrics/chart_base.rb', line 73

def label_issues count
  "#{count} issue#{'s' unless count == 1}"
end


95
96
97
98
99
100
101
# File 'lib/jirametrics/chart_base.rb', line 95

def link_to_issue issue, args = {}
  attributes = { class: 'issue_key' }
    .merge(args)
    .collect { |key, value| "#{key}='#{value}'" }
    .join(' ')
  "<a href='#{issue.url}' #{attributes}>#{issue.key}</a>"
end

#next_idObject



61
62
63
# File 'lib/jirametrics/chart_base.rb', line 61

def next_id
  @@chart_counter += 1
end

#random_colorObject



247
248
249
# File 'lib/jirametrics/chart_base.rb', line 247

def random_color
  "##{Random.bytes(3).unpack1('H*')}"
end

#render(caller_binding, file) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jirametrics/chart_base.rb', line 33

def render caller_binding, file
  pathname = Pathname.new(File.realpath(file))
  basename = pathname.basename.to_s
  raise "Unexpected filename #{basename.inspect}" unless basename =~ /^(.+)\.rb$/

  # Insert a incrementing chart_id so that all the chart names on the page are unique
  caller_binding.eval "chart_id='chart#{next_id}'" # chart_id=chart3

  # @html_directory = "#{pathname.dirname}/html"
  erb = ERB.new file_system.load "#{html_directory}/#{$1}.erb"
  erb.result(caller_binding)
end

#render_top_text(caller_binding) ⇒ Object



46
47
48
49
50
51
# File 'lib/jirametrics/chart_base.rb', line 46

def render_top_text caller_binding
  result = +''
  result << "<h1>#{@header_text}</h1>" if @header_text
  result << ERB.new(@description_text).result(caller_binding) if @description_text
  result
end

#status_category_color(status) ⇒ Object



238
239
240
241
242
243
244
245
# File 'lib/jirametrics/chart_base.rb', line 238

def status_category_color status
  case status.category.key
  when 'new' then CssVariable['--status-category-todo-color']
  when 'indeterminate' then CssVariable['--status-category-inprogress-color']
  when 'done' then CssVariable['--status-category-done-color']
  else CssVariable['--status-category-unknown-color'] # Theoretically impossible but seen in prod.
  end
end

#working_days_annotationObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/jirametrics/chart_base.rb', line 133

def working_days_annotation
  holidays.each_with_index.collect do |range, index|
    <<~TEXT
      holiday#{index}: {
        drawTime: 'beforeDraw',
        type: 'box',
        xMin: '#{range.begin}T00:00:00',
        xMax: '#{range.end}T23:59:59',
        backgroundColor: #{CssVariable.new('--non-working-days-color').to_json},
        borderColor: #{CssVariable.new('--non-working-days-color').to_json}
      },
    TEXT
  end.join
end

#wrap_and_render(caller_binding, file) ⇒ Object

Render the file and then wrap it with standard headers and quality checks.



54
55
56
57
58
59
# File 'lib/jirametrics/chart_base.rb', line 54

def wrap_and_render caller_binding, file
  result = +''
  result << render_top_text(caller_binding)
  result << render(caller_binding, file)
  result
end