flyerhzm-metric_fu 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. data/HISTORY +164 -0
  2. data/MIT-LICENSE +22 -0
  3. data/Manifest.txt +25 -0
  4. data/README.textile +27 -0
  5. data/Rakefile +18 -0
  6. data/TODO +9 -0
  7. data/lib/base/base_template.rb +145 -0
  8. data/lib/base/configuration.rb +180 -0
  9. data/lib/base/generator.rb +167 -0
  10. data/lib/base/graph.rb +39 -0
  11. data/lib/base/md5_tracker.rb +52 -0
  12. data/lib/base/report.rb +100 -0
  13. data/lib/generators/churn.rb +90 -0
  14. data/lib/generators/flay.rb +34 -0
  15. data/lib/generators/flog.rb +167 -0
  16. data/lib/generators/rails_best_practices.rb +31 -0
  17. data/lib/generators/rcov.rb +87 -0
  18. data/lib/generators/reek.rb +64 -0
  19. data/lib/generators/roodi.rb +32 -0
  20. data/lib/generators/saikuro.rb +208 -0
  21. data/lib/generators/stats.rb +43 -0
  22. data/lib/graphs/engines/bluff.rb +98 -0
  23. data/lib/graphs/engines/gchart.rb +119 -0
  24. data/lib/graphs/flay_grapher.rb +20 -0
  25. data/lib/graphs/flog_grapher.rb +40 -0
  26. data/lib/graphs/grapher.rb +11 -0
  27. data/lib/graphs/rails_best_practices_grapher.rb +20 -0
  28. data/lib/graphs/rcov_grapher.rb +20 -0
  29. data/lib/graphs/reek_grapher.rb +32 -0
  30. data/lib/graphs/roodi_grapher.rb +20 -0
  31. data/lib/metric_fu.rb +31 -0
  32. data/lib/templates/awesome/awesome_template.rb +37 -0
  33. data/lib/templates/awesome/churn.html.erb +19 -0
  34. data/lib/templates/awesome/css/buttons.css +82 -0
  35. data/lib/templates/awesome/css/default.css +75 -0
  36. data/lib/templates/awesome/css/integrity.css +335 -0
  37. data/lib/templates/awesome/css/reset.css +7 -0
  38. data/lib/templates/awesome/flay.html.erb +33 -0
  39. data/lib/templates/awesome/flog.html.erb +53 -0
  40. data/lib/templates/awesome/index.html.erb +31 -0
  41. data/lib/templates/awesome/layout.html.erb +30 -0
  42. data/lib/templates/awesome/rails_best_practices.html.erb +27 -0
  43. data/lib/templates/awesome/rcov.html.erb +42 -0
  44. data/lib/templates/awesome/reek.html.erb +40 -0
  45. data/lib/templates/awesome/roodi.html.erb +27 -0
  46. data/lib/templates/awesome/saikuro.html.erb +71 -0
  47. data/lib/templates/awesome/stats.html.erb +41 -0
  48. data/lib/templates/javascripts/bluff-min.js +1 -0
  49. data/lib/templates/javascripts/excanvas.js +19 -0
  50. data/lib/templates/javascripts/js-class.js +1 -0
  51. data/lib/templates/standard/churn.html.erb +31 -0
  52. data/lib/templates/standard/default.css +64 -0
  53. data/lib/templates/standard/flay.html.erb +34 -0
  54. data/lib/templates/standard/flog.html.erb +53 -0
  55. data/lib/templates/standard/index.html.erb +41 -0
  56. data/lib/templates/standard/rails_best_practices.html.erb +29 -0
  57. data/lib/templates/standard/rcov.html.erb +43 -0
  58. data/lib/templates/standard/reek.html.erb +42 -0
  59. data/lib/templates/standard/roodi.html.erb +29 -0
  60. data/lib/templates/standard/saikuro.html.erb +84 -0
  61. data/lib/templates/standard/standard_template.rb +26 -0
  62. data/lib/templates/standard/stats.html.erb +55 -0
  63. data/spec/base/base_template_spec.rb +161 -0
  64. data/spec/base/configuration_spec.rb +269 -0
  65. data/spec/base/generator_spec.rb +244 -0
  66. data/spec/base/graph_spec.rb +24 -0
  67. data/spec/base/md5_tracker_spec.rb +57 -0
  68. data/spec/base/report_spec.rb +139 -0
  69. data/spec/generators/churn_spec.rb +152 -0
  70. data/spec/generators/flay_spec.rb +104 -0
  71. data/spec/generators/flog_spec.rb +238 -0
  72. data/spec/generators/reek_spec.rb +126 -0
  73. data/spec/generators/saikuro_spec.rb +58 -0
  74. data/spec/generators/stats_spec.rb +74 -0
  75. data/spec/graphs/engines/bluff_spec.rb +15 -0
  76. data/spec/graphs/engines/gchart_spec.rb +15 -0
  77. data/spec/graphs/flay_grapher_spec.rb +37 -0
  78. data/spec/graphs/flog_grapher_spec.rb +45 -0
  79. data/spec/graphs/rcov_grapher_spec.rb +37 -0
  80. data/spec/graphs/reek_grapher_spec.rb +46 -0
  81. data/spec/graphs/roodi_grapher_spec.rb +37 -0
  82. data/spec/resources/saikuro/app/controllers/sessions_controller.rb_cyclo.html +10 -0
  83. data/spec/resources/saikuro/app/controllers/users_controller.rb_cyclo.html +16 -0
  84. data/spec/resources/saikuro/index_cyclo.html +155 -0
  85. data/spec/resources/saikuro_sfiles/thing.rb_cyclo.html +11 -0
  86. data/spec/resources/yml/20090630.yml +7844 -0
  87. data/spec/spec.opts +8 -0
  88. data/spec/spec_helper.rb +7 -0
  89. data/tasks/metric_fu.rake +22 -0
  90. data/vendor/_fonts/monaco.ttf +0 -0
  91. data/vendor/saikuro/saikuro.rb +1219 -0
  92. metadata +234 -0
@@ -0,0 +1,167 @@
1
+ module MetricFu
2
+
3
+ # = Generator
4
+ #
5
+ # The Generator class is an abstract class that provides the
6
+ # skeleton for producing different types of metrics.
7
+ #
8
+ # It drives the production of the metrics through a template
9
+ # method - #generate_report(options={}). This method calls
10
+ # #emit, #analyze and #to_h in order to produce the metrics.
11
+ #
12
+ # To implement a concrete class to generate a metric, therefore,
13
+ # the class must implement those three methods.
14
+ #
15
+ # * #emit should take care of running the metric tool and
16
+ # gathering its output.
17
+ # * #analyze should take care of manipulating the output from
18
+ # #emit and making it possible to store it in a programmatic way.
19
+ # * #to_h should provide a hash representation of the output from
20
+ # #analyze ready to be serialized into yaml at some point.
21
+ #
22
+ # == Pre-conditions
23
+ #
24
+ # Based on the class name of the concrete class implementing a
25
+ # Generator, the Generator class will create a 'metric_directory'
26
+ # named after the class under the MetricFu.scratch_directory, where
27
+ # any output from the #emit method should go.
28
+ #
29
+ # It will also create the MetricFu.output_directory if neccessary, and
30
+ # in general setup the directory structure that the MetricFu system
31
+ # expects.
32
+ class Generator
33
+ attr_reader :report, :template
34
+
35
+ def initialize(options={})
36
+ self.class.verify_dependencies!
37
+ create_metric_dir_if_missing
38
+ create_output_dir_if_missing
39
+ create_data_dir_if_missing
40
+ end
41
+
42
+ # Creates a new generator and returns the output of the
43
+ # #generate_report method. This is the typical way to
44
+ # generate a new MetricFu report. For more information see
45
+ # the #generate_report instance method.
46
+ #
47
+ # @params options Hash
48
+ # A currently unused hash to configure the Generator
49
+ #
50
+ # @see generate_report
51
+ def self.generate_report(options={})
52
+ generator = self.new(options)
53
+ generator.generate_report
54
+ end
55
+
56
+ # Provides the unqualified class name of an implemented concrete
57
+ # class, as a string. For example:
58
+ #
59
+ # class Flay < Generator; end
60
+ # klass = Flay.new
61
+ # klass.class_name
62
+ # > "flay"
63
+ #
64
+ # @return String
65
+ # The unqualified class name of this concrete class, returned
66
+ # as a string.
67
+ def self.class_name
68
+ self.to_s.split('::').last.downcase
69
+ end
70
+
71
+ # Returns the directory where the Generator will write any output
72
+ def self.metric_directory
73
+ File.join(MetricFu.scratch_directory, class_name)
74
+ end
75
+
76
+ def create_metric_dir_if_missing #:nodoc:
77
+ unless File.directory?(metric_directory)
78
+ FileUtils.mkdir_p(metric_directory, :verbose => false)
79
+ end
80
+ end
81
+
82
+ def create_output_dir_if_missing #:nodoc:
83
+ unless File.directory?(MetricFu.output_directory)
84
+ FileUtils.mkdir_p(MetricFu.output_directory, :verbose => false)
85
+ end
86
+ end
87
+
88
+ def create_data_dir_if_missing #:nodoc:
89
+ unless File.directory?(MetricFu.data_directory)
90
+ FileUtils.mkdir_p(MetricFu.data_directory, :verbose => false)
91
+ end
92
+ end
93
+
94
+ # @return String
95
+ # The path of the metric directory this class is using.
96
+ def metric_directory
97
+ self.class.metric_directory
98
+ end
99
+
100
+ def remove_excluded_files(paths, globs_to_remove = MetricFu.file_globs_to_ignore)
101
+ files_to_remove = []
102
+ globs_to_remove.each do |glob|
103
+ files_to_remove.concat(Dir[glob])
104
+ end
105
+ paths - files_to_remove
106
+ end
107
+
108
+ # Defines some hook methods for the concrete classes to hook into.
109
+ %w[emit analyze].each do |meth|
110
+ define_method("before_#{meth}".to_sym) {}
111
+ define_method("after_#{meth}".to_sym) {}
112
+ end
113
+ define_method("before_to_h".to_sym) {}
114
+
115
+ # Provides a template method to drive the production of a metric
116
+ # from a concrete implementation of this class. Each concrete
117
+ # class must implement the three methods that this template method
118
+ # calls: #emit, #analyze and #to_h. For more details, see the
119
+ # class documentation.
120
+ #
121
+ # This template method also calls before_emit, after_emit... etc.
122
+ # methods to allow extra hooks into the processing methods, and help
123
+ # to keep the logic of your Generators clean.
124
+ def generate_report
125
+ %w[emit analyze].each do |meth|
126
+ send("before_#{meth}".to_sym)
127
+ send("#{meth}".to_sym)
128
+ send("after_#{meth}".to_sym)
129
+ end
130
+ before_to_h()
131
+ to_h()
132
+ end
133
+
134
+ def round_to_tenths(decimal)
135
+ (decimal * 10).round / 10.0
136
+ end
137
+
138
+ # Allows subclasses to check for required gems
139
+ def self.verify_dependencies!
140
+ true
141
+ end
142
+
143
+ def emit #:nodoc:
144
+ raise <<-EOF
145
+ This method must be implemented by a concrete class descending
146
+ from Generator. See generator class documentation for more
147
+ information.
148
+ EOF
149
+ end
150
+
151
+ def analyze #:nodoc:
152
+ raise <<-EOF
153
+ This method must be implemented by a concrete class descending
154
+ from Generator. See generator class documentation for more
155
+ information.
156
+ EOF
157
+ end
158
+
159
+ def to_graph #:nodoc:
160
+ raise <<-EOF
161
+ This method must be implemented by a concrete class descending
162
+ from Generator. See generator class documentation for more
163
+ information.
164
+ EOF
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,39 @@
1
+ module MetricFu
2
+
3
+ def self.graph
4
+ @graph ||= Graph.new
5
+ end
6
+
7
+ class Graph
8
+
9
+ attr_accessor :clazz
10
+
11
+ def initialize
12
+ self.clazz = []
13
+ end
14
+
15
+ def add(graph_type, graph_engine)
16
+ grapher_name = graph_type.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } + graph_engine.to_s.capitalize + "Grapher"
17
+ self.clazz.push MetricFu.const_get(grapher_name).new
18
+ end
19
+
20
+
21
+ def generate
22
+ return if self.clazz.empty?
23
+ puts "Generating graphs"
24
+ Dir[File.join(MetricFu.data_directory, '*.yml')].sort.each do |metric_file|
25
+ puts "Generating graphs for #{metric_file}"
26
+ date = metric_file.split('/')[3].split('.')[0]
27
+ y, m, d = date[0..3].to_i, date[4..5].to_i, date[6..7].to_i
28
+ metrics = YAML::load(File.open(metric_file))
29
+
30
+ self.clazz.each do |grapher|
31
+ grapher.get_metrics(metrics, "#{m}/#{d}")
32
+ end
33
+ end
34
+ self.clazz.each do |grapher|
35
+ grapher.graph!
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,52 @@
1
+ require 'digest/md5'
2
+ require 'fileutils'
3
+
4
+ module MetricFu
5
+ class MD5Tracker
6
+
7
+ @@unchanged_md5s = []
8
+
9
+ class << self
10
+ def md5_dir(path_to_file, base_dir)
11
+ File.join(base_dir,
12
+ path_to_file.split('/')[0..-2].join('/'))
13
+ end
14
+
15
+ def md5_file(path_to_file, base_dir)
16
+ File.join(md5_dir(path_to_file, base_dir),
17
+ path_to_file.split('/').last.sub(/\.[a-z]+/, '.md5'))
18
+ end
19
+
20
+ def track(path_to_file, base_dir)
21
+ md5 = Digest::MD5.hexdigest(File.read(path_to_file))
22
+ FileUtils.mkdir_p(md5_dir(path_to_file, base_dir), :verbose => false)
23
+ f = File.new(md5_file(path_to_file, base_dir), "w")
24
+ f.puts(md5)
25
+ f.close
26
+ md5
27
+ end
28
+
29
+ def file_changed?(path_to_file, base_dir)
30
+ orig_md5_file = md5_file(path_to_file, base_dir)
31
+ return !!track(path_to_file, base_dir) unless File.exist?(orig_md5_file)
32
+
33
+ current_md5 = ""
34
+ file = File.open(orig_md5_file, 'r')
35
+ file.each_line { |line| current_md5 << line }
36
+ file.close
37
+ current_md5.chomp!
38
+
39
+ new_md5 = Digest::MD5.hexdigest(File.read(path_to_file))
40
+ new_md5.chomp!
41
+
42
+ @@unchanged_md5s << path_to_file if new_md5 == current_md5
43
+
44
+ return new_md5 != current_md5
45
+ end
46
+
47
+ def file_already_counted?(path_to_file)
48
+ return @@unchanged_md5s.include?(path_to_file)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,100 @@
1
+ module MetricFu
2
+
3
+ # MetricFu.report memoizes access to a Report object, that will be
4
+ # used throughout the lifecycle of the MetricFu app.
5
+ def self.report
6
+ @report ||= Report.new
7
+ end
8
+
9
+ # = Report
10
+ #
11
+ # The Report class is responsible two things:
12
+ #
13
+ # It adds information to the yaml report, produced by the system
14
+ # as a whole, for each of the generators used in this test run.
15
+ #
16
+ # It also handles passing the information from each generator used
17
+ # in this test run out to the template class set in
18
+ # MetricFu::Configuration.
19
+ class Report
20
+
21
+ # Renders the result of the report_hash into a yaml serialization
22
+ # ready for writing out to a file.
23
+ #
24
+ # @return YAML
25
+ # A YAML object containing the results of the report generation
26
+ # process
27
+ def to_yaml
28
+ report_hash.to_yaml
29
+ end
30
+
31
+
32
+ def report_hash #:nodoc:
33
+ @report_hash ||= {}
34
+ end
35
+
36
+ # Instantiates a new template class based on the configuration set
37
+ # in MetricFu::Configuration, or through the MetricFu.config block
38
+ # in your rake file (defaults to the included AwesomeTemplate),
39
+ # assigns the report_hash to the report_hash in the template, and
40
+ # tells the template to to write itself out.
41
+ def save_templatized_report
42
+ @template = MetricFu.template_class.new
43
+ @template.report = report_hash
44
+ @template.write
45
+ end
46
+
47
+ # Adds a hash from a passed report, produced by one of the Generator
48
+ # classes to the aggregate report_hash managed by this hash.
49
+ #
50
+ # @param report_type Hash
51
+ # The hash to add to the aggregate report_hash
52
+ def add(report_type)
53
+ clazz = MetricFu.const_get(report_type.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase })
54
+ report_hash.merge!(clazz.generate_report)
55
+ end
56
+
57
+ # Saves the passed in content to the passed in directory. If
58
+ # a filename is passed in it will be used as the name of the
59
+ # file, otherwise it will default to 'index.html'
60
+ #
61
+ # @param content String
62
+ # A string containing the content (usually html) to be written
63
+ # to the file.
64
+ #
65
+ # @param dir String
66
+ # A dir containing the path to the directory to write the file in.
67
+ #
68
+ # @param file String
69
+ # A filename to save the path as. Defaults to 'index.html'.
70
+ #
71
+ def save_output(content, dir, file='index.html')
72
+ open("#{dir}/#{file}", "w") do |f|
73
+ f.puts content
74
+ end
75
+ end
76
+
77
+ # Checks to discover whether we should try and open the results
78
+ # of the report in the browser on this system. We only try and open
79
+ # in the browser if we're on OS X and we're not running in a
80
+ # CruiseControl.rb environment. See MetricFu.configuration for more
81
+ # details about how we make those guesses.
82
+ #
83
+ # @return Boolean
84
+ # Should we open in the browser or not?
85
+ def open_in_browser?
86
+ MetricFu.configuration.platform.include?('darwin') &&
87
+ ! MetricFu.configuration.is_cruise_control_rb?
88
+ end
89
+
90
+ # Shows 'index.html' from the passed directory in the browser
91
+ # if we're able to open the browser on this platform.
92
+ #
93
+ # @param dir String
94
+ # The directory path where the 'index.html' we want to open is
95
+ # stored
96
+ def show_in_browser(dir)
97
+ system("open #{dir}/index.html") if open_in_browser?
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,90 @@
1
+ require 'chronic'
2
+ module MetricFu
3
+
4
+ class Churn < Generator
5
+
6
+
7
+ def initialize(options={})
8
+ super
9
+ if self.class.git?
10
+ @source_control = Git.new(MetricFu.churn[:start_date])
11
+ elsif File.exist?(".svn")
12
+ @source_control = Svn.new(MetricFu.churn[:start_date])
13
+ else
14
+ raise "Churning requires a subversion or git repo"
15
+ end
16
+ @minimum_churn_count = MetricFu.churn[:minimum_churn_count] || 5
17
+ end
18
+
19
+ def self.git?
20
+ system("git branch")
21
+ end
22
+
23
+ def emit
24
+ @changes = parse_log_for_changes.reject {|file, change_count| change_count < @minimum_churn_count}
25
+ end
26
+
27
+ def analyze
28
+ @changes = @changes.to_a.sort {|x,y| y[1] <=> x[1]}
29
+ @changes = @changes.map {|change| {:file_path => change[0], :times_changed => change[1] }}
30
+ end
31
+
32
+ def to_h
33
+ {:churn => {:changes => @changes}}
34
+ end
35
+
36
+ private
37
+
38
+ def parse_log_for_changes
39
+ changes = {}
40
+
41
+ logs = @source_control.get_logs
42
+ logs.each do |line|
43
+ changes[line] ? changes[line] += 1 : changes[line] = 1
44
+ end
45
+ changes
46
+ end
47
+
48
+
49
+ class SourceControl
50
+ def initialize(start_date=nil)
51
+ @start_date = start_date
52
+ end
53
+ end
54
+
55
+ class Git < SourceControl
56
+ def get_logs
57
+ `git log #{date_range} --name-only --pretty=format:`.split(/\n/).reject{|line| line == ""}
58
+ end
59
+
60
+ private
61
+ def date_range
62
+ if @start_date
63
+ date = Chronic.parse(@start_date)
64
+ "--after=#{date.strftime('%Y-%m-%d')}"
65
+ end
66
+ end
67
+
68
+ end
69
+
70
+ class Svn < SourceControl
71
+ def get_logs
72
+ `svn log #{date_range} --verbose`.split(/\n/).map { |line| clean_up_svn_line(line) }.compact
73
+ end
74
+
75
+ private
76
+ def date_range
77
+ if @start_date
78
+ date = Chronic.parse(@start_date)
79
+ "--revision {#{date.strftime('%Y-%m-%d')}}:{#{Time.now.strftime('%Y-%m-%d')}}"
80
+ end
81
+ end
82
+
83
+ def clean_up_svn_line(line)
84
+ m = line.match(/\W*[A,M]\W+(\/.*)\b/)
85
+ m ? m[1] : nil
86
+ end
87
+ end
88
+
89
+ end
90
+ end