Class: DailyWipChart

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

Instance Attribute Summary collapse

Attributes inherited from ChartBase

#aggregated_project, #all_boards, #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, #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

#initialize(block) ⇒ DailyWipChart

Returns a new instance of DailyWipChart.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jirametrics/daily_wip_chart.rb', line 17

def initialize block
  super()

  header_text default_header_text
  description_text default_description_text

  instance_eval(&block) if block

  unless @group_by_block
    grouping_rules do |issue, rules|
      default_grouping_rules issue: issue, rules: rules
    end
  end
end

Instance Attribute Details

#possible_statusesObject

Returns the value of attribute possible_statuses.



15
16
17
# File 'lib/jirametrics/daily_wip_chart.rb', line 15

def possible_statuses
  @possible_statuses
end

Instance Method Details

#add_trend_line(group_labels:, line_color:) ⇒ Object



129
130
131
# File 'lib/jirametrics/daily_wip_chart.rb', line 129

def add_trend_line group_labels:, line_color:
  (@trend_lines ||= []) << [group_labels, line_color]
end

#configure_rule(issue:, date:) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/jirametrics/daily_wip_chart.rb', line 116

def configure_rule issue:, date:
  raise "#{self.class}: grouping_rules must be set" if @group_by_block.nil?

  rules = DailyGroupingRules.new
  rules.current_date = date
  @group_by_block.call issue, rules
  rules
end

#default_description_textObject



49
# File 'lib/jirametrics/daily_wip_chart.rb', line 49

def default_description_text = ''

#default_grouping_rules(issue:, rules:) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



51
52
53
# File 'lib/jirametrics/daily_wip_chart.rb', line 51

def default_grouping_rules issue:, rules: # rubocop:disable Lint/UnusedMethodArgument
  raise 'If you use this class directly then you must provide grouping_rules'
end

#default_header_textObject



48
# File 'lib/jirametrics/daily_wip_chart.rb', line 48

def default_header_text = 'Daily WIP'

#group_issues_by_active_datesObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/jirametrics/daily_wip_chart.rb', line 65

def group_issues_by_active_dates
  hash = {}

  @issues.each do |issue|
    cycletime = issue.board.cycletime
    start = cycletime.started_time(issue)&.to_date
    stop = cycletime.stopped_time(issue)&.to_date
    next if start.nil? && stop.nil?

    # If it stopped but never started then assume it started at creation so the data points
    # will be available for the config.
    start = issue.created.to_date if stop && start.nil?
    start = @date_range.begin if start < @date_range.begin

    start.upto(stop || @date_range.end) do |date|
      rule = configure_rule issue: issue, date: date
      (hash[date] ||= []) << [issue, rule] unless rule.ignored?
    end
  end
  hash
end

#grouping_rules(&block) ⇒ Object



125
126
127
# File 'lib/jirametrics/daily_wip_chart.rb', line 125

def grouping_rules &block
  @group_by_block = block
end

#make_data_set(grouping_rule:, issue_rules_by_active_date:) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/jirametrics/daily_wip_chart.rb', line 87

def make_data_set grouping_rule:, issue_rules_by_active_date:
  positive = grouping_rule.group_priority >= 0

  data = issue_rules_by_active_date.collect do |date, issue_rules|
    # issues = []
    issue_strings = issue_rules
      .select { |_issue, rules| rules.group == grouping_rule.group }
      .sort_by { |issue, _rules| issue.key_as_i }
      .collect { |issue, rules| "#{issue.key} : #{issue.summary.strip} #{rules.issue_hint}" }
    title = ["#{grouping_rule.label} (#{label_issues issue_strings.size})"] + issue_strings

    {
      x: date,
      y: positive ? issue_strings.size : -issue_strings.size,
      title: title
    }
  end

  {
    type: 'bar',
    label: grouping_rule.label,
    data: data,
    backgroundColor: grouping_rule.color || random_color,
    borderColor: CssVariable['--wip-chart-border-color'],
    borderWidth: grouping_rule.color.to_s == 'var(--body-background)' ? 1 : 0,
    borderRadius: positive ? 0 : 5
  }
end

#runObject



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

def run
  issue_rules_by_active_date = group_issues_by_active_dates
  possible_rules = select_possible_rules issue_rules_by_active_date

  data_sets = possible_rules.collect do |grouping_rule|
    make_data_set grouping_rule: grouping_rule, issue_rules_by_active_date: issue_rules_by_active_date
  end
  if @trend_lines
    data_sets = @trend_lines.filter_map do |group_labels, line_color|
      trend_line_data_set(data: data_sets, group_labels: group_labels, color: line_color)
    end + data_sets
  end

  wrap_and_render(binding, __FILE__)
end

#select_possible_rules(issue_rules_by_active_date) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/jirametrics/daily_wip_chart.rb', line 55

def select_possible_rules issue_rules_by_active_date
  possible_rules = []
  issue_rules_by_active_date.each_pair do |_date, issues_rules_list|
    issues_rules_list.each do |_issue, rules| # rubocop:disable Style/HashEachMethods
      possible_rules << rules unless possible_rules.any? { |r| r.group == rules.group }
    end
  end
  possible_rules.sort_by!(&:group_priority)
end

#trend_line_data_set(data:, group_labels:, color:) ⇒ Object



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
170
171
# File 'lib/jirametrics/daily_wip_chart.rb', line 133

def trend_line_data_set data:, group_labels:, color:
  day_wip_hash = {}
  data.each do |top_level|
    next unless group_labels.include? top_level[:label]

    top_level[:data].each do |datapoint|
      date = datapoint[:x]
      day_wip_hash[date] = (day_wip_hash[date] || 0) + datapoint[:y]
    end
  end

  points = day_wip_hash
    .collect { |date, wip| [date.jd, wip] }
    .sort_by(&:first)

  calculator = TrendLineCalculator.new(points)
  return nil unless calculator.valid?

  data_points = calculator.chart_datapoints(
    range: date_range.begin.jd..date_range.end.jd,
    max_y: points.collect { |_date, wip| wip }.max
  )
  data_points.each do |point_hash|
    point_hash[:x] = chart_format Date.jd(point_hash[:x])
  end

  {
    type: 'line',
    label: "Trendline",
    data: data_points,
    fill: false,
    borderWidth: 1,
    markerType: 'none',
    borderColor: CssVariable[color],
    borderDash: [6, 3],
    pointStyle: 'dash',
    hidden: false
  }
end