summaryrefslogtreecommitdiffstats
path: root/app/models/repository.rb
diff options
context:
space:
mode:
authorJean-Baptiste Barth <jeanbaptiste.barth@gmail.com>2014-08-31 17:23:48 +0000
committerJean-Baptiste Barth <jeanbaptiste.barth@gmail.com>2014-08-31 17:23:48 +0000
commitad9a35c6b917d6a96d1ca4312e2a10923c261e5b (patch)
tree23fda6942726d455218f2818ac3de9bc88a06ff2 /app/models/repository.rb
parent1c203312ae73814f3d5d1019cf633467eebd3274 (diff)
downloadredmine-ad9a35c6b917d6a96d1ca4312e2a10923c261e5b.tar.gz
redmine-ad9a35c6b917d6a96d1ca4312e2a10923c261e5b.zip
Optimize committers/users map retrieval for statistic graphs (#13487).
git-svn-id: http://svn.redmine.org/redmine/trunk@13361 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models/repository.rb')
-rw-r--r--app/models/repository.rb36
1 files changed, 27 insertions, 9 deletions
diff --git a/app/models/repository.rb b/app/models/repository.rb
index 55cdb6d88..9f50c6358 100644
--- a/app/models/repository.rb
+++ b/app/models/repository.rb
@@ -414,17 +414,35 @@ class Repository < ActiveRecord::Base
# Notes:
# - this hash honnors the users mapping defined for the repository
def stats_by_author
- commits_by_author = Changeset.where("repository_id = ?", id).group(:committer).count
- commits_by_author.to_a.sort! {|x, y| x.last <=> y.last}
-
- changes_by_author = Change.joins(:changeset).where("#{Changeset.table_name}.repository_id = ?", id).group(:committer).count
- h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
+ commits = Changeset.where("repository_id = ?", id)
+ .select("committer, user_id, count(*) as count")
+ .group("committer, user_id")
+
+ #TODO: restore ordering ; this line probably never worked
+ #commits.to_a.sort! {|x, y| x.last <=> y.last}
+
+ changes = Change.joins(:changeset)
+ .where("#{Changeset.table_name}.repository_id = ?", id)
+ .select("committer, user_id, count(*) as count")
+ .group("committer, user_id")
+
+ user_ids = changesets.map(&:user_id).compact.uniq
+ authors_names = User.where(:id => user_ids).inject({}) do |memo, user|
+ memo[user.id] = user.to_s
+ memo
+ end
- commits_by_author.inject({}) do |hash, (name, commits_count)|
- mapped_name = (find_committer_user(name) || name).to_s
+ (commits + changes).inject({}) do |hash, element|
+ mapped_name = element.committer
+ if username = authors_names[element.user_id.to_i]
+ mapped_name = username
+ end
hash[mapped_name] ||= { :commits_count => 0, :changes_count => 0 }
- hash[mapped_name][:commits_count] += commits_count
- hash[mapped_name][:changes_count] += h[name] || 0
+ if element.is_a?(Changeset)
+ hash[mapped_name][:commits_count] += element.count.to_i
+ else
+ hash[mapped_name][:changes_count] += element.count.to_i
+ end
hash
end
end