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

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, #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(rules_block) ⇒ DependencyChart

Returns a new instance of DependencyChart.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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
  @link_rules_block = ->(link_name, link_rules) {}

  issue_rules do |issue, rules|
    key = issue.key
    key = "<S>#{key} </S> " if issue.status.category_name == 'Done'
    rules.label = "<#{key} [#{issue.type}]<BR/>#{word_wrap issue.summary}>"
  end
end

Instance Method Details

#build_dot_graphObject



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
178
179
180
181
# File 'lib/jirametrics/dependency_chart.rb', line 125

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



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

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

#execute_graphviz(dot_graph) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/jirametrics/dependency_chart.rb', line 183

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 execute the command 'dot' which is part of graphviz. " \
    'Ensure that graphviz is installed and that dot is in your path.'
  puts message
  message
end


72
73
74
75
76
77
78
# File 'lib/jirametrics/dependency_chart.rb', line 72

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

#issue_rules(&block) ⇒ Object



68
69
70
# File 'lib/jirametrics/dependency_chart.rb', line 68

def issue_rules &block
  @issue_rules_block = block
end


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

def link_rules &block
  @link_rules_block = block
end

#make_dot_issue(issue:, issue_rules:) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/jirametrics/dependency_chart.rb', line 94

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


80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/jirametrics/dependency_chart.rb', line 80

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



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

def run
  instance_eval(&@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



196
197
198
199
200
201
202
203
# File 'lib/jirametrics/dependency_chart.rb', line 196

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



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/jirametrics/dependency_chart.rb', line 205

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