Refactor commits graph

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
This commit is contained in:
Dmitriy Zaporozhets 2014-09-29 12:05:17 +03:00
parent 6d0ddf45ba
commit 3a8cd1fa59
No known key found for this signature in database
GPG key ID: 161B5D6A44D3D88A
3 changed files with 58 additions and 32 deletions

View file

@ -15,33 +15,10 @@ class Projects::GraphsController < Projects::ApplicationController
def commits def commits
@commits = @project.repository.commits(nil, nil, 2000, 0, true) @commits = @project.repository.commits(nil, nil, 2000, 0, true)
@start_date = @commits.last.committed_date.to_date @commits_graph = Gitlab::Graphs::Commits.new(@commits)
@end_date = @commits.first.committed_date.to_date @commits_per_week_days = @commits_graph.commits_per_week_days
@duration = (@end_date - @start_date).to_i @commits_per_time = @commits_graph.commits_per_time
@authors = @commits.map(&:author_email).uniq.size @commits_per_month = @commits_graph.commits_per_month
@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
end end
private private

View file

@ -3,26 +3,26 @@
%p.lead %p.lead
Commits statistic for Commits statistic for
%strong #{@repository.root_ref} %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 .row
.col-md-6 .col-md-6
%ul %ul
%li %li
%p.lead %p.lead
%strong #{@commits.size} %strong #{@commits_graph.commits.size}
commits during commits during
%strong #{@duration} %strong #{@commits_graph.duration}
days days
%li %li
%p.lead %p.lead
Average Average
%strong #{@commit_per_day} %strong #{@commits_graph.commit_per_day}
commits per day commits per day
%li %li
%p.lead %p.lead
Contributed by Contributed by
%strong #{@authors} %strong #{@commits_graph.authors}
authors authors
.col-md-6 .col-md-6
%div %div

View file

@ -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