Commit 36295e07 authored by Corinna Wiesner's avatar Corinna Wiesner

Count total amount of users for each access level

Preparations to fill the users_statistics table with the highest role
counts by counting the total amount of users for each access level
stored in the users_highest_roles table.
parent 5c32fdaf
......@@ -4,4 +4,6 @@ class UserHighestRole < ApplicationRecord
belongs_to :user, optional: false
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
......@@ -11,4 +11,22 @@ class UsersStatistics < ApplicationRecord
:bots,
:blocked
].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
......@@ -13,8 +13,8 @@ module EE
pluck(:user_id)
end
# Get amout of users with highest role they have.
# If John is developer in one project but maintainer in another he will be
# Get amount of users with highest role they have.
# 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
# functionality available to higher roles only.
#
......
......@@ -3,5 +3,13 @@
FactoryBot.define do
factory :user_highest_role do
user
trait :maintainer do
highest_access_level { Gitlab::Access::MAINTAINER }
end
trait :developer do
highest_access_level { Gitlab::Access::DEVELOPER }
end
end
end
......@@ -10,4 +10,20 @@ describe UserHighestRole do
describe 'validations' do
it { is_expected.to validate_inclusion_of(:highest_access_level).in_array([nil, *Gitlab::Access.all_values]) }
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
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