Commit 70534bc5 authored by Terri Chu's avatar Terri Chu Committed by Douglas Barbosa Alexandre

RUN AS-IF-FOSS Fix group search users scope times out

parent 95a8b847
---
title: Improve group search users scope performance
merge_request: 38701
author:
type: performance
...@@ -12,20 +12,24 @@ module Gitlab ...@@ -12,20 +12,24 @@ module Gitlab
# rubocop:disable CodeReuse/ActiveRecord # rubocop:disable CodeReuse/ActiveRecord
def users def users
# 1: get all groups the current user has access to # get all groups the current user has access to
groups = GroupsFinder.new(current_user).execute.joins(:users) # ignore order inherited from GroupsFinder to improve performance
current_user_groups = GroupsFinder.new(current_user).execute.unscope(:order)
# 2: Get the group's whole hierarchy # the hierarchy of the current group
group_users = @group.direct_and_indirect_users group_groups = @group.self_and_hierarchy.unscope(:order)
# 3: get all users the current user has access to (-> # the groups where the above hierarchies intersect
# `SearchResults#users`), which also applies the query. intersect_groups = group_groups.where(id: current_user_groups)
# members of @group hierarchy where the user has access to the groups
members = GroupMember.where(group: intersect_groups).non_invite
# get all users the current user has access to (-> `SearchResults#users`), which also applies the query
users = super users = super
# 4: filter for users that belong to the previously selected groups # filter users that belong to the previously selected groups
users users.where(id: members.select(:user_id))
.where(id: group_users.select('id'))
.where(id: groups.select('members.user_id'))
end end
# rubocop:enable CodeReuse/ActiveRecord # rubocop:enable CodeReuse/ActiveRecord
......
...@@ -3,8 +3,10 @@ ...@@ -3,8 +3,10 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Gitlab::GroupSearchResults do RSpec.describe Gitlab::GroupSearchResults do
# group creation calls GroupFinder, so need to create the group
# before so expect(GroupsFinder) check works
let_it_be(:group) { create(:group) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:group) { create(:group) }
subject(:results) { described_class.new(user, 'gob', anything, group: group) } subject(:results) { described_class.new(user, 'gob', anything, group: group) }
...@@ -60,6 +62,19 @@ RSpec.describe Gitlab::GroupSearchResults do ...@@ -60,6 +62,19 @@ RSpec.describe Gitlab::GroupSearchResults do
is_expected.to be_empty is_expected.to be_empty
end end
it 'does not return the user invited to the group' do
user = create(:user, username: 'gob_bluth')
create(:group_member, :invited, :developer, user: user, group: group)
is_expected.to be_empty
end
it 'calls GroupFinder during execution' do
expect(GroupsFinder).to receive(:new).with(user).and_call_original
subject
end
end end
describe "#issuable_params" do describe "#issuable_params" 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