forked from redmine-git-hosting/redmine_git_hosting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository_patch.rb
267 lines (210 loc) · 9.85 KB
/
repository_patch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
require_dependency 'repository'
module RedmineGitHosting
module Patches
module RepositoryPatch
def self.included(base)
base.send(:extend, ClassMethods)
base.send(:include, InstanceMethods)
base.class_eval do
unloadable
class << self
alias_method_chain :factory, :git_hosting
end
end
end
module ClassMethods
def factory_with_git_hosting(klass_name, *args)
new_repo = factory_without_git_hosting(klass_name, *args)
if new_repo.is_a?(Repository::Git)
if new_repo.extra.nil?
# Note that this autoinitializes default values and hook key
RedmineGitolite::GitHosting.logger.error { "Automatic initialization of RepositoryGitExtra failed for #{self.project.to_s}" }
end
end
return new_repo
end
end
module InstanceMethods
def global_statistics
total_commits = Changeset.where("repository_id = ?", self.id).count
first_commit = Changeset.where("repository_id = ?", self.id).order('commit_date ASC').first
last_commit = Changeset.where("repository_id = ?", self.id).order('commit_date ASC').last
active_for = (last_commit.commit_date - first_commit.commit_date).to_i
committers = Changeset.where("repository_id = ?", self.id).where("user_id IS NOT NULL").select(:user_id).uniq.count
committers += Changeset.where("repository_id = ?", self.id).where(user_id: nil).select(:committer).uniq.count
data = {}
data[l(:label_total_commits)] = total_commits
data[l(:label_total_contributors)] = committers
data[l(:label_first_commit_date)] = first_commit.commit_date
data[l(:label_latest_commit_date)] = last_commit.commit_date
data[l(:label_active_for)] = "#{active_for} #{l(:label_active_days)}"
data[l(:label_average_commit_per_day)] = total_commits.fdiv(active_for).round(2)
data[l(:label_average_contributor_commits)] = total_commits.fdiv(committers).round(2)
return data
end
def commits_per_hours
total_commits_by_hour = Changeset.where("repository_id = ?", self.id).map(&:committed_on)
commits_by_hour = [0] * 24
total_commits_by_hour.each {|c| commits_by_hour[get_hour_from_date(c)] += 1 }
hours = (0..23).step(1).to_a
new_hours = []
hours.each { |h| new_hours.push("#{h}h") }
data = {}
data[:categories] = new_hours
data[:series] = []
data[:series].push({:name => l(:label_commit_plural), :data => commits_by_hour})
return data
end
def commits_per_day
total_commits_by_day = Changeset.where("repository_id = ?", self.id).group(:commit_date).order(:commit_date).count
total_changes_by_day = Change.joins(:changeset).where("#{Changeset.table_name}.repository_id = ?", self.id).group(:commit_date).order(:commit_date).count
data = {}
data[:categories] = total_commits_by_day.keys
data[:series] = []
data[:series].push({:name => l(:label_commit_plural), :data => total_commits_by_day.values})
data[:series].push({:name => l(:label_change_plural), :data => total_changes_by_day.values})
return data
end
def commits_per_weekday
week_day = {}
week_day[l(:label_monday)] = 0
week_day[l(:label_tuesday)] = 0
week_day[l(:label_wednesday)] = 0
week_day[l(:label_thursday)] = 0
week_day[l(:label_friday)] = 0
week_day[l(:label_saturday)] = 0
week_day[l(:label_sunday)] = 0
total_commits = Changeset.where("repository_id = ?", self.id).group(:commit_date).count
total_commits.each do |commit_date, commit_count|
case commit_date.to_date.wday
when 0
week_day[l(:label_sunday)] += commit_count
when 1
week_day[l(:label_monday)] += commit_count
when 2
week_day[l(:label_tuesday)] += commit_count
when 3
week_day[l(:label_wednesday)] += commit_count
when 4
week_day[l(:label_thursday)] += commit_count
when 5
week_day[l(:label_friday)] += commit_count
when 6
week_day[l(:label_saturday)] += commit_count
end
end
data = {}
data[:name] = l(:label_commit_plural)
data[:data] = []
week_day.each do |key, value|
data[:data].push([key, value])
end
return [data]
end
def commits_per_month
date_to = Date.today
commits_by_day = Changeset.
where("repository_id = ?", self.id).
group(:commit_date).
count
commits_by_month = [0] * 12
commits_by_day.each {|c| commits_by_month[(date_to.month - c.first.to_date.month) % 12] += c.last }
changes_by_day = Change.
joins(:changeset).
where("#{Changeset.table_name}.repository_id = ?", self.id).
group(:commit_date).
count
changes_by_month = [0] * 12
changes_by_day.each {|c| changes_by_month[(date_to.month - c.first.to_date.month) % 12] += c.last }
fields = []
12.times {|m| fields << month_name(((Date.today.month - 1 - m) % 12) + 1)}
data = {}
data[:categories] = fields.reverse
data[:series] = []
data[:series].push({:name => l(:label_commit_plural), :data => commits_by_month[0..11].reverse})
data[:series].push({:name => l(:label_change_plural), :data => changes_by_month[0..11].reverse})
return data
end
def commits_per_author_with_aliases
commits_by_author = Changeset.where("repository_id = ?", self.id).group(:committer).count
changes_by_author = Change.joins(:changeset).where("#{Changeset.table_name}.repository_id = ?", self.id).group(:committer).count
# generate mappings from the registered users to the comitters
# user_committer_mapping = { name => [comitter, ...] }
# registered_committers = [ committer,... ]
registered_committers = []
user_committer_mapping = {}
Changeset.where(repository_id: self.id).where("user_id IS NOT NULL").group(:committer).includes(:user).each do |x|
name = "#{x.user.firstname} #{x.user.lastname}"
registered_committers << x.committer
user_committer_mapping[[name,x.user.mail]] ||= []
user_committer_mapping[[name,x.user.mail]] << x.committer
end
merged = []
commits_by_author.each do |committer, count|
# skip all registered users
next if registered_committers.include?(committer)
name = committer.gsub(%r{<.+@.+>}, '').strip
mail = committer[/<(.+@.+)>/,1]
merged << { name: name, mail: mail, commits: count, changes: changes_by_author[committer] || 0, committers: [committer] }
end
user_committer_mapping.each do |identity, committers|
count = 0
changes = 0
committers.each do |c|
count += commits_by_author[c] || 0
changes += changes_by_author[c] || 0
end
merged << { name: identity[0], mail: identity[1], commits: count, changes: changes, committers: committers }
end
# sort by name
merged.sort! {|x, y| x[:name] <=> y[:name]}
# merged = merged + [{name:"",commits:0,changes:0}]*(10 - merged.length) if merged.length < 10
return merged
end
def commits_per_author_global
merged = commits_per_author_with_aliases
data = {}
data[:categories] = merged.map { |x| x[:name] }
data[:series] = []
data[:series].push({:name => l(:label_commit_plural), :data => merged.map { |x| x[:commits] }})
data[:series].push({:name => l(:label_change_plural), :data => merged.map { |x| x[:changes] }})
return data
end
def commits_per_author
data = []
committers = commits_per_author_with_aliases
# sort by commits (descending)
committers.sort! {|x, y| y[:commits] <=> x[:commits]}
committers.each do |committer_hash|
commits = {}
committer_hash[:committers].each do |committer|
c = Changeset.where("repository_id = ? AND committer = ?", self.id, committer).group(:commit_date).order(:commit_date).count
commits = commits.merge(c){|key, oldval, newval| newval + oldval}
end
commits = Hash[commits.sort]
commits_data = {}
commits_data[:author_name] = committer_hash[:name]
commits_data[:author_mail] = committer_hash[:mail]
commits_data[:total_commits] = committer_hash[:commits]
commits_data[:categories] = commits.keys
commits_data[:series] = []
commits_data[:series].push({:name => l(:label_commit_plural), :data => commits.values})
data.push(commits_data)
end
return data
end
private
def get_hour_from_date(date)
return nil unless date
time = date.to_time
zone = User.current.time_zone
local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time)
local.hour
end
end
end
end
end
unless Repository.included_modules.include?(RedmineGitHosting::Patches::RepositoryPatch)
Repository.send(:include, RedmineGitHosting::Patches::RepositoryPatch)
end