Skip to content
This repository was archived by the owner on Sep 25, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions app/controllers/conductor/satistics_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'rails/code_statistics'

STATS_DIRECTORIES = [
%w(Controllers app/controllers),
%w(Helpers app/helpers),
%w(Models app/models),
%w(Libraries lib/),
%w(APIs app/apis),
%w(Integration\ tests test/integration),
%w(Functional\ tests test/functional),
%w(Unit\ tests test/unit)
].collect { |name, dir| [ name, "#{Rails.root}/#{dir}" ] }.select { |name, dir| File.directory?(dir) }

module Conductor
class StatisticsController < ApplicationController
def index
@stats = Conductor::CodeStatistics.new(*STATS_DIRECTORIES)
end
end
end
89 changes: 89 additions & 0 deletions app/models/conductor/code_statistics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
TEST_TYPES = %w(Units Functionals Unit\ tests Functional\ tests Integration\ tests)
module Conductor
class CodeStatistics #:nodoc:
attr_reader :pairs

def initialize(*pairs)
@pairs = pairs
end

def lines
@pairs.collect do |pair|
line(pair.first, statistics[pair.first])
end
end

def total_line
line("Total", calculate_total)
end

def statistics
@pairs.inject({}) do |stats, pair|
stats[pair.first] = calculate_directory_statistics(pair.last)
stats
end
end

def calculate_directory_statistics(directory, pattern = /.*\.rb$/)
stats = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }

Dir.foreach(directory) do |file_name|
if File.stat(directory + "/" + file_name).directory? and (/^\./ !~ file_name)
newstats = calculate_directory_statistics(directory + "/" + file_name, pattern)
stats.each { |k, v| stats[k] += newstats[k] }
end

next unless file_name =~ pattern

f = File.open(directory + "/" + file_name)

while line = f.gets
stats["lines"] += 1
stats["classes"] += 1 if line =~ /class [A-Z]/
stats["methods"] += 1 if line =~ /def [a-z]/
stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ /^\s*#/
end
end

stats
end

def calculate_total
total = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
statistics.each_value { |pair| pair.each { |k, v| total[k] += v } }
total
end

def code_loc
code_loc = 0
statistics.each { |k, v| code_loc += v['codelines'] unless TEST_TYPES.include? k }
code_loc
end

def tests_loc
test_loc = 0
statistics.each { |k, v| test_loc += v['codelines'] if TEST_TYPES.include? k }
test_loc
end

def line(name, statistics)
m_over_c = (statistics["methods"] / statistics["classes"]) rescue m_over_c = 0
loc_over_m = (statistics["codelines"] / statistics["methods"]) - 2 rescue loc_over_m = 0

{
:name => name,
:lines => statistics["lines"],
:codelines => statistics["codelines"],
:classes => statistics["classes"],
:methods => statistics["methods"],
:m_over_c => m_over_c,
:loc_over_m => loc_over_m
}
end

def code_to_test
tests_loc.to_f/code_loc
end

end
end
46 changes: 46 additions & 0 deletions app/views/conductor/statistics/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<h1>Statistics</h1>

<table>
<tr>
<th>Name</th>
<th>Lines</th>
<th>LOC</th>
<th>Classes</th>
<th>Methods</th>
<th>M/C</th>
<th>LOC/M</th>
</tr>
<% @stats.lines.each do |line| %>
<tr class="statistic">
<th><%= line[:name] %></th>
<td><%= line[:lines] %></td>
<td><%= line[:codelines] %></td>
<td><%= line[:classes] %></td>
<td><%= line[:methods] %></td>
<td><%= line[:m_over_c] %></td>
<td><%= line[:loc_over_m] %></td>
</tr>
<% end %>

<%
if @stats.pairs.length > 1
line = @stats.total_line
%>
<tr class="statistic">
<th><%= line[:name] %></th>
<td><%= line[:lines] %></td>
<td><%= line[:codelines] %></td>
<td><%= line[:classes] %></td>
<td><%= line[:methods] %></td>
<td><%= line[:m_over_c] %></td>
<td><%= line[:loc_over_m] %></td>
</tr>
<% end %>
</table>

<br>
<table>
<tr class="statistic"><th>Code LOC</th><td><%= @stats.code_loc %></td></tr>
<tr class="statistic"><th>Test LOC</th><td><%= @stats.tests_loc %></td></tr>
<tr class="statistic"><th>Code to Test Ratio</th><td>1:<%= @stats.code_to_test %></td></tr>
</table>
2 changes: 1 addition & 1 deletion app/views/layouts/conductor/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<ul class="links">
<li><%= link_to("Routes", conductor_routes_path) %></li>
<li><%= link_to("Annotations", conductor_annotations_path) %></li>
<li>Statistics</li>
<li><%= link_to("Statistics", conductor_statistics_path) %></li>
</ul>
</li>

Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Rails.application.routes.draw do
namespace :conductor do
resources :scaffolds, :routes, :annotations
resources :scaffolds, :routes, :annotations, :statistics
end
end
14 changes: 13 additions & 1 deletion public/stylesheets/conductor.css
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,16 @@ a:hover {
}
#sidebar ul.links li {
margin-bottom: 5px;
}
}

tr.statistic th {
text-align: left;
}

tr.statistic th {
text-align: left;
}

tr.statistic td {
text-align: right;
}