Commit 3028cd68 authored by Nick Thomas's avatar Nick Thomas

Merge branch '39-count-unique-users-for-more-accurate-smau-reporting' into 'master'

Allow UsageData.count to use count_by:

See merge request gitlab-org/gitlab-ce!30770
parents 4d4e88df f53112de
...@@ -15,8 +15,8 @@ class GroupMember < Member ...@@ -15,8 +15,8 @@ class GroupMember < Member
default_scope { where(source_type: SOURCE_TYPE) } default_scope { where(source_type: SOURCE_TYPE) }
scope :of_groups, ->(groups) { where(source_id: groups.select(:id)) } scope :of_groups, ->(groups) { where(source_id: groups.select(:id)) }
scope :count_users_by_group_id, -> { joins(:user).group(:source_id).count } scope :count_users_by_group_id, -> { joins(:user).group(:source_id).count }
scope :of_ldap_type, -> { where(ldap: true) }
after_create :update_two_factor_requirement, unless: :invite? after_create :update_two_factor_requirement, unless: :invite?
after_destroy :update_two_factor_requirement, unless: :invite? after_destroy :update_two_factor_requirement, unless: :invite?
......
...@@ -188,8 +188,8 @@ module Gitlab ...@@ -188,8 +188,8 @@ module Gitlab
{} # augmented in EE {} # augmented in EE
end end
def count(relation, fallback: -1) def count(relation, count_by: nil, fallback: -1)
relation.count count_by ? relation.count(count_by) : relation.count
rescue ActiveRecord::StatementInvalid rescue ActiveRecord::StatementInvalid
fallback fallback
end end
......
...@@ -20,5 +20,9 @@ FactoryBot.define do ...@@ -20,5 +20,9 @@ FactoryBot.define do
"email#{n}@email.com" "email#{n}@email.com"
end end
end end
trait(:ldap) do
ldap true
end
end end
end end
...@@ -266,6 +266,12 @@ describe Gitlab::UsageData do ...@@ -266,6 +266,12 @@ describe Gitlab::UsageData do
expect(described_class.count(relation)).to eq(1) expect(described_class.count(relation)).to eq(1)
end end
it 'returns the count for count_by when provided' do
allow(relation).to receive(:count).with(:creator_id).and_return(2)
expect(described_class.count(relation, count_by: :creator_id)).to eq(2)
end
it 'returns the fallback value when counting fails' do it 'returns the fallback value when counting fails' do
allow(relation).to receive(:count).and_raise(ActiveRecord::StatementInvalid.new('')) allow(relation).to receive(:count).and_raise(ActiveRecord::StatementInvalid.new(''))
......
...@@ -3,19 +3,29 @@ ...@@ -3,19 +3,29 @@
require 'spec_helper' require 'spec_helper'
describe GroupMember do describe GroupMember do
describe '.count_users_by_group_id' do context 'scopes' do
it 'counts users by group ID' do describe '.count_users_by_group_id' do
user_1 = create(:user) it 'counts users by group ID' do
user_2 = create(:user) user_1 = create(:user)
group_1 = create(:group) user_2 = create(:user)
group_2 = create(:group) group_1 = create(:group)
group_2 = create(:group)
group_1.add_owner(user_1)
group_1.add_owner(user_2) group_1.add_owner(user_1)
group_2.add_owner(user_1) group_1.add_owner(user_2)
group_2.add_owner(user_1)
expect(described_class.count_users_by_group_id).to eq(group_1.id => 2,
group_2.id => 1) expect(described_class.count_users_by_group_id).to eq(group_1.id => 2,
group_2.id => 1)
end
end
describe '.of_ldap_type' do
it 'returns ldap type users' do
group_member = create(:group_member, :ldap)
expect(described_class.of_ldap_type).to eq([group_member])
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