Skip to content
26 changes: 24 additions & 2 deletions app/services/reporting/account_reuse_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,20 @@ def initialize(

def update_details(
num_entities: nil, entity_type: nil,
num_idv_users: nil, num_all_users: nil
num_all_users: nil, all_percent: nil,
num_idv_users: nil, idv_percent: nil
)
self.num_entities = num_entities if !num_entities.nil?

self.entity_type = entity_type if !entity_type.nil?

self.num_all_users = num_all_users if !num_all_users.nil?

self.all_percent = all_percent if !all_percent.nil?

self.num_idv_users = num_idv_users if !num_idv_users.nil?

self.num_all_users = num_all_users if !num_all_users.nil?
self.idv_percent = idv_percent if !idv_percent.nil?

self
end
Expand Down Expand Up @@ -173,6 +178,23 @@ def update_from_results(results:, total_registered:, total_proofed:)
end
end
end

results.each_with_index do |details_section, section_index|
details_section.select { |details| details.num_entities >= 10 }.
reduce do |summary_row, captured_row|
# Delete any rows after the first captured_row (which becomes the summary_row)
details_section.delete(captured_row) if captured_row != summary_row
summary_row.update_details(
num_entities: "10-#{captured_row.num_entities}",
entity_type: summary_row.entity_type,
num_all_users: summary_row.num_all_users + captured_row.num_all_users,
all_percent: summary_row.all_percent + captured_row.all_percent,
num_idv_users: summary_row.num_idv_users + captured_row.num_idv_users,
idv_percent: summary_row.idv_percent + captured_row.idv_percent,
)
end
end

self.details_section = results

self
Expand Down
171 changes: 141 additions & 30 deletions spec/services/reporting/account_reuse_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,33 @@
let(:in_query) { report_date - 12.days }
let(:out_of_query) { report_date + 12.days }

let(:agency) { create(:agency, name: 'The Agency') }
let(:agency1) { create(:agency, name: 'The Agency') }
let(:agency2) { create(:agency, name: 'The Other Agency') }
let(:sp_a) { 'a' }
let(:sp_b) { 'b' }
let(:sp_c) { 'c' }
let(:sp_d) { 'd' }
let(:sp_e) { 'e' }
let(:sp_f) { 'f' }
let(:sp_g) { 'g' }
let(:sp_h) { 'h' }
let(:sp_i) { 'i' }
let(:sp_j) { 'j' }
let(:sp_k) { 'k' }
let(:sp_l) { 'l' }

let(:agency1_apps) { [sp_a, sp_d, sp_e, sp_h, sp_i, sp_l] }
let(:all_agency_apps) do
[sp_a, sp_b, sp_c, sp_d, sp_e, sp_f, sp_g, sp_h, sp_i, sp_j, sp_k, sp_l]
end

before do
create(
:service_provider,
issuer: sp_a,
iaa: 'iaa123',
friendly_name: 'The App',
agency: agency,
agency: agency1,
)
create(
:service_provider,
Expand All @@ -48,7 +61,63 @@
issuer: sp_d,
iaa: 'iaa321',
friendly_name: 'The Other First App',
agency: agency,
agency: agency1,
)
create(
:service_provider,
issuer: sp_e,
iaa: 'iaa123',
friendly_name: 'App E',
agency: agency1,
)
create(
:service_provider,
issuer: sp_f,
iaa: 'iaa456',
friendly_name: 'App F',
agency: agency2,
)
create(
:service_provider,
issuer: sp_g,
iaa: 'iaa789',
friendly_name: 'App G',
agency: agency2,
)
create(
:service_provider,
issuer: sp_h,
iaa: 'iaa321',
friendly_name: 'App H',
agency: agency1,
)
create(
:service_provider,
issuer: sp_i,
iaa: 'iaa123',
friendly_name: 'App I',
agency: agency1,
)
create(
:service_provider,
issuer: sp_j,
iaa: 'iaa456',
friendly_name: 'App J',
agency: agency2,
)
create(
:service_provider,
issuer: sp_k,
iaa: 'iaa789',
friendly_name: 'App K',
agency: agency2,
)
create(
:service_provider,
issuer: sp_l,
iaa: 'iaa321',
friendly_name: 'App L',
agency: agency1,
)

# Seed the database with data to be queried
Expand All @@ -61,57 +130,72 @@
# User 6 has 2 SPs and only 1 shows up in the query
# User 7 has 1 SP and 1 shows up in the query
# User 8 has 1 SP and 0 show up in the query
# User 9 has 2 SPs and only 1 shows up in the query
# User 10 has 2 SPs and 0 show up in the query
# User 11 has 2 SPs and 0 show up in the query
# User 12 has 1 SP and 1 shows up in the query
# User 13 has 1 SP and 0 show up in the query
# User 14 has 1 SP and 0 show up in the query
# User 15 has 12 SPs and 12 show up in the query
# User 16 has 11 SPs and 11 show up in the query
# User 17 has 11 SPs and 11 show up in the query
# User 18 has 10 SPs and 10 show up in the query
# User 19 has 10 SPs and 10 show up in the query
# User 20 has 10 SPs and 9 show up in the query
#
# This will give 1 user with 3 SPs/apps and 3 users with 2 SPs/apps for the IDV app report
# This will give 4 users with 3 SPs/apps and 5 users with 2 SPs/apps for the ALL app report
# This will give 3 users with 2 agencies for the IDV agency report
# This will give 7 users with 2 agencies for the ALL agency report
# This will give 1 user with 9 SPs/apps for the ALL app report and 0 for the IDV app report
# This will give 6 users with 10-12 SPs/apps for the ALL app report
# This will give 5 users with 10-12 SPs/apps for the IDV app report
# This will give 9 users with 2 agencies for the IDV agency report
# This will give 13 users with 2 agencies for the ALL agency report

users_to_query = [
{ id: 1, # 3 apps, 2 agencies
created_timestamp: in_query,
sp: [sp_a, sp_b, sp_c],
sp_timestamp: [in_query, in_query, in_query] },
sp: all_agency_apps.first(3),
sp_timestamp: Array.new(3) { in_query } },
{ id: 2, # 3 apps, 2 agencies
created_timestamp: in_query,
sp: [sp_a, sp_b, sp_c],
sp: all_agency_apps.first(3),
sp_timestamp: [in_query, in_query, out_of_query] },
{ id: 3, # 3 apps, 2 agencies
created_timestamp: in_query,
sp: [sp_a, sp_b, sp_c],
sp: all_agency_apps.first(3),
sp_timestamp: [in_query, out_of_query, out_of_query] },
{ id: 4, # 3 apps, 2 agencies
created_timestamp: in_query,
sp: [sp_a, sp_b, sp_c],
sp: all_agency_apps.first(3),
sp_timestamp: [in_query, out_of_query, out_of_query] },
{ id: 5, # 3 apps, 2 agencies
created_timestamp: out_of_query,
sp: [sp_a, sp_b, sp_c],
sp_timestamp: [out_of_query, out_of_query, out_of_query] },
sp: all_agency_apps.first(3),
sp_timestamp: Array.new(3) { out_of_query } },
{ id: 6, # 2 apps, 2 agencies
created_timestamp: in_query,
sp: [sp_a, sp_b],
sp_timestamp: [in_query, in_query] },
sp: all_agency_apps.first(2),
sp_timestamp: Array.new(2) { in_query } },
{ id: 7, # 2 apps, 1 agency
created_timestamp: in_query,
sp: [sp_a, sp_d],
sp_timestamp: [in_query, in_query] },
sp: agency1_apps.first(2),
sp_timestamp: Array.new(2) { in_query } },
{ id: 8, # 2 apps, 2 agencies
created_timestamp: in_query,
sp: [sp_a, sp_b],
sp: all_agency_apps.first(2),
sp_timestamp: [in_query, out_of_query] },
{ id: 9, # 2 apps, 1 agency
created_timestamp: in_query,
sp: [sp_a, sp_d],
sp: agency1_apps.first(2),
sp_timestamp: [in_query, out_of_query] },
{ id: 10, # 2 apps, 2 agencies
created_timestamp: in_query,
sp: [sp_a, sp_b],
sp_timestamp: [out_of_query, out_of_query] },
sp: all_agency_apps.first(2),
sp_timestamp: Array.new(2) { out_of_query } },
{ id: 11, # 2 apps, 2 agencies
created_timestamp: out_of_query,
sp: [sp_a, sp_b],
sp_timestamp: [out_of_query, out_of_query] },
sp: all_agency_apps.first(2),
sp_timestamp: Array.new(2) { out_of_query } },
{ id: 12,
created_timestamp: in_query,
sp: [sp_a],
Expand All @@ -124,6 +208,31 @@
created_timestamp: out_of_query,
sp: [sp_a],
sp_timestamp: [out_of_query] },
{ id: 15, # 12 apps, 2 agencies
created_timestamp: in_query,
sp: all_agency_apps,
sp_timestamp: Array.new(12) { in_query } },
{ id: 16, # 11 apps, 2 agencies
created_timestamp: in_query,
sp: all_agency_apps.first(11),
sp_timestamp: Array.new(11) { in_query } },
{ id: 17, # 11 apps, 2 agencies
created_timestamp: in_query,
sp: all_agency_apps.first(11),
sp_timestamp: Array.new(11) { in_query } },
{ id: 18, # 10 apps, 2 agencies
created_timestamp: in_query,
sp: all_agency_apps.first(10),
sp_timestamp: Array.new(10) { in_query } },
{ id: 19, # 10 apps, 2 agencies
created_timestamp: in_query,
sp: all_agency_apps.first(10),
sp_timestamp: Array.new(10) { in_query } },
{ id: 20, # 10 apps, 2 agencies
created_timestamp: in_query,
sp: all_agency_apps.first(10),
sp_timestamp: Array.new(9) { in_query } + Array.new(1) { out_of_query } },

]

users_to_query.each do |user|
Expand All @@ -139,16 +248,16 @@
end

# Create active profiles for total_proofed_identities
# These 13 profiles will yield 10 active profiles in the results
(1..10).each do |_|
# These 20 profiles will yield 10 active profiles in the results
10.times do
create(
:profile,
:active,
activated_at: in_query,
user: create(:user, :fully_registered, registered_at: in_query),
)
end
(1..3).each do |_|
10.times do
create(
:profile,
:active,
Expand All @@ -162,11 +271,13 @@
it 'has the correct results' do
expected_csv = [
['Metric', 'Num. all users', '% of accounts', 'Num. IDV users', '% of accounts'],
['2 apps', 5, 5 / 13.0, 3, 0.3],
['3 apps', 4, 4 / 13.0, 1, 0.1],
['2+ apps', 9, 9 / 13.0, 4, 0.4],
['2 agencies', 7, 7 / 13.0, 3, 0.3],
['2+ agencies', 7, 7 / 13.0, 3, 0.3],
['2 apps', 5, 5 / 20.0, 3, 0.3],
['3 apps', 4, 4 / 20.0, 1, 0.1],
['9 apps', 0, 0 / 20.0, 1, 0.1],
['10-12 apps', 6, 6 / 20.0, 5, 0.5],
['2+ apps', 15, 15 / 20.0, 10, 0.9999999999999999],
['2 agencies', 13, 13 / 20.0, 9, 0.9],
['2+ agencies', 13, 13 / 20.0, 9, 0.9],
]

aggregate_failures do
Expand Down