0001-Optimize-committers-users-map-retrieval-for-statisti.patch

Jean-Baptiste Barth, 2014-08-27 19:23

Download (2.48 KB)

View differences:

app/models/repository.rb
414 414
  # Notes:
415 415
  # - this hash honnors the users mapping defined for the repository
416 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}
417
    commits = Changeset.where("repository_id = ?", id)
418
                       .select("committer, user_id, count(*) as count")
419
                       .group("committer, user_id")
420

  
421
    #TODO: restore ordering ; this line probably never worked
422
    #commits.to_a.sort! {|x, y| x.last <=> y.last}
423

  
424
    changes = Change.joins(:changeset)
425
                    .where("#{Changeset.table_name}.repository_id = ?", id)
426
                    .select("committer, user_id, count(*) as count")
427
                    .group("committer, user_id")
428

  
429
    user_ids = changesets.map(&:user_id).compact.uniq
430
    authors_names = User.where(:id => user_ids).inject({}) do |memo, user|
431
      memo[user.id] = user.to_s
432
      memo
433
    end
422 434

  
423
    commits_by_author.inject({}) do |hash, (name, commits_count)|
424
      mapped_name = (find_committer_user(name) || name).to_s
435
    (commits + changes).inject({}) do |hash, element|
436
      mapped_name = element.committer
437
      if username = authors_names[element.user_id.to_i]
438
        mapped_name = username
439
      end
425 440
      hash[mapped_name] ||= { :commits_count => 0, :changes_count => 0 }
426
      hash[mapped_name][:commits_count] += commits_count
427
      hash[mapped_name][:changes_count] += h[name] || 0
441
      if element.is_a?(Changeset)
442
        hash[mapped_name][:commits_count] += element.count.to_i
443
      else
444
        hash[mapped_name][:changes_count] += element.count.to_i
445
      end
428 446
      hash
429 447
    end
430 448
  end
431
-