Commit c35426a2 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch 'highest_role_for_users_count_in_groups_and_projects' into 'master'

Count total amount of users for each access level

See merge request gitlab-org/gitlab!25360
parents 40b8c503 36295e07
...@@ -4,4 +4,6 @@ class UserHighestRole < ApplicationRecord ...@@ -4,4 +4,6 @@ class UserHighestRole < ApplicationRecord
belongs_to :user, optional: false belongs_to :user, optional: false
validates :highest_access_level, allow_nil: true, inclusion: { in: Gitlab::Access.all_values } validates :highest_access_level, allow_nil: true, inclusion: { in: Gitlab::Access.all_values }
scope :with_highest_access_level, -> (highest_access_level) { where(highest_access_level: highest_access_level) }
end end
...@@ -11,4 +11,22 @@ class UsersStatistics < ApplicationRecord ...@@ -11,4 +11,22 @@ class UsersStatistics < ApplicationRecord
:bots, :bots,
:blocked :blocked
].freeze ].freeze
private
def highest_role_stats
return unless Feature.enabled?(:users_statistics)
{
owner: batch_count_for_access_level(Gitlab::Access::OWNER),
maintainer: batch_count_for_access_level(Gitlab::Access::MAINTAINER),
developer: batch_count_for_access_level(Gitlab::Access::DEVELOPER),
reporter: batch_count_for_access_level(Gitlab::Access::REPORTER),
guest: batch_count_for_access_level(Gitlab::Access::GUEST)
}
end
def batch_count_for_access_level(access_level)
Gitlab::Database::BatchCount.batch_count(UserHighestRole.with_highest_access_level(access_level))
end
end end
...@@ -13,8 +13,8 @@ module EE ...@@ -13,8 +13,8 @@ module EE
pluck(:user_id) pluck(:user_id)
end end
# Get amout of users with highest role they have. # Get amount of users with highest role they have.
# If John is developer in one project but maintainer in another he will be # If John is a developer in one project but maintainer in another he will be
# counted once as maintainer. This is needed to count users who don't use # counted once as maintainer. This is needed to count users who don't use
# functionality available to higher roles only. # functionality available to higher roles only.
# #
......
...@@ -3,5 +3,13 @@ ...@@ -3,5 +3,13 @@
FactoryBot.define do FactoryBot.define do
factory :user_highest_role do factory :user_highest_role do
user user
trait :maintainer do
highest_access_level { Gitlab::Access::MAINTAINER }
end
trait :developer do
highest_access_level { Gitlab::Access::DEVELOPER }
end
end end
end end
...@@ -10,4 +10,20 @@ describe UserHighestRole do ...@@ -10,4 +10,20 @@ describe UserHighestRole do
describe 'validations' do describe 'validations' do
it { is_expected.to validate_inclusion_of(:highest_access_level).in_array([nil, *Gitlab::Access.all_values]) } it { is_expected.to validate_inclusion_of(:highest_access_level).in_array([nil, *Gitlab::Access.all_values]) }
end end
describe 'scopes' do
describe '.with_highest_access_level' do
let(:developer_access_level) { Gitlab::Access::DEVELOPER }
let!(:developer) { create(:user_highest_role, :developer) }
let!(:another_developer) { create(:user_highest_role, :developer) }
let!(:maintainer) { create(:user_highest_role, :maintainer) }
it 'only returns entry for developer access level' do
expect(described_class.with_highest_access_level(developer_access_level)).to contain_exactly(
developer,
another_developer
)
end
end
end
end end
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