diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2015-10-08 16:22:43 +0200 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2015-10-08 16:22:43 +0200 |
commit | 69bcef32e12cca8a4a31c3035509d479a712b504 (patch) | |
tree | 7ff4b1e444ba1ba470f5f2da29f168b3e487c672 /app | |
parent | a2374758e34475ebdfacbe68b6c5b095b66262ec (diff) | |
parent | 4841e883020146fecd386caff7f31f2d9cf307b6 (diff) | |
download | gitlab-ce-69bcef32e12cca8a4a31c3035509d479a712b504.tar.gz |
Merge remote-tracking branch 'public/trending-projects-performance'
Diffstat (limited to 'app')
-rw-r--r-- | app/finders/trending_projects_finder.rb | 11 | ||||
-rw-r--r-- | app/models/project.rb | 14 |
2 files changed, 16 insertions, 9 deletions
diff --git a/app/finders/trending_projects_finder.rb b/app/finders/trending_projects_finder.rb index 9ea342cb26d..81a12403801 100644 --- a/app/finders/trending_projects_finder.rb +++ b/app/finders/trending_projects_finder.rb @@ -1,13 +1,6 @@ class TrendingProjectsFinder - def execute(current_user, start_date = nil) - start_date ||= Date.today - 1.month - - projects = projects_for(current_user) - - # Determine trending projects based on comments count - # for period of time - ex. month - projects.joins(:notes).where('notes.created_at > ?', start_date). - group("projects.id").reorder("count(notes.id) DESC") + def execute(current_user, start_date = 1.month.ago) + projects_for(current_user).trending(start_date) end private 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 |