@@ -38,7 +38,8 @@ def global_statistics
38
38
first_commit = Changeset . where ( "repository_id = ?" , self . id ) . order ( 'commit_date ASC' ) . first
39
39
last_commit = Changeset . where ( "repository_id = ?" , self . id ) . order ( 'commit_date ASC' ) . last
40
40
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
42
43
43
44
data = { }
44
45
data [ l ( :label_total_commits ) ] = total_commits
@@ -129,23 +130,21 @@ def commits_per_weekday
129
130
130
131
131
132
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
135
134
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 ) .
137
136
group ( :commit_date ) .
138
137
count
139
138
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 }
141
140
142
141
changes_by_day = Change .
143
142
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 ) .
145
144
group ( :commit_date ) .
146
145
count
147
146
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 }
149
148
150
149
fields = [ ]
151
150
12 . times { |m | fields << month_name ( ( ( Date . today . month - 1 - m ) % 12 ) + 1 ) }
@@ -159,50 +158,81 @@ def commits_per_month
159
158
return data
160
159
end
161
160
162
-
163
- def commits_per_author_global
161
+ def commits_per_author_with_aliases
164
162
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
-
167
163
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 }
169
164
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
173
176
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 )
177
181
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
180
205
181
206
data = { }
182
- data [ :categories ] = fields
207
+ data [ :categories ] = merged . map { | x | x [ :name ] }
183
208
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 ] } } )
186
211
187
212
return data
188
213
end
189
214
190
215
191
216
def commits_per_author
192
217
data = [ ]
218
+ committers = commits_per_author_with_aliases
193
219
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 ] }
195
222
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 = { }
198
225
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
201
230
231
+ commits = Hash [ commits . sort ]
202
232
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 ]
206
236
commits_data [ :categories ] = commits . keys
207
237
commits_data [ :series ] = [ ]
208
238
commits_data [ :series ] . push ( { :name => l ( :label_commit_plural ) , :data => commits . values } )
0 commit comments