237c8f66e6
This commit introduces a Sidekiq worker that precalculates the list of trending projects on a daily basis. The resulting set is stored in a database table that is then queried by Project.trending. This setup means that Unicorn workers no longer _may_ have to calculate the list of trending projects. Furthermore it supports filtering without any complex caching mechanisms. The data in the "trending_projects" table is inserted in the same order as the project ranking. This means that getting the projects in the correct order is simply a matter of: SELECT projects.* FROM projects INNER JOIN trending_projects ON trending_projects.project_id = projects.id ORDER BY trending_projects.id ASC; Such a query will only take a few milliseconds at most (as measured on GitLab.com), opposed to a few seconds for the query used for calculating the project ranks. The migration in this commit does not require downtime and takes care of populating an initial list of trending projects.
35 lines
1.1 KiB
Ruby
35 lines
1.1 KiB
Ruby
class TrendingProject < ActiveRecord::Base
|
|
belongs_to :project
|
|
|
|
# The number of months to include in the trending calculation.
|
|
MONTHS_TO_INCLUDE = 1
|
|
|
|
# The maximum number of projects to include in the trending set.
|
|
PROJECTS_LIMIT = 100
|
|
|
|
# Populates the trending projects table with the current list of trending
|
|
# projects.
|
|
def self.refresh!
|
|
# The calculation **must** run in a transaction. If the removal of data and
|
|
# insertion of new data were to run separately a user might end up with an
|
|
# empty list of trending projects for a short period of time.
|
|
transaction do
|
|
delete_all
|
|
|
|
timestamp = connection.quote(MONTHS_TO_INCLUDE.months.ago)
|
|
|
|
connection.execute <<-EOF.strip_heredoc
|
|
INSERT INTO #{table_name} (project_id)
|
|
SELECT project_id
|
|
FROM notes
|
|
INNER JOIN projects ON projects.id = notes.project_id
|
|
WHERE notes.created_at >= #{timestamp}
|
|
AND notes.system IS FALSE
|
|
AND projects.visibility_level = #{Gitlab::VisibilityLevel::PUBLIC}
|
|
GROUP BY project_id
|
|
ORDER BY count(*) DESC
|
|
LIMIT #{PROJECTS_LIMIT};
|
|
EOF
|
|
end
|
|
end
|
|
end
|