diff --git a/app/controllers/projects/graphs_controller.rb b/app/controllers/projects/graphs_controller.rb index d66465d9579..610b4967fea 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 2b72bdfae21..a189a487135 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 00000000000..2122339d2db --- /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