Commit 752166a6 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch 'topics/switch_projects_counter_for_order' into 'master'

Topics: order by `non_private_projects_count`

See merge request gitlab-org/gitlab!80478
parents 9390d79d 99f6dfc8
...@@ -12,7 +12,7 @@ module Projects ...@@ -12,7 +12,7 @@ module Projects
end end
def execute def execute
topics = Projects::Topic.order_by_total_projects_count topics = Projects::Topic.order_by_non_private_projects_count
by_search(topics) by_search(topics)
end end
......
...@@ -10,9 +10,9 @@ module Resolvers ...@@ -10,9 +10,9 @@ module Resolvers
def resolve(**args) def resolve(**args)
if args[:search].present? if args[:search].present?
::Projects::Topic.search(args[:search]).order_by_total_projects_count ::Projects::Topic.search(args[:search]).order_by_non_private_projects_count
else else
::Projects::Topic.order_by_total_projects_count ::Projects::Topic.order_by_non_private_projects_count
end end
end end
end end
......
...@@ -14,12 +14,12 @@ module Projects ...@@ -14,12 +14,12 @@ module Projects
has_many :project_topics, class_name: 'Projects::ProjectTopic' has_many :project_topics, class_name: 'Projects::ProjectTopic'
has_many :projects, through: :project_topics has_many :projects, through: :project_topics
scope :order_by_total_projects_count, -> { order(total_projects_count: :desc).order(id: :asc) } scope :order_by_non_private_projects_count, -> { order(non_private_projects_count: :desc).order(id: :asc) }
scope :reorder_by_similarity, -> (search) do scope :reorder_by_similarity, -> (search) do
order_expression = Gitlab::Database::SimilarityScore.build_expression(search: search, rules: [ order_expression = Gitlab::Database::SimilarityScore.build_expression(search: search, rules: [
{ column: arel_table['name'] } { column: arel_table['name'] }
]) ])
reorder(order_expression.desc, arel_table['total_projects_count'].desc, arel_table['id']) reorder(order_expression.desc, arel_table['non_private_projects_count'].desc, arel_table['id'])
end end
class << self class << self
......
# frozen_string_literal: true
class CleanupPopulateTopicsNonPrivateProjectsCount < Gitlab::Database::Migration[1.0]
MIGRATION = 'PopulateTopicsNonPrivateProjectsCount'
disable_ddl_transaction!
def up
finalize_background_migration(MIGRATION)
end
def down
# no-op
end
end
644d38e401ac8179777cb9d3c5fefca2fb55e0c409197bb2d222f7e96e5dd42f
\ No newline at end of file
...@@ -9,9 +9,9 @@ RSpec.describe Projects::TopicsFinder do ...@@ -9,9 +9,9 @@ RSpec.describe Projects::TopicsFinder do
let!(:topic2) { create(:topic, name: 'topicC') } let!(:topic2) { create(:topic, name: 'topicC') }
let!(:topic3) { create(:topic, name: 'topicA') } let!(:topic3) { create(:topic, name: 'topicA') }
let!(:project1) { create(:project, namespace: user.namespace, topic_list: 'topicC, topicA, topicB') } let!(:project1) { create(:project, :public, namespace: user.namespace, topic_list: 'topicC, topicA, topicB') }
let!(:project2) { create(:project, namespace: user.namespace, topic_list: 'topicC, topicA') } let!(:project2) { create(:project, :public, namespace: user.namespace, topic_list: 'topicC, topicA') }
let!(:project3) { create(:project, namespace: user.namespace, topic_list: 'topicC') } let!(:project3) { create(:project, :public, namespace: user.namespace, topic_list: 'topicC') }
describe '#execute' do describe '#execute' do
it 'returns topics' do it 'returns topics' do
......
...@@ -6,9 +6,9 @@ RSpec.describe Resolvers::TopicsResolver do ...@@ -6,9 +6,9 @@ RSpec.describe Resolvers::TopicsResolver do
include GraphqlHelpers include GraphqlHelpers
describe '#resolve' do describe '#resolve' do
let!(:topic1) { create(:topic, name: 'GitLab', total_projects_count: 1) } let!(:topic1) { create(:topic, name: 'GitLab', non_private_projects_count: 1) }
let!(:topic2) { create(:topic, name: 'git', total_projects_count: 2) } let!(:topic2) { create(:topic, name: 'git', non_private_projects_count: 2) }
let!(:topic3) { create(:topic, name: 'topic3', total_projects_count: 3) } let!(:topic3) { create(:topic, name: 'topic3', non_private_projects_count: 3) }
it 'finds all topics' do it 'finds all topics' do
expect(resolve_topics).to eq([topic3, topic2, topic1]) expect(resolve_topics).to eq([topic3, topic2, topic1])
......
...@@ -28,16 +28,16 @@ RSpec.describe Projects::Topic do ...@@ -28,16 +28,16 @@ RSpec.describe Projects::Topic do
end end
describe 'scopes' do describe 'scopes' do
describe 'order_by_total_projects_count' do describe 'order_by_non_private_projects_count' do
let!(:topic1) { create(:topic, name: 'topicB') } let!(:topic1) { create(:topic, name: 'topicB') }
let!(:topic2) { create(:topic, name: 'topicC') } let!(:topic2) { create(:topic, name: 'topicC') }
let!(:topic3) { create(:topic, name: 'topicA') } let!(:topic3) { create(:topic, name: 'topicA') }
let!(:project1) { create(:project, topic_list: 'topicC, topicA, topicB') } let!(:project1) { create(:project, :public, topic_list: 'topicC, topicA, topicB') }
let!(:project2) { create(:project, topic_list: 'topicC, topicA') } let!(:project2) { create(:project, :public, topic_list: 'topicC, topicA') }
let!(:project3) { create(:project, topic_list: 'topicC') } let!(:project3) { create(:project, :public, topic_list: 'topicC') }
it 'sorts topics by total_projects_count' do it 'sorts topics by non_private_projects_count' do
topics = described_class.order_by_total_projects_count topics = described_class.order_by_non_private_projects_count
expect(topics.map(&:name)).to eq(%w[topicC topicA topicB topic]) expect(topics.map(&:name)).to eq(%w[topicC topicA topicB topic])
end end
......
...@@ -7,9 +7,9 @@ RSpec.describe API::Topics do ...@@ -7,9 +7,9 @@ RSpec.describe API::Topics do
let_it_be(:file) { fixture_file_upload('spec/fixtures/dk.png') } let_it_be(:file) { fixture_file_upload('spec/fixtures/dk.png') }
let_it_be(:topic_1) { create(:topic, name: 'Git', total_projects_count: 1, avatar: file) } let_it_be(:topic_1) { create(:topic, name: 'Git', total_projects_count: 1, non_private_projects_count: 1, avatar: file) }
let_it_be(:topic_2) { create(:topic, name: 'GitLab', total_projects_count: 2) } let_it_be(:topic_2) { create(:topic, name: 'GitLab', total_projects_count: 2, non_private_projects_count: 2) }
let_it_be(:topic_3) { create(:topic, name: 'other-topic', total_projects_count: 3) } let_it_be(:topic_3) { create(:topic, name: 'other-topic', total_projects_count: 3, non_private_projects_count: 3) }
let_it_be(:admin) { create(:user, :admin) } let_it_be(:admin) { create(:user, :admin) }
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
......
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