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.
56 lines
1.7 KiB
Ruby
56 lines
1.7 KiB
Ruby
require 'spec_helper'
|
|
|
|
describe TrendingProject do
|
|
let(:user) { create(:user) }
|
|
let(:public_project1) { create(:empty_project, :public) }
|
|
let(:public_project2) { create(:empty_project, :public) }
|
|
let(:public_project3) { create(:empty_project, :public) }
|
|
let(:private_project) { create(:empty_project, :private) }
|
|
let(:internal_project) { create(:empty_project, :internal) }
|
|
|
|
before do
|
|
3.times do
|
|
create(:note_on_commit, project: public_project1)
|
|
end
|
|
|
|
2.times do
|
|
create(:note_on_commit, project: public_project2)
|
|
end
|
|
|
|
create(:note_on_commit, project: public_project3, created_at: 5.weeks.ago)
|
|
create(:note_on_commit, project: private_project)
|
|
create(:note_on_commit, project: internal_project)
|
|
end
|
|
|
|
describe '.refresh!' do
|
|
before do
|
|
described_class.refresh!
|
|
end
|
|
|
|
it 'populates the trending projects table' do
|
|
expect(described_class.count).to eq(2)
|
|
end
|
|
|
|
it 'removes existing rows before populating the table' do
|
|
described_class.refresh!
|
|
|
|
expect(described_class.count).to eq(2)
|
|
end
|
|
|
|
it 'stores the project IDs for every trending project' do
|
|
rows = described_class.order(id: :asc).all
|
|
|
|
expect(rows[0].project_id).to eq(public_project1.id)
|
|
expect(rows[1].project_id).to eq(public_project2.id)
|
|
end
|
|
|
|
it 'does not store projects that fall out of the trending time range' do
|
|
expect(described_class.where(project_id: public_project3).any?).to eq(false)
|
|
end
|
|
|
|
it 'stores only public projects' do
|
|
expect(described_class.where(project_id: [public_project1.id, public_project2.id]).count).to eq(2)
|
|
expect(described_class.where(project_id: [private_project.id, internal_project.id]).count).to eq(0)
|
|
end
|
|
end
|
|
end
|