diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2015-10-08 17:22:43 +0300 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2015-10-08 17:22:43 +0300 |
commit | 69bcef32e12cca8a4a31c3035509d479a712b504 (patch) | |
tree | 7ff4b1e444ba1ba470f5f2da29f168b3e487c672 /app/models | |
parent | a2374758e34475ebdfacbe68b6c5b095b66262ec (diff) | |
parent | 4841e883020146fecd386caff7f31f2d9cf307b6 (diff) |
Merge remote-tracking branch 'public/trending-projects-performance'
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/project.rb | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/app/models/project.rb b/app/models/project.rb index bb47b9abb03..4661522b8a0 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -260,6 +260,20 @@ class Project < ActiveRecord::Base name_pattern = Gitlab::Regex::NAMESPACE_REGEX_STR %r{(?<project>#{name_pattern}/#{name_pattern})} end + + def trending(since = 1.month.ago) + # By counting in the JOIN we don't expose the GROUP BY to the outer query. + # This means that calls such as "any?" and "count" just return a number of + # the total count, instead of the counts grouped per project as a Hash. + join_body = "INNER JOIN ( + SELECT project_id, COUNT(*) AS amount + FROM notes + WHERE created_at >= #{sanitize(since)} + GROUP BY project_id + ) join_note_counts ON projects.id = join_note_counts.project_id" + + joins(join_body).reorder('join_note_counts.amount DESC') + end end def team |