Commit dcf1037c authored by Peter Leitzen's avatar Peter Leitzen

Merge branch 'projects/order-by-updated_at2' into 'master'

Project list: order by real last update

See merge request gitlab-org/gitlab!82488
parents c7c2d93c df508685
...@@ -11,6 +11,6 @@ class Dashboard::ApplicationController < ApplicationController ...@@ -11,6 +11,6 @@ class Dashboard::ApplicationController < ApplicationController
private private
def projects def projects
@projects ||= current_user.authorized_projects.sorted_by_activity.non_archived @projects ||= current_user.authorized_projects.sorted_by_updated_desc.non_archived
end end
end end
...@@ -546,8 +546,8 @@ class Project < ApplicationRecord ...@@ -546,8 +546,8 @@ class Project < ApplicationRecord
.or(arel_table[:storage_version].eq(nil))) .or(arel_table[:storage_version].eq(nil)))
end end
# last_activity_at is throttled every minute, but last_repository_updated_at is updated with every push scope :sorted_by_updated_asc, -> { reorder(self.arel_table['updated_at'].asc) }
scope :sorted_by_activity, -> { reorder(Arel.sql("GREATEST(COALESCE(last_activity_at, '1970-01-01'), COALESCE(last_repository_updated_at, '1970-01-01')) DESC")) } scope :sorted_by_updated_desc, -> { reorder(self.arel_table['updated_at'].desc) }
scope :sorted_by_stars_desc, -> { reorder(self.arel_table['star_count'].desc) } scope :sorted_by_stars_desc, -> { reorder(self.arel_table['star_count'].desc) }
scope :sorted_by_stars_asc, -> { reorder(self.arel_table['star_count'].asc) } scope :sorted_by_stars_asc, -> { reorder(self.arel_table['star_count'].asc) }
# Sometimes queries (e.g. using CTEs) require explicit disambiguation with table name # Sometimes queries (e.g. using CTEs) require explicit disambiguation with table name
...@@ -783,9 +783,9 @@ class Project < ApplicationRecord ...@@ -783,9 +783,9 @@ class Project < ApplicationRecord
# pass a string to avoid AR adding the table name # pass a string to avoid AR adding the table name
reorder('project_statistics.storage_size DESC, projects.id DESC') reorder('project_statistics.storage_size DESC, projects.id DESC')
when 'latest_activity_desc' when 'latest_activity_desc'
reorder(self.arel_table['last_activity_at'].desc) sorted_by_updated_desc
when 'latest_activity_asc' when 'latest_activity_asc'
reorder(self.arel_table['last_activity_at'].asc) sorted_by_updated_asc
when 'stars_desc' when 'stars_desc'
sorted_by_stars_desc sorted_by_stars_desc
when 'stars_asc' when 'stars_asc'
...@@ -1404,7 +1404,7 @@ class Project < ApplicationRecord ...@@ -1404,7 +1404,7 @@ class Project < ApplicationRecord
end end
def last_activity_date def last_activity_date
[last_activity_at, last_repository_updated_at, updated_at].compact.max updated_at
end end
def project_id def project_id
......
...@@ -1191,29 +1191,8 @@ RSpec.describe Project, factory_default: :keep do ...@@ -1191,29 +1191,8 @@ RSpec.describe Project, factory_default: :keep do
end end
describe 'last_activity_date' do describe 'last_activity_date' do
it 'returns the creation date of the project\'s last event if present' do it 'returns the project\'s last update date' do
new_event = create(:event, :closed, project: project, created_at: Time.current) expect(project.last_activity_date).to be_like_time(project.updated_at)
project.reload
expect(project.last_activity_at.to_i).to eq(new_event.created_at.to_i)
end
it 'returns the project\'s last update date if it has no events' do
expect(project.last_activity_date).to eq(project.updated_at)
end
it 'returns the most recent timestamp' do
project.update!(updated_at: nil,
last_activity_at: timestamp,
last_repository_updated_at: timestamp - 1.hour)
expect(project.last_activity_date).to be_like_time(timestamp)
project.update!(updated_at: timestamp,
last_activity_at: timestamp - 1.hour,
last_repository_updated_at: nil)
expect(project.last_activity_date).to be_like_time(timestamp)
end end
end end
end end
...@@ -1690,15 +1669,27 @@ RSpec.describe Project, factory_default: :keep do ...@@ -1690,15 +1669,27 @@ RSpec.describe Project, factory_default: :keep do
end end
describe '.sort_by_attribute' do describe '.sort_by_attribute' do
it 'reorders the input relation by start count desc' do let_it_be(:project1) { create(:project, star_count: 2, updated_at: 1.minute.ago) }
project1 = create(:project, star_count: 2) let_it_be(:project2) { create(:project, star_count: 1) }
project2 = create(:project, star_count: 1) let_it_be(:project3) { create(:project, updated_at: 2.minutes.ago) }
project3 = create(:project)
it 'reorders the input relation by start count desc' do
projects = described_class.sort_by_attribute(:stars_desc) projects = described_class.sort_by_attribute(:stars_desc)
expect(projects).to eq([project1, project2, project3]) expect(projects).to eq([project1, project2, project3])
end end
it 'reorders the input relation by last activity desc' do
projects = described_class.sort_by_attribute(:latest_activity_desc)
expect(projects).to eq([project2, project1, project3])
end
it 'reorders the input relation by last activity asc' do
projects = described_class.sort_by_attribute(:latest_activity_asc)
expect(projects).to eq([project3, project1, project2])
end
end end
describe '.with_shared_runners' do describe '.with_shared_runners' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment