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.



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

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.



6
7
8
# File 'lib/gitlab/graphs/commits.rb', line 6

def commits
  @commits
end

#commits_per_monthObject (readonly)

Returns the value of attribute commits_per_month.



6
7
8
# File 'lib/gitlab/graphs/commits.rb', line 6

def commits_per_month
  @commits_per_month
end

#commits_per_timeObject (readonly)

Returns the value of attribute commits_per_time.



6
7
8
# File 'lib/gitlab/graphs/commits.rb', line 6

def commits_per_time
  @commits_per_time
end

#commits_per_week_daysObject (readonly)

Returns the value of attribute commits_per_week_days.



6
7
8
# File 'lib/gitlab/graphs/commits.rb', line 6

def commits_per_week_days
  @commits_per_week_days
end

#durationObject (readonly)

Returns the value of attribute duration.



6
7
8
# File 'lib/gitlab/graphs/commits.rb', line 6

def duration
  @duration
end

#end_dateObject (readonly)

Returns the value of attribute end_date.



6
7
8
# File 'lib/gitlab/graphs/commits.rb', line 6

def end_date
  @end_date
end

#start_dateObject (readonly)

Returns the value of attribute start_date.



6
7
8
# File 'lib/gitlab/graphs/commits.rb', line 6

def start_date
  @start_date
end

Instance Method Details

#authorsObject



18
19
20
# File 'lib/gitlab/graphs/commits.rb', line 18

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

#collect_dataObject



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

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



22
23
24
# File 'lib/gitlab/graphs/commits.rb', line 22

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