Class: DependencyChart

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

Defined Under Namespace

Classes: IssueRules, LinkRules

Instance Attribute Summary

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, #users

Instance Method Summary collapse

Methods inherited from ChartBase

#aggregated_project?, #canvas, #canvas_responsive?, #chart_format, #collapsible_issues_panel, #color_block, #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(rules_block) ⇒ DependencyChart

Returns a new instance of DependencyChart.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jirametrics/dependency_chart.rb', line 34

def initialize rules_block
  super()

  header_text 'Dependencies'
  description_text <<-HTML
    <p>
      These are all the "linked issues" as defined in Jira
    </p>
  HTML

  @rules_block = rules_block

  issue_rules(&default_issue_rules)
  link_rules(&default_link_rules)
end

Instance Method Details

#build_dot_graphObject



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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/jirametrics/dependency_chart.rb', line 121

def build_dot_graph
  issue_links = find_links

  visible_issues = {}
  link_graph = []
  links_to_ignore = []

  issue_links.each do |link|
    next if links_to_ignore.include? link

    link_rules = LinkRules.new
    @link_rules_block.call link, link_rules

    next if link_rules.ignored?

    if link_rules.get_merge_bidirectional
      opposite = issue_links.find do |l|
        l.name == link.name && l.origin.key == link.other_issue.key && l.other_issue.key == link.origin.key
      end
      if opposite
        # rubocop:disable Style/GuardClause
        if link_rules.get_merge_bidirectional.to_sym == link.direction
          # We keep this one and discard the opposite
          links_to_ignore << opposite
        else
          # We keep the opposite and discard this one
          next
        end
        # rubocop:enable Style/GuardClause
      end
    end

    link_graph << make_dot_link(issue_link: link, link_rules: link_rules)

    visible_issues[link.origin.key] = link.origin
    visible_issues[link.other_issue.key] = link.other_issue
  end

  dot_graph = []
  dot_graph << 'digraph mygraph {'
  dot_graph << 'rankdir=LR'
  dot_graph << 'bgcolor="transparent"'

  # Sort the keys so they are proccessed in a deterministic order.
  visible_issues.values.sort_by(&:key_as_i).each do |issue|
    rules = IssueRules.new
    @issue_rules_block.call(issue, rules)
    dot_graph << make_dot_issue(issue: issue, issue_rules: rules)
  end

  dot_graph += link_graph
  dot_graph << '}'

  return nil if visible_issues.empty?

  dot_graph
end

#color_for(type:) ⇒ Object

This used to pull colours from chart_base but the migration to CSS colours kept breaking this chart so we moved it here, until we’re finished with the rest. TODO: Revisit whether this can also use customizable CSS colours



110
111
112
113
114
115
116
117
118
119
# File 'lib/jirametrics/dependency_chart.rb', line 110

def color_for type:
  @chart_colors = {
    'Story' => '#90EE90',
    'Task' => '#87CEFA',
    'Bug' => '#ffdab9',
    'Defect' => '#ffdab9',
    'Epic' => '#fafad2',
    'Spike' => '#DDA0DD' # light purple
  }[type] ||= random_color
end

#default_issue_rulesObject



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/jirametrics/dependency_chart.rb', line 218

def default_issue_rules
  chart = self
  lambda do |issue, rules|
    is_done = issue.done?

    key = issue.key
    key = "<S>#{key} </S> " if is_done
    line2 = +'<BR/>'
    if issue.artificial?
      line2 << '(unknown state)' # Shouldn't happen if we've done a full download but is still possible.
    elsif is_done
      line2 << 'Done'
    else
      started_at = issue.board.cycletime.started_stopped_times(issue).first
      if started_at.nil?
        line2 << 'Not started'
      else
        line2 << "Age: #{issue.board.cycletime.age(issue, today: chart.date_range.end)} days"
      end
    end
    rules.label = "<#{key} [#{issue.type}]#{line2}<BR/>#{word_wrap issue.summary}>"
  end
end


242
243
244
245
246
247
248
# File 'lib/jirametrics/dependency_chart.rb', line 242

def default_link_rules
  lambda do |link, rules|
    rules.ignore if link.origin.done? && link.other_issue.done?
    rules.ignore if link.name == 'Cloners'
    rules.merge_bidirectional keep: 'outward'
  end
end

#execute_graphviz(dot_graph) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/jirametrics/dependency_chart.rb', line 179

def execute_graphviz dot_graph
  Open3.popen3('dot -Tsvg') do |stdin, stdout, _stderr, _wait_thread|
    stdin.write dot_graph
    stdin.close
    return stdout.read
  end
rescue # rubocop:disable Style/RescueStandardError
  message = 'Unable to generate the dependency chart because graphviz could not be found in the path.'
  file_system.log message, also_write_to_stderr: true
  message
end


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

def find_links
  result = []
  issues.each do |issue|
    result += issue.issue_links
  end
  result
end

#issue_rules(&block) ⇒ Object



64
65
66
# File 'lib/jirametrics/dependency_chart.rb', line 64

def issue_rules &block
  @issue_rules_block = block
end


60
61
62
# File 'lib/jirametrics/dependency_chart.rb', line 60

def link_rules &block
  @link_rules_block = block
end

#make_dot_issue(issue:, issue_rules:) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/jirametrics/dependency_chart.rb', line 90

def make_dot_issue issue:, issue_rules:
  result = +''
  result << issue.key.inspect
  result << '['
  label = issue_rules.label || "#{issue.key}|#{issue.type}"
  label = label.inspect unless label.match?(/^<.+>$/)
  result << "label=#{label}"
  result << ',shape=Mrecord'
  tooltip = "#{issue.key}: #{issue.summary}"
  result << ",tooltip=#{tooltip[0..80].inspect}"
  unless issue_rules.color == :none
    result << %(,style=filled,fillcolor="#{issue_rules.color || color_for(type: issue.type)}")
  end
  result << ']'
  result
end


76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/jirametrics/dependency_chart.rb', line 76

def make_dot_link issue_link:, link_rules:
  result = +''
  result << issue_link.origin.key.inspect
  result << ' -> '
  result << issue_link.other_issue.key.inspect
  result << '['
  result << 'label=' << (link_rules.label || issue_link.label).inspect
  result << ',color=' << (link_rules.line_color || 'gray').inspect
  result << ',fontcolor=' << (link_rules.line_color || 'gray').inspect
  result << ',dir=both' if link_rules.bidirectional_arrows?
  result << '];'
  result
end

#runObject



50
51
52
53
54
55
56
57
58
# File 'lib/jirametrics/dependency_chart.rb', line 50

def run
  instance_eval(&@rules_block) if @rules_block

  dot_graph = build_dot_graph
  return "<h1>#{@header_text}</h1>No data matched the selected criteria. Nothing to show." if dot_graph.nil?

  svg = execute_graphviz(dot_graph.join("\n"))
  "<h1>#{@header_text}</h1><div>#{@description_text}</div>#{shrink_svg svg}"
end

#shrink_svg(svg) ⇒ Object



191
192
193
194
195
196
197
198
# File 'lib/jirametrics/dependency_chart.rb', line 191

def shrink_svg svg
  scale = 0.8
  svg.sub(/width="([\d.]+)pt" height="([\d.]+)pt"/) do
    width = $1.to_i * scale
    height = $2.to_i * scale
    "width=\"#{width.to_i}pt\" height=\"#{height.to_i}pt\""
  end
end

#word_wrap(text, max_width: 50, separator: '<BR/>') ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/jirametrics/dependency_chart.rb', line 200

def word_wrap text, max_width: 50, separator: '<BR/>'
  text.chomp.lines.collect do |line|
    line.chomp!

    # The following characters all cause problems when passed to graphviz
    line.gsub!(/[{<]/, '[')
    line.gsub!(/[}>]/, ']')
    line.gsub!(/\s*&\s*/, ' and ')
    line.delete!('|')

    if line.length > max_width
      line.gsub(/(.{1,#{max_width}})(\s+|$)/, "\\1#{separator}").strip
    else
      line
    end
  end.join(separator)
end