files and lines by extension

This commit is contained in:
Tomasz Gieniusz 2012-10-13 19:00:31 +02:00
parent 5d2f838bae
commit 7ce8a2a7eb
9 changed files with 39 additions and 4 deletions

View file

@ -15,6 +15,10 @@ module GitStats
@content ||= Command.new(repo, "git cat-file blob #{self.hash}").run
end
def extension
@ext ||= File.extname(filename)
end
def to_s
"#{self.class} #@hash #@filename"
end

View file

@ -15,6 +15,14 @@ module GitStats
end
end
def files_by_extension
@files_by_extension ||= files.inject({}) { |acc, f| acc[f.extension] ||= []; acc[f.extension] << f; acc }
end
def lines_by_extension
@lines_by_extension ||= Hash[files_by_extension.map { |ext, files| [ext, files.map(&:lines_count).sum] }]
end
def files_count
@files_count ||= Command.new(repo, "git ls-tree -r --name-only #{self.hash} | wc -l").run.to_i
end

View file

@ -1,5 +1,6 @@
module HashInitializable
def initialize(params = {})
raise "pass a Hash to initialize #{self.class}" unless params.is_a? Hash
params.each { |k, v| instance_variable_set("@#{k}", v) }
end
end

View file

@ -42,6 +42,26 @@ module GitStats
end
end
def files_by_extension
@files_by_extension ||= LazyHighCharts::HighChart.new('graph') do |f|
f.chart(type: "column")
f.title(text: "files by extension")
f.xAxis(categories: repo.commits.last.files_by_extension.keys)
f.yAxis(min: 0, title: {text: 'Commits'})
f.series(name: "commits", data: repo.commits.last.files_by_extension.values.map(&:count))
end
end
def lines_by_extension
@lines_by_extension ||= LazyHighCharts::HighChart.new('graph') do |f|
f.chart(type: "column")
f.title(text: "files by extension")
f.xAxis(categories: repo.commits.last.lines_by_extension.keys)
f.yAxis(min: 0, title: {text: 'Commits'})
f.series(name: "commits", data: repo.commits.last.lines_by_extension.values)
end
end
def files_by_date
@files_by_date ||= LazyHighCharts::HighChart.new('graph') do |f|
f.title(text: "Files")

View file

@ -1,7 +1,7 @@
require 'spec_helper'
describe GitStats::GitData::Author do
let(:repo) { GitStats::GitData::Repo.new("repo_path") }
let(:repo) { GitStats::GitData::Repo.new(path: "repo_path") }
let(:author) { GitStats::GitData::Author.new(repo: repo, name: "author1", email: "author1@gmail.com") }
let(:other_author) { GitStats::GitData::Author.new(repo: repo, name: "author2", email: "author2@gmail.com") }
let(:my_commits) { 10.times.map { |i| double("my_commit #{i}", :author => author) } }

View file

@ -19,7 +19,7 @@ describe GitStats::Generator do
it 'should render all templates with view data for this repo' do
repo = double('repo')
GitStats::GitData::Repo.should_receive(:new).with(repo_path).and_return(repo)
GitStats::GitData::Repo.should_receive(:new).with(path: repo_path, git_command_observer: nil).and_return(repo)
view_data = double('view_data')
GitStats::StatsView::ViewData.should_receive(:new).with(repo).and_return(view_data)

View file

@ -1,7 +1,7 @@
require 'spec_helper'
describe GitStats::GitData::Repo do
let(:repo) { GitStats::GitData::Repo.new("repo_path") }
let(:repo) { GitStats::GitData::Repo.new(path: "repo_path") }
describe 'git output parsing' do
before {

View file

@ -1 +1,2 @@
= high_chart("files_by_date", files_by_date)
= high_chart("files_by_extension", files_by_extension)

View file

@ -1 +1,2 @@
= high_chart("lines_by_date", lines_by_date)
= high_chart("lines_by_date", lines_by_date)
= high_chart("lines_by_extension", lines_by_extension)