Class: Gitlab::Graphs::Commits

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/graphs/commits.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commits) ⇒ Commits

Returns a new instance of Commits


7
8
9
10
11
12
13
14
# File 'lib/gitlab/graphs/commits.rb', line 7

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

Instance Attribute Details

#commitsObject (readonly)

Returns the value of attribute commits


4
5
6
# File 'lib/gitlab/graphs/commits.rb', line 4

def commits
  @commits
end

#commits_per_monthObject (readonly)

Returns the value of attribute commits_per_month


4
5
6
# File 'lib/gitlab/graphs/commits.rb', line 4

def commits_per_month
  @commits_per_month
end

#commits_per_timeObject (readonly)

Returns the value of attribute commits_per_time


4
5
6
# File 'lib/gitlab/graphs/commits.rb', line 4

def commits_per_time
  @commits_per_time
end

#commits_per_week_daysObject (readonly)

Returns the value of attribute commits_per_week_days


4
5
6
# File 'lib/gitlab/graphs/commits.rb', line 4

def commits_per_week_days
  @commits_per_week_days
end

#durationObject (readonly)

Returns the value of attribute duration


4
5
6
# File 'lib/gitlab/graphs/commits.rb', line 4

def duration
  @duration
end

#end_dateObject (readonly)

Returns the value of attribute end_date


4
5
6
# File 'lib/gitlab/graphs/commits.rb', line 4

def end_date
  @end_date
end

#start_dateObject (readonly)

Returns the value of attribute start_date


4
5
6
# File 'lib/gitlab/graphs/commits.rb', line 4

def start_date
  @start_date
end

Instance Method Details

#authorsObject


16
17
18
# File 'lib/gitlab/graphs/commits.rb', line 16

def authors
  @authors ||= @commits.map(&:author_email).uniq.size
end

#collect_dataObject


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gitlab/graphs/commits.rb', line 24

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

#commit_per_dayObject


20
21
22
# File 'lib/gitlab/graphs/commits.rb', line 20

def commit_per_day
  @commit_per_day ||= (@commits.size.to_f / @duration).round(1)
end