Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Made charts more modular so that chart engine can be specified in the…

… configuration
  • Loading branch information...
commit 987bf641681771688f9f890862c089d5d819adb4 1 parent ba26e90
@cayblood cayblood authored
View
2  lib/base/configuration.rb
@@ -9,6 +9,7 @@ module MetricFu
:roodi, :saikuro, :rcov]
AVAILABLE_GRAPHS = [:flog, :flay, :reek, :roodi, :rcov]
+ AVAILABLE_GRAPH_ENGINES = [:gchart, :bluff]
# The @@configuration class variable holds a global type configuration
# object for any parts of the system to use.
@@ -131,6 +132,7 @@ def reset
"--rails",
"--exclude /gems/,/Library/,/usr/,spec"]}
+ @graph_engine = AVAILABLE_GRAPH_ENGINES.first
@graph_theme = { :colors => %w(orange purple green white red blue pink yellow),
:marker_color => 'blue',
:background_colors => %w(white white)}
View
7 lib/base/graph.rb
@@ -12,8 +12,8 @@ def initialize
self.clazz = []
end
- def add(graph_type)
- grapher_name = graph_type.to_s.capitalize + "Grapher"
+ def add(graph_type, graph_engine)
+ grapher_name = graph_type.to_s.capitalize + graph_engine.to_s.capitalize + "Grapher"
self.clazz.push MetricFu.const_get(grapher_name).new
end
@@ -24,10 +24,11 @@ def generate
Dir[File.join(MetricFu.data_directory, '*.yml')].sort.each do |metric_file|
puts "Generating graphs for #{metric_file}"
date = metric_file.split('/')[3].split('.')[0]
+ y, m, d = date[0..3].to_i, date[4..5].to_i, date[6..7].to_i
metrics = YAML::load(File.open(metric_file))
self.clazz.each do |grapher|
- grapher.get_metrics(metrics, date)
+ grapher.get_metrics(metrics, "#{m}/#{d}")
end
end
self.clazz.each do |grapher|
View
110 lib/graphs/engines/gchart.rb
@@ -0,0 +1,110 @@
+require 'gchart'
+
+module MetricFu
+ module GchartGrapher
+ COLORS = %w{009999 FF7400 A60000 008500 E6399B 344AD7 00B860 D5CCB9}
+
+ NUMBER_OF_TICKS = 6
+ def determine_y_axis_scale(score)
+ if score.empty?
+ @max_value = 10
+ @yaxis = [0, 2, 4, 6, 8, 10]
+ else
+ @max_value = score.max + Integer(0.1 * score.max)
+ portion_size = (@max_value / (NUMBER_OF_TICKS - 1).to_f).ceil
+ @yaxis = []
+ NUMBER_OF_TICKS.times.each {|n| @yaxis << Integer(portion_size * n) }
+ @max_value = @yaxis.last
+ end
+ end
+ end
+
+ class Grapher
+ include MetricFu::GchartGrapher
+ end
+
+ class FlayGchartGrapher < FlayGrapher
+ def graph!
+ determine_y_axis_scale(@flay_score)
+ url = Gchart.line(
+ :size => MetricFu.graph_size,
+ :title => URI.escape("Flay: duplication"),
+ :data => @flay_score,
+ :max_value => @max_value,
+ :axis_with_labels => 'x,y',
+ :axis_labels => [@labels.values, @yaxis],
+ :format => 'file',
+ :filename => File.join(MetricFu.output_directory, 'flay.png'))
+ end
+ end
+
+ class FlogGchartGrapher < FlogGrapher
+ def graph!
+ determine_y_axis_scale(@top_five_percent_average + @flog_average)
+ url = Gchart.line(
+ :size => MetricFu.graph_size,
+ :title => URI.escape("Flay: code complexity"),
+ :data => [@flog_average, @top_five_percent_average],
+ :stacked => false,
+ :bar_colors => COLORS[0..1],
+ :legend => ['average', 'top 5%25 average'],
+ :max_value => @max_value,
+ :axis_with_labels => 'x,y',
+ :axis_labels => [@labels.values, @yaxis],
+ :format => 'file',
+ :filename => File.join(MetricFu.output_directory, 'flog.png'))
+ end
+ end
+
+ class RcovGchartGrapher < RcovGrapher
+ def graph!
+ determine_y_axis_scale(self.rcov_percent)
+ url = Gchart.line(
+ :size => MetricFu.graph_size,
+ :title => URI.escape("Rcov: code coverage"),
+ :data => self.rcov_percent,
+ :max_value => 101,
+ :axis_with_labels => 'x,y',
+ :axis_labels => [self.labels.values, [0,20,40,60,80,100]],
+ :format => 'file',
+ :filename => File.join(MetricFu.output_directory, 'rcov.png'))
+ end
+ end
+
+ class ReekGchartGrapher < ReekGrapher
+ def graph!
+ determine_y_axis_scale(@reek_count.values.flatten.uniq)
+ values = []
+ legend = @reek_count.keys.sort
+ legend.collect {|k| values << @reek_count[k]}
+
+ url = Gchart.line(
+ :size => MetricFu.graph_size,
+ :title => URI.escape("Reek: code smells"),
+ :data => values,
+ :stacked => false,
+ :bar_colors => COLORS,
+ :legend => legend,
+ :max_value => @max_value,
+ :axis_with_labels => 'x,y',
+ :axis_labels => [@labels.values, @yaxis],
+ :format => 'file',
+ :filename => File.join(MetricFu.output_directory, 'reek.png'))
+ end
+ end
+
+ class RoodiGchartGrapher < RoodiGrapher
+ def graph!
+ determine_y_axis_scale(@roodi_count)
+ url = Gchart.line(
+ :size => MetricFu.graph_size,
+ :title => URI.escape("Roodi: potential design problems"),
+ :data => @roodi_count,
+ :max_value => @max_value,
+ :axis_with_labels => 'x,y',
+ :axis_labels => [@labels.values, @yaxis],
+ :format => 'file',
+ :filename => File.join(MetricFu.output_directory, 'roodi.png'))
+ end
+ end
+end
View
17 lib/graphs/flay_grapher.rb
@@ -1,4 +1,3 @@
-
module MetricFu
class FlayGrapher < Grapher
@@ -13,21 +12,7 @@ def initialize
def get_metrics(metrics, date)
@flay_score.push(metrics[:flay][:total_score].to_i)
- year, month, day = self.class.parsedate(date)
- @labels.update( { @labels.size => "#{month}/#{day}" })
- end
-
- def graph!
- determine_y_axis_scale(@flay_score)
- url = Gchart.line(
- :size => MetricFu.graph_size,
- :title => URI.escape("Flay: duplication"),
- :data => @flay_score,
- :max_value => @max_value,
- :axis_with_labels => 'x,y',
- :axis_labels => [@labels.values, @yaxis],
- :format => 'file',
- :filename => File.join(MetricFu.output_directory, 'flay.png'))
+ @labels.update( { @labels.size => date })
end
end
View
28 lib/graphs/flog_grapher.rb
@@ -14,24 +14,7 @@ def initialize
def get_metrics(metrics, date)
@top_five_percent_average.push(calc_top_five_percent_average(metrics))
@flog_average.push(metrics[:flog][:average])
- year, month, day = self.class.parsedate(date)
- @labels.update( { @labels.size => "#{month}/#{day}" })
- end
-
- def graph!
- determine_y_axis_scale(@top_five_percent_average + @flog_average)
- url = Gchart.line(
- :size => MetricFu.graph_size,
- :title => URI.escape("Flay: code complexity"),
- :data => [@flog_average, @top_five_percent_average],
- :stacked => false,
- :bar_colors => COLORS[0..2],
- :legend => ['average', 'top 5%25 average'],
- :max_value => @max_value,
- :axis_with_labels => 'x,y',
- :axis_labels => [@labels.values, @yaxis],
- :format => 'file',
- :filename => File.join(MetricFu.output_directory, 'flog.png'))
+ @labels.update( { @labels.size => date })
end
private
@@ -43,8 +26,13 @@ def calc_top_five_percent_average(metrics)
number_of_methods_that_is_five_percent = (methods.size * 0.05).ceil
- total_for_five_percent = methods[0...number_of_methods_that_is_five_percent].inject(0) {|total, method| total += method[:score]}
- number_of_methods_that_is_five_percent == 0 ? 0.0 : total_for_five_percent / number_of_methods_that_is_five_percent.to_f
+ total_for_five_percent =
+ methods[0...number_of_methods_that_is_five_percent].inject(0) {|total, method| total += method[:score] }
+ if number_of_methods_that_is_five_percent == 0
+ 0.0
+ else
+ total_for_five_percent / number_of_methods_that_is_five_percent.to_f
+ end
end
end
View
34 lib/graphs/grapher.rb
@@ -1,38 +1,4 @@
module MetricFu
class Grapher
- COLORS = %w{009999 FF7400 A60000 008500 E6399B 344AD7 00B860 D5CCB9}
-
- def initialize
- self.class.require_libraries
- end
-
- def self.require_libraries
- require 'gchart'
- rescue LoadError
- puts "#"*99 + "\n" +
- "If you want to use metric_fu's graphing features then you'll need to install the googlecharts gem. " +
- "If you don't want graphs, then make sure you set config.graphs = [] (see the metric_fu's homepage for more details) " +
- "to indicate that you don't want graphing." +
- "\n" + "#"*99
- raise
- end
-
- def self.parsedate(date)
- [date[0..3].to_i, date[4..5].to_i, date[6..7].to_i]
- end
-
- NUMBER_OF_TICKS = 6
- def determine_y_axis_scale(score)
- if score.empty?
- @max_value = 10
- @yaxis = [0, 2, 4, 6, 8, 10]
- else
- @max_value = score.max + Integer(0.1 * score.max)
- portion_size = (@max_value / (NUMBER_OF_TICKS - 1).to_f).ceil
- @yaxis = []
- NUMBER_OF_TICKS.times.each {|n| @yaxis << Integer(portion_size * n) }
- @max_value = @yaxis.last
- end
- end
end
end
View
16 lib/graphs/rcov_grapher.rb
@@ -12,21 +12,7 @@ def initialize
def get_metrics(metrics, date)
self.rcov_percent.push(metrics[:rcov][:global_percent_run])
- year, month, day = self.class.parsedate(date)
- self.labels.update( { self.labels.size => "#{month}/#{day}" })
- end
-
- def graph!
- determine_y_axis_scale(self.rcov_percent)
- url = Gchart.line(
- :size => MetricFu.graph_size,
- :title => URI.escape("Rcov: code coverage"),
- :data => self.rcov_percent,
- :max_value => 101,
- :axis_with_labels => 'x,y',
- :axis_labels => [self.labels.values, [0,20,40,60,80,100]],
- :format => 'file',
- :filename => File.join(MetricFu.output_directory, 'rcov.png'))
+ self.labels.update( { self.labels.size => date })
end
end
View
23 lib/graphs/reek_grapher.rb
@@ -12,8 +12,7 @@ def initialize
def get_metrics(metrics, date)
counter = @labels.size
- year, month, day = self.class.parsedate(date)
- @labels.update( { @labels.size => "#{month}/#{day}" })
+ @labels.update( { @labels.size => date })
metrics[:reek][:matches].each do |reek_chunk|
reek_chunk[:code_smells].each do |code_smell|
@@ -28,26 +27,6 @@ def get_metrics(metrics, date)
end
end
- def graph!
- determine_y_axis_scale(@reek_count.values.flatten.uniq)
- values = []
- legend = @reek_count.keys.sort
- legend.collect {|k| values << @reek_count[k]}
-
- url = Gchart.line(
- :size => MetricFu.graph_size,
- :title => URI.escape("Reek: code smells"),
- :data => values,
- :stacked => false,
- :bar_colors => COLORS,
- :legend => legend,
- :max_value => @max_value,
- :axis_with_labels => 'x,y',
- :axis_labels => [@labels.values, @yaxis],
- :format => 'file',
- :filename => File.join(MetricFu.output_directory, 'reek.png'))
- end
-
end
end
View
16 lib/graphs/roodi_grapher.rb
@@ -12,21 +12,7 @@ def initialize
def get_metrics(metrics, date)
@roodi_count.push(metrics[:roodi][:problems].size)
- year, month, day = self.class.parsedate(date)
- @labels.update( { @labels.size => "#{month}/#{day}" })
- end
-
- def graph!
- determine_y_axis_scale(@roodi_count)
- url = Gchart.line(
- :size => MetricFu.graph_size,
- :title => URI.escape("Roodi: potential design problems"),
- :data => @roodi_count,
- :max_value => @max_value,
- :axis_with_labels => 'x,y',
- :axis_labels => [@labels.values, @yaxis],
- :format => 'file',
- :filename => File.join(MetricFu.output_directory, 'roodi.png'))
+ @labels.update( { @labels.size => date })
end
end
View
11 lib/metric_fu.rb
@@ -5,9 +5,9 @@ module MetricFu
LIB_ROOT = File.dirname(__FILE__)
end
base_dir = File.join(MetricFu::LIB_ROOT, 'base')
-generator_dir = File.join(MetricFu::LIB_ROOT, 'generators')
-template_dir = File.join(MetricFu::LIB_ROOT, 'templates')
-graph_dir = File.join(MetricFu::LIB_ROOT, 'graphs')
+generator_dir = File.join(MetricFu::LIB_ROOT, 'generators')
+template_dir = File.join(MetricFu::LIB_ROOT, 'templates')
+graph_dir = File.join(MetricFu::LIB_ROOT, 'graphs')
# We need to require these two things first because our other classes
# depend on them.
@@ -27,4 +27,7 @@ module MetricFu
Dir[File.join(template_dir, 'standard/*.rb')].each {|l| require l}
Dir[File.join(template_dir, 'awesome/*.rb')].each {|l| require l}
require graph_dir + "/grapher"
-Dir[File.join(graph_dir, '*.rb')].each {|l| require l}
+Dir[File.join(graph_dir, '*.rb')].each {|l| p l; require l}
+
+# load the graphing files for the specific graphing engine specified in the configuration
+require File.join(graph_dir, 'engines', MetricFu.configuration.graph_engine.to_s.downcase)
View
6 spec/base/graph_spec.rb
@@ -15,10 +15,10 @@
@graph = MetricFu::Graph.new
end
- describe "responding to #add" do
+ describe "responding to #add with gchart enabled" do
it 'should instantiate a grapher and push it to clazz' do
- @graph.clazz.should_receive(:push).with(an_instance_of(RcovGrapher))
- @graph.add("rcov")
+ @graph.clazz.should_receive(:push).with(an_instance_of(RcovGchartGrapher))
+ @graph.add("rcov", 'gchart')
end
end
end
View
4 spec/graphs/flay_grapher_spec.rb
@@ -2,7 +2,7 @@
describe FlayGrapher do
before :each do
- @flay_grapher = MetricFu::FlayGrapher.new
+ @flay_grapher = MetricFu::FlayGchartGrapher.new
MetricFu.configuration
end
@@ -21,7 +21,7 @@
describe "responding to #get_metrics" do
before(:each) do
@metrics = YAML::load(File.open(File.join(File.dirname(__FILE__), "..", "resources", "yml", "20090630.yml")))
- @date = "20030102"
+ @date = "1/2"
end
it "should push 476 to flay_score" do
View
4 spec/graphs/flog_grapher_spec.rb
@@ -2,7 +2,7 @@
describe MetricFu::FlogGrapher do
before :each do
- @flog_grapher = MetricFu::FlogGrapher.new
+ @flog_grapher = MetricFu::FlogGchartGrapher.new
MetricFu.configuration
end
@@ -23,7 +23,7 @@
describe "responding to #get_metrics" do
before(:each) do
@metrics = YAML::load(File.open(File.join(File.dirname(__FILE__), "..", "resources", "yml", "20090630.yml")))
- @date = "20030102"
+ @date = "1/2"
end
it "should push to top_five_percent_average" do
View
9 spec/graphs/grapher_spec.rb
@@ -1,9 +0,0 @@
-require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
-
-describe Grapher do
- it "should blow up if gchart is not availible" do
- Grapher.should_receive(:require).and_raise(LoadError)
- Grapher.should_receive(:puts).with(/config\.graphs/)
- lambda {Grapher.new}.should raise_error(LoadError)
- end
-end
View
4 spec/graphs/rcov_grapher_spec.rb
@@ -2,7 +2,7 @@
describe RcovGrapher do
before :each do
- @rcov_grapher = MetricFu::RcovGrapher.new
+ @rcov_grapher = MetricFu::RcovGchartGrapher.new
MetricFu.configuration
end
@@ -21,7 +21,7 @@
describe "responding to #get_metrics" do
before(:each) do
@metrics = YAML::load(File.open(File.join(File.dirname(__FILE__), "..", "resources", "yml", "20090630.yml")))
- @date = "20030102"
+ @date = "1/2"
end
it "should push 49.6 to rcov_percent" do
View
4 spec/graphs/reek_grapher_spec.rb
@@ -2,7 +2,7 @@
describe ReekGrapher do
before :each do
- @reek_grapher = MetricFu::ReekGrapher.new
+ @reek_grapher = MetricFu::ReekGchartGrapher.new
MetricFu.configuration
end
@@ -21,7 +21,7 @@
describe "responding to #get_metrics" do
before(:each) do
@metrics = YAML::load(File.open(File.join(File.dirname(__FILE__), "..", "resources", "yml", "20090630.yml")))
- @date = "20030102"
+ @date = "1/2"
end
it "should set a hash of code smells to reek_count" do
View
4 spec/graphs/roodi_grapher_spec.rb
@@ -2,7 +2,7 @@
describe RoodiGrapher do
before :each do
- @roodi_grapher = MetricFu::RoodiGrapher.new
+ @roodi_grapher = MetricFu::RoodiGchartGrapher.new
MetricFu.configuration
end
@@ -21,7 +21,7 @@
describe "responding to #get_metrics" do
before(:each) do
@metrics = YAML::load(File.open(File.join(File.dirname(__FILE__), "..", "resources", "yml", "20090630.yml")))
- @date = "20030102"
+ @date = "1/2"
end
it "should push 13 to roodi_count" do
View
2  tasks/metric_fu.rake
@@ -12,7 +12,7 @@ namespace :metrics do
"#{Time.now.strftime("%Y%m%d")}.yml")
MetricFu.report.save_templatized_report
- MetricFu.graphs.each {|graph| MetricFu.graph.add(graph) }
+ MetricFu.graphs.each {|graph| MetricFu.graph.add(graph, MetricFu.graph_engine) }
MetricFu.graph.generate
if MetricFu.report.open_in_browser?
Please sign in to comment.
Something went wrong with that request. Please try again.