##// END OF EJS Templates
Move some RepositoriesController logic to Repository#stats_by_author (#13487)....
Jean-Baptiste Barth -
r12997:41bf39df3655
parent child
Show More
@@ -397,16 +397,16 class RepositoriesController < ApplicationController
397 end
397 end
398
398
399 def graph_commits_per_author(repository)
399 def graph_commits_per_author(repository)
400 commits_by_author = Changeset.where("repository_id = ?", repository.id).group(:committer).count
400 #data
401 commits_by_author.to_a.sort! {|x, y| x.last <=> y.last}
401 stats = repository.stats_by_author
402
402 fields, commits_data, changes_data = [], [], []
403 changes_by_author = Change.joins(:changeset).where("#{Changeset.table_name}.repository_id = ?", repository.id).group(:committer).count
403 stats.each do |name, hsh|
404 h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
404 fields << name
405
405 commits_data << hsh[:commits_count]
406 fields = commits_by_author.collect {|r| r.first}
406 changes_data << hsh[:changes_count]
407 commits_data = commits_by_author.collect {|r| r.last}
407 end
408 changes_data = commits_by_author.collect {|r| h[r.first] || 0}
409
408
409 #expand to 10 values if needed
410 fields = fields + [""]*(10 - fields.length) if fields.length<10
410 fields = fields + [""]*(10 - fields.length) if fields.length<10
411 commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
411 commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
412 changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
412 changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
@@ -414,6 +414,7 class RepositoriesController < ApplicationController
414 # Remove email address in usernames
414 # Remove email address in usernames
415 fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') }
415 fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') }
416
416
417 #prepare graph
417 graph = SVG::Graph::BarHorizontal.new(
418 graph = SVG::Graph::BarHorizontal.new(
418 :height => 30 * commits_data.length,
419 :height => 30 * commits_data.length,
419 :width => 800,
420 :width => 800,
@@ -405,6 +405,29 class Repository < ActiveRecord::Base
405 new_record? && project && Repository.where(:project_id => project.id).empty?
405 new_record? && project && Repository.where(:project_id => project.id).empty?
406 end
406 end
407
407
408 # Returns a hash with statistics by author in the following form:
409 # {
410 # "John Smith" => { :commits => 45, :changes => 324 },
411 # "Bob" => { ... }
412 # }
413 #
414 # Notes:
415 # - this hash honnors the users mapping defined for the repository
416 def stats_by_author
417 commits_by_author = Changeset.where("repository_id = ?", id).group(:committer).count
418 commits_by_author.to_a.sort! {|x, y| x.last <=> y.last}
419
420 changes_by_author = Change.joins(:changeset).where("#{Changeset.table_name}.repository_id = ?", id).group(:committer).count
421 h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
422
423 commits_by_author.inject({}) do |hash, (name, commits_count)|
424 hash[name] = {}
425 hash[name][:commits_count] = commits_count
426 hash[name][:changes_count] = h[name] || 0
427 hash
428 end
429 end
430
408 protected
431 protected
409
432
410 def check_default
433 def check_default
@@ -395,4 +395,23 class RepositoryTest < ActiveSupport::TestCase
395 [r1, r2].sort
395 [r1, r2].sort
396 end
396 end
397 end
397 end
398
399 def test_stats_by_author_reflect_changesets_and_changes
400 repository = Repository.find(10)
401
402 expected = {"dlopper"=>{:commits_count=>10, :changes_count=>3}}
403 assert_equal expected, repository.stats_by_author
404
405 set = Changeset.create!(
406 :repository => repository,
407 :committer => 'dlopper',
408 :committed_on => Time.now,
409 :revision => 101,
410 :comments => 'Another commit by foo.'
411 )
412 Change.create!(:changeset => set, :action => 'create', :path => '/path/to/file1')
413 Change.create!(:changeset => set, :action => 'create', :path => '/path/to/file2')
414 expected = {"dlopper"=>{:commits_count=>11, :changes_count=>5}}
415 assert_equal expected, repository.stats_by_author
416 end
398 end
417 end
General Comments 0
You need to be logged in to leave comments. Login now