From 71c06cd8e3a7295783764f8e6bc1996a3bddfe58 Mon Sep 17 00:00:00 2001 From: Tomasz Gieniusz Date: Sat, 13 Oct 2012 16:52:25 +0200 Subject: [PATCH] by_hour and lines_by_date charts --- lib/git_stats/core_extensions/hash.rb | 5 +++++ lib/git_stats/git_data/blob.rb | 2 +- lib/git_stats/git_data/commit.rb | 4 ++++ lib/git_stats/stats_view/view.rb | 2 +- lib/git_stats/stats_view/view_data.rb | 30 ++++++++++++++++++++++++++- templates/activity.haml | 1 + templates/files.haml | 3 ++- 7 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 lib/git_stats/core_extensions/hash.rb create mode 100644 templates/activity.haml diff --git a/lib/git_stats/core_extensions/hash.rb b/lib/git_stats/core_extensions/hash.rb new file mode 100644 index 000000000..995fb370a --- /dev/null +++ b/lib/git_stats/core_extensions/hash.rb @@ -0,0 +1,5 @@ +class Hash + def to_key_indexed_array + self.inject([]) { |acc, (k, v)| acc[k] = v; acc } + end +end \ No newline at end of file diff --git a/lib/git_stats/git_data/blob.rb b/lib/git_stats/git_data/blob.rb index 3e4f55254..d4b717290 100644 --- a/lib/git_stats/git_data/blob.rb +++ b/lib/git_stats/git_data/blob.rb @@ -8,7 +8,7 @@ module GitStats attr_reader :repo, :hash, :filename def lines_count - @lines ||= Command.new(repo, "git cat-file blob #{self.hash} | wc -l").run.to_i + @lines_count ||= Command.new(repo, "git cat-file blob #{self.hash} | wc -l").run.to_i end def content diff --git a/lib/git_stats/git_data/commit.rb b/lib/git_stats/git_data/commit.rb index 8b1a0c07b..7a4a85d7c 100644 --- a/lib/git_stats/git_data/commit.rb +++ b/lib/git_stats/git_data/commit.rb @@ -19,6 +19,10 @@ module GitStats @files_count ||= Command.new(repo, "git ls-tree -r --name-only #{self.hash} | wc -l").run.to_i end + def lines_count + @lines_count ||= files.map(&:lines_count).sum + end + def short_stat @short_stat ||= ShortStat.new(self) end diff --git a/lib/git_stats/stats_view/view.rb b/lib/git_stats/stats_view/view.rb index 65b5c8bf7..f0a09ee14 100644 --- a/lib/git_stats/stats_view/view.rb +++ b/lib/git_stats/stats_view/view.rb @@ -17,7 +17,7 @@ module GitStats end def all_templates - @all_templates ||= (Dir["templates/*.haml"] - Dir["templates/layout.haml"]).map { |t| File.basename(t, ".haml") } + %w(index activity authors files) end def prepare_assets diff --git a/lib/git_stats/stats_view/view_data.rb b/lib/git_stats/stats_view/view_data.rb index 258423fcc..7ebbe0eba 100644 --- a/lib/git_stats/stats_view/view_data.rb +++ b/lib/git_stats/stats_view/view_data.rb @@ -27,11 +27,21 @@ module GitStats shadow: true ) repo.authors.each do |email, author| - f.series(name: email, data: author.activity.by_wday.inject([]) { |acc, (k, v)| acc[k] = v; acc }) + f.series(name: email, data: author.activity.by_wday.to_key_indexed_array) end end end + def by_hour + @by_hour ||= LazyHighCharts::HighChart.new('graph') do |f| + f.chart(type: "column") + f.title(text: "Commits") + f.xAxis(categories: (0..23)) + f.yAxis(min: 0, title: {text: 'Commits'}) + f.series(name: "commits", data: repo.activity.by_hour.to_key_indexed_array) + end + end + def files_by_date @files_by_date ||= LazyHighCharts::HighChart.new('graph') do |f| f.title(text: "Files") @@ -49,6 +59,24 @@ module GitStats ) end end + + def lines_by_date + @lines_by_date ||= LazyHighCharts::HighChart.new('graph') do |f| + f.title(text: "Files") + f.xAxis(type: "datetime") + f.yAxis(min: 0, title: {text: 'Commits'}) + rcommits = repo.commits.reverse + f.series( + type: "area", + name: "commits", + pointInterval: 1.day * 1000, + pointStart: repo.commits.first.date.to_i * 1000, + data: repo.commits.first.date.midnight.upto((repo.commits.last.date + 1.day).midnight).map { |day| + rcommits.find { |c| c.date < day }.lines_count rescue 0 + } + ) + end + end end end end \ No newline at end of file diff --git a/templates/activity.haml b/templates/activity.haml new file mode 100644 index 000000000..1463eacb3 --- /dev/null +++ b/templates/activity.haml @@ -0,0 +1 @@ += high_chart("by_hour", by_hour) \ No newline at end of file diff --git a/templates/files.haml b/templates/files.haml index 9c93e343b..ed5987c6a 100644 --- a/templates/files.haml +++ b/templates/files.haml @@ -1 +1,2 @@ -= high_chart("files_by_date", files_by_date) \ No newline at end of file += high_chart("files_by_date", files_by_date) += high_chart("lines_by_date", lines_by_date) \ No newline at end of file