Class: RailsCodeAuditor::Grapher

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_code_auditor/grapher.rb

Constant Summary collapse

REPORT_PATH =
"./report".freeze

Class Method Summary collapse

Class Method Details

.bar_color(score) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/rails_code_auditor/grapher.rb', line 71

def self.bar_color(score)
  case score
  when 0..49   then '#e74c3c' # red
  when 50..74  then '#f1c40f' # yellow
  when 75..89  then '#3498db' # blue
  else              '#2ecc71' # green
  end
end

.generate(results) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rails_code_auditor/grapher.rb', line 8

def self.generate(results)
  results = results.with_indifferent_access
  Dir.mkdir(REPORT_PATH) unless Dir.exist?(REPORT_PATH)

  graphs = []

  summary = {
    "Security" => results[:security],
    "Code Quality" => results[:code_quality],
    "Dependencies" => results[:dependencies],
    "Test Coverage" => results[:test_coverage],
    "Overall" => results[:overall]
  }.compact

  graphs << graph_bar("Audit Scores", summary)

  summary.each do |label, data|
    graphs << graph_pie(label, data[:score])
  end

  graphs
end

.graph_bar(title, metrics) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rails_code_auditor/grapher.rb', line 31

def self.graph_bar(title, metrics)
  g = Gruff::Bar.new
  g.title = title

  labels = {}
  metrics.each_with_index do |(label, data), index|
    score = data[:score] || 0
    labels[index] = label
    g.data(label, [score], bar_color(score))
  end

  g.labels = labels

  file_name = "#{title.downcase.gsub(" ", "_")}.png"
  path = File.join(REPORT_PATH, file_name)
  g.write(path)

  puts "Generated graph at #{path}"

  { title: title, path: path }
end

.graph_pie(title, score) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rails_code_auditor/grapher.rb', line 53

def self.graph_pie(title, score)
  score = score || 0
  remaining = 100 - score

  g = Gruff::Pie.new
  g.title = title
  g.data("Score", score, bar_color(score))
  g.data("Remaining", remaining, "#dddddd")

  file_name = "#{title.downcase.gsub(" ", "_")}_pie.png"
  path = File.join(REPORT_PATH, file_name)
  g.write(path)

  puts "Generated pie chart: #{path}"

  { title: title, path: path }
end