Skip to content

Commit 56fb274

Browse files
author
root
committed
Backport patch from #266
1 parent c5822a4 commit 56fb274

File tree

1 file changed

+62
-32
lines changed

1 file changed

+62
-32
lines changed

Diff for: lib/redmine_git_hosting/patches/repository_patch.rb

+62-32
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ def global_statistics
3838
first_commit = Changeset.where("repository_id = ?", self.id).order('commit_date ASC').first
3939
last_commit = Changeset.where("repository_id = ?", self.id).order('commit_date ASC').last
4040
active_for = (last_commit.commit_date - first_commit.commit_date).to_i
41-
committers = Changeset.where("repository_id = ?", self.id).map(&:committer).uniq.size
41+
committers = Changeset.where("repository_id = ?", self.id).where("user_id IS NOT NULL").select(:user_id).uniq.count
42+
committers += Changeset.where("repository_id = ?", self.id).where(user_id: nil).select(:committer).uniq.count
4243

4344
data = {}
4445
data[l(:label_total_commits)] = total_commits
@@ -129,23 +130,21 @@ def commits_per_weekday
129130

130131

131132
def commits_per_month
132-
@date_to = Date.today
133-
@date_from = @date_to << 11
134-
@date_from = Date.civil(@date_from.year, @date_from.month, 1)
133+
date_to = Date.today
135134
commits_by_day = Changeset.
136-
where("repository_id = ? AND commit_date BETWEEN ? AND ?", self.id, @date_from, @date_to).
135+
where("repository_id = ?", self.id).
137136
group(:commit_date).
138137
count
139138
commits_by_month = [0] * 12
140-
commits_by_day.each {|c| commits_by_month[(@date_to.month - c.first.to_date.month) % 12] += c.last }
139+
commits_by_day.each {|c| commits_by_month[(date_to.month - c.first.to_date.month) % 12] += c.last }
141140

142141
changes_by_day = Change.
143142
joins(:changeset).
144-
where("#{Changeset.table_name}.repository_id = ? AND #{Changeset.table_name}.commit_date BETWEEN ? AND ?", self.id, @date_from, @date_to).
143+
where("#{Changeset.table_name}.repository_id = ?", self.id).
145144
group(:commit_date).
146145
count
147146
changes_by_month = [0] * 12
148-
changes_by_day.each {|c| changes_by_month[(@date_to.month - c.first.to_date.month) % 12] += c.last }
147+
changes_by_day.each {|c| changes_by_month[(date_to.month - c.first.to_date.month) % 12] += c.last }
149148

150149
fields = []
151150
12.times {|m| fields << month_name(((Date.today.month - 1 - m) % 12) + 1)}
@@ -159,50 +158,81 @@ def commits_per_month
159158
return data
160159
end
161160

162-
163-
def commits_per_author_global
161+
def commits_per_author_with_aliases
164162
commits_by_author = Changeset.where("repository_id = ?", self.id).group(:committer).count
165-
commits_by_author.to_a.sort! {|x, y| x.last <=> y.last}
166-
167163
changes_by_author = Change.joins(:changeset).where("#{Changeset.table_name}.repository_id = ?", self.id).group(:committer).count
168-
h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
169164

170-
fields = commits_by_author.collect {|r| r.first}
171-
commits_data = commits_by_author.collect {|r| r.last}
172-
changes_data = commits_by_author.collect {|r| h[r.first] || 0}
165+
# generate mappings from the registered users to the comitters
166+
# user_committer_mapping = { name => [comitter, ...] }
167+
# registered_committers = [ committer,... ]
168+
registered_committers = []
169+
user_committer_mapping = {}
170+
Changeset.where(repository_id: self.id).where("user_id IS NOT NULL").group(:committer).includes(:user).each do |x|
171+
name = "#{x.user.firstname} #{x.user.lastname}"
172+
registered_committers << x.committer
173+
user_committer_mapping[[name,x.user.mail]] ||= []
174+
user_committer_mapping[[name,x.user.mail]] << x.committer
175+
end
173176

174-
fields = fields + [""]*(10 - fields.length) if fields.length<10
175-
commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
176-
changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
177+
merged = []
178+
commits_by_author.each do |committer, count|
179+
# skip all registered users
180+
next if registered_committers.include?(committer)
177181

178-
# Remove email adress in usernames
179-
fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '').strip }
182+
name = committer.gsub(%r{<.+@.+>}, '').strip
183+
mail = committer[/<(.+@.+)>/,1]
184+
merged << { name: name, mail: mail, commits: count, changes: changes_by_author[committer] || 0, committers: [committer] }
185+
end
186+
user_committer_mapping.each do |identity, committers|
187+
count = 0
188+
changes = 0
189+
committers.each do |c|
190+
count += commits_by_author[c] || 0
191+
changes += changes_by_author[c] || 0
192+
end
193+
merged << { name: identity[0], mail: identity[1], commits: count, changes: changes, committers: committers }
194+
end
195+
196+
# sort by name
197+
merged.sort! {|x, y| x[:name] <=> y[:name]}
198+
199+
# merged = merged + [{name:"",commits:0,changes:0}]*(10 - merged.length) if merged.length < 10
200+
return merged
201+
end
202+
203+
def commits_per_author_global
204+
merged = commits_per_author_with_aliases
180205

181206
data = {}
182-
data[:categories] = fields
207+
data[:categories] = merged.map { |x| x[:name] }
183208
data[:series] = []
184-
data[:series].push({:name => l(:label_commit_plural), :data => commits_data})
185-
data[:series].push({:name => l(:label_change_plural), :data => changes_data})
209+
data[:series].push({:name => l(:label_commit_plural), :data => merged.map { |x| x[:commits] }})
210+
data[:series].push({:name => l(:label_change_plural), :data => merged.map { |x| x[:changes] }})
186211

187212
return data
188213
end
189214

190215

191216
def commits_per_author
192217
data = []
218+
committers = commits_per_author_with_aliases
193219

194-
committers = Changeset.where("repository_id = ?", self.id).map(&:committer).uniq
220+
# sort by commits (descending)
221+
committers.sort! {|x, y| y[:commits] <=> x[:commits]}
195222

196-
committers.each do |committer|
197-
commits = Changeset.where("repository_id = ? AND committer = ?", self.id, committer).group(:commit_date).order(:commit_date).count
223+
committers.each do |committer_hash|
224+
commits = {}
198225

199-
committer_name = committer.split('<')[0].strip
200-
committer_mail = committer.split('<')[1].gsub('>', '') rescue ''
226+
committer_hash[:committers].each do |committer|
227+
c = Changeset.where("repository_id = ? AND committer = ?", self.id, committer).group(:commit_date).order(:commit_date).count
228+
commits = commits.merge(c){|key, oldval, newval| newval + oldval}
229+
end
201230

231+
commits = Hash[commits.sort]
202232
commits_data = {}
203-
commits_data[:author_name] = committer_name
204-
commits_data[:author_mail] = committer_mail
205-
commits_data[:total_commits] = commits.values.map(&:to_i).reduce(:+)
233+
commits_data[:author_name] = committer_hash[:name]
234+
commits_data[:author_mail] = committer_hash[:mail]
235+
commits_data[:total_commits] = committer_hash[:commits]
206236
commits_data[:categories] = commits.keys
207237
commits_data[:series] = []
208238
commits_data[:series].push({:name => l(:label_commit_plural), :data => commits.values})

0 commit comments

Comments
 (0)