diff --git a/app/controllers/projects/graphs_controller.rb b/app/controllers/projects/graphs_controller.rb index d66465d95795c928f635c533ce4d478dbef9c968..610b4967fea974ad20c49bb7b8b7e660029e4872 100644 --- a/app/controllers/projects/graphs_controller.rb +++ b/app/controllers/projects/graphs_controller.rb @@ -15,33 +15,10 @@ class Projects::GraphsController < Projects::ApplicationController def commits @commits = @project.repository.commits(nil, nil, 2000, 0, true) - @start_date = @commits.last.committed_date.to_date - @end_date = @commits.first.committed_date.to_date - @duration = (@end_date - @start_date).to_i - @authors = @commits.map(&:author_email).uniq.size - @commit_per_day = (@commits.size.to_f / @duration).round(1) - - @commits_per_week_days = {} - Date::DAYNAMES.each { |day| @commits_per_week_days[day] = 0 } - - @commits_per_time = {} - (0..23).to_a.each { |hour| @commits_per_time[hour] = 0 } - - @commits_per_month = {} - (1..31).to_a.each { |day| @commits_per_month[day] = 0 } - - @commits.each do |commit| - hour = commit.committed_date.strftime('%k').to_i - day_of_month = commit.committed_date.strftime('%e').to_i - weekday = commit.committed_date.strftime('%A') - - @commits_per_week_days[weekday] ||= 0 - @commits_per_week_days[weekday] += 1 - @commits_per_time[hour] ||= 0 - @commits_per_time[hour] += 1 - @commits_per_month[day_of_month] ||= 0 - @commits_per_month[day_of_month] += 1 - end + @commits_graph = Gitlab::Graphs::Commits.new(@commits) + @commits_per_week_days = @commits_graph.commits_per_week_days + @commits_per_time = @commits_graph.commits_per_time + @commits_per_month = @commits_graph.commits_per_month end private diff --git a/app/views/projects/graphs/commits.html.haml b/app/views/projects/graphs/commits.html.haml index 2b72bdfae21a7b2e3b65a43765228964f39d6846..a189a487135ffeb7d6588857542301a4eb842828 100644 --- a/app/views/projects/graphs/commits.html.haml +++ b/app/views/projects/graphs/commits.html.haml @@ -3,26 +3,26 @@ %p.lead Commits statistic for %strong #{@repository.root_ref} - #{@start_date.strftime('%b %d')} - #{@end_date.strftime('%b %d')} + #{@commits_graph.start_date.strftime('%b %d')} - #{@commits_graph.end_date.strftime('%b %d')} .row .col-md-6 %ul %li %p.lead - %strong #{@commits.size} + %strong #{@commits_graph.commits.size} commits during - %strong #{@duration} + %strong #{@commits_graph.duration} days %li %p.lead Average - %strong #{@commit_per_day} + %strong #{@commits_graph.commit_per_day} commits per day %li %p.lead Contributed by - %strong #{@authors} + %strong #{@commits_graph.authors} authors .col-md-6 %div diff --git a/lib/gitlab/graphs/commits.rb b/lib/gitlab/graphs/commits.rb new file mode 100644 index 0000000000000000000000000000000000000000..2122339d2db89cbe72393837b713e274626d2dff --- /dev/null +++ b/lib/gitlab/graphs/commits.rb @@ -0,0 +1,49 @@ +module Gitlab + module Graphs + class Commits + attr_reader :commits, :start_date, :end_date, :duration, + :commits_per_week_days, :commits_per_time, :commits_per_month + + def initialize(commits) + @commits = commits + @start_date = commits.last.committed_date.to_date + @end_date = commits.first.committed_date.to_date + @duration = (@end_date - @start_date).to_i + + collect_data + end + + def authors + @authors ||= @commits.map(&:author_email).uniq.size + end + + def commit_per_day + @commit_per_day ||= (@commits.size.to_f / @duration).round(1) + end + + def collect_data + @commits_per_week_days = {} + Date::DAYNAMES.each { |day| @commits_per_week_days[day] = 0 } + + @commits_per_time = {} + (0..23).to_a.each { |hour| @commits_per_time[hour] = 0 } + + @commits_per_month = {} + (1..31).to_a.each { |day| @commits_per_month[day] = 0 } + + @commits.each do |commit| + hour = commit.committed_date.strftime('%k').to_i + day_of_month = commit.committed_date.strftime('%e').to_i + weekday = commit.committed_date.strftime('%A') + + @commits_per_week_days[weekday] ||= 0 + @commits_per_week_days[weekday] += 1 + @commits_per_time[hour] ||= 0 + @commits_per_time[hour] += 1 + @commits_per_month[day_of_month] ||= 0 + @commits_per_month[day_of_month] += 1 + end + end + end + end +end