Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions app/jobs/reports/combined_invoice_supplement_report_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ def build_csv(iaas, partner_accounts)
)
end

by_issuer_profile_age_results = iaas.flat_map do |iaa|
iaa.issuers.flat_map do |issuer|
by_issuer_profile_age_results = partner_accounts.flat_map do |partner_account|
partner_account.issuers.flat_map do |issuer|
Db::MonthlySpAuthCount::NewUniqueMonthlyUserCountsByPartner.call(
partner: issuer, # just a label
partner: partner_account.partner,
issuers: [issuer],
start_date: iaa.start_date,
end_date: iaa.end_date,
start_date: partner_account.start_date,
end_date: partner_account.end_date,
)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module MonthlySpAuthCount
module NewUniqueMonthlyUserCountsByPartner
extend Reports::QueryHelpers

UserVerifiedKey = Data.define(:user_id, :profile_verified_at, :profile_age).freeze

module_function

# @param [String] partner label for billing (Partner requesting agency)
Expand Down Expand Up @@ -43,31 +45,34 @@ def call(partner:, issuers:, start_date:, end_date:)
) do
Reports::BaseReport.transaction_with_timeout do
ActiveRecord::Base.connection.execute(query).each do |row|
user_id = row['user_id']
year_month = row['year_month']
profile_age = row['profile_age']
user_id = row['user_id']
profile_verified_at = row['profile_verified_at']

year_month_to_users_to_profile_age[year_month][user_id] = profile_age
user_unique_id = UserVerifiedKey.new(user_id:, profile_verified_at:, profile_age:)

year_month_to_users_to_profile_age[year_month][user_unique_id] = profile_age
end
end
end
end

rows = []

prev_seen_users = Set.new
prev_seen_user_proofed_events = Set.new
issuers_set = issuers.to_set
year_months = year_month_to_users_to_profile_age.keys.sort

# rubocop:disable Metrics/BlockLength
year_months.each do |year_month|
users_to_profile_age = year_month_to_users_to_profile_age[year_month]

this_month_users = users_to_profile_age.keys.to_set
new_unique_users = this_month_users - prev_seen_users
this_month_user_proofed_events = users_to_profile_age.keys.to_set
new_unique_user_proofed_events = this_month_user_proofed_events -
prev_seen_user_proofed_events

profile_age_counts = new_unique_users.group_by do |user_id|
age = users_to_profile_age[user_id]
profile_age_counts = new_unique_user_proofed_events.group_by do |user_unique_id|
age = users_to_profile_age[user_unique_id]
if age.nil? || age < 0
:unknown
elsif age > 4
Expand All @@ -77,16 +82,16 @@ def call(partner:, issuers:, start_date:, end_date:)
end
end.tap { |counts| counts.default = [] }

prev_seen_users |= this_month_users
prev_seen_user_proofed_events |= this_month_user_proofed_events

rows << {
partner: partner,
issuers: issuers_set,
year_month: year_month,
iaa_start_date: date_range.begin.to_s,
iaa_end_date: date_range.end.to_s,
unique_users: this_month_users.count,
new_unique_users: new_unique_users.count,
unique_user_proofed_events: this_month_user_proofed_events.count,
new_unique_user_proofed_events: new_unique_user_proofed_events.count,
partner_ial2_new_unique_users_year1: profile_age_counts[0].count,
partner_ial2_new_unique_users_year2: profile_age_counts[1].count,
partner_ial2_new_unique_users_year3: profile_age_counts[2].count,
Expand All @@ -105,7 +110,7 @@ def call(partner:, issuers:, start_date:, end_date:)
# the first and last may be partial months
# @return [Array<String>]
def build_queries(issuers:, months:)
months.map do |month_range|
months.map do |month_range| # rubocop:disable Metrics/BlockLength
params = {
range_start: month_range.begin,
range_end: month_range.end,
Expand All @@ -117,10 +122,12 @@ def build_queries(issuers:, months:)
SELECT
subq.user_id AS user_id
, %{year_month} AS year_month
, MIN(subq.profile_age) AS profile_age
, subq.profile_verified_at
, subq.profile_age
FROM (
SELECT
sp_return_logs.user_id
, sp_return_logs.profile_verified_at
, DATE_PART('year', AGE(sp_return_logs.returned_at, sp_return_logs.profile_verified_at)) AS profile_age
FROM sp_return_logs
WHERE
Expand All @@ -131,6 +138,8 @@ def build_queries(issuers:, months:)
) subq
GROUP BY
subq.user_id
, subq.profile_verified_at
, subq.profile_age
SQL
end
end
Expand Down
42 changes: 29 additions & 13 deletions spec/jobs/reports/combined_invoice_supplement_report_v2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@
end

let(:iaa1) { 'iaa1' }
let(:iaa1_range) { Date.new(2020, 4, 15)..Date.new(2021, 4, 14) }
let(:iaa1_range) { DateTime.new(2020, 4, 15).utc..DateTime.new(2021, 4, 14).utc }
let(:inside_iaa1) { iaa1_range.begin + 1.day }

let(:iaa2) { 'iaa2' }
let(:iaa2_range) { Date.new(2020, 9, 1)..Date.new(2021, 8, 30) }
let(:iaa2_range) { DateTime.new(2020, 9, 1).utc..DateTime.new(2021, 8, 30).utc }
let(:inside_iaa2) { iaa2_range.begin + 1.day }

describe '#perform' do
Expand All @@ -90,6 +90,8 @@
let(:user2) { create(:user) }
let(:user3) { create(:user) }
let(:user4) { create(:user) }
let(:user5) { create(:user) }
let(:user6) { create(:user) }

before do
iaa_order1.integrations << build_integration(
Expand Down Expand Up @@ -126,7 +128,7 @@
issuer: iaa2_sp1.issuer,
requested_at: inside_iaa2,
returned_at: inside_iaa2,
profile_verified_at: '2020-01-01 00:00:00',
profile_verified_at: DateTime.new(2020, 1, 1).utc,
billable: true,
)

Expand All @@ -139,7 +141,7 @@
issuer: iaa2_sp2.issuer,
requested_at: inside_iaa2,
returned_at: inside_iaa2,
profile_verified_at: '2019-01-01 00:00:00',
profile_verified_at: DateTime.new(2019, 1, 1).utc,
billable: true,
)
end
Expand All @@ -155,6 +157,20 @@
profile_verified_at: nil,
billable: true,
)

# 2 new unique users in month 1 at IAA 1 sp @ IAL 2 with profile age 2
[user5, user6].each do |user|
create(
:sp_return_log,
user_id: user.id,
ial: 2,
issuer: iaa1_sp.issuer,
requested_at: inside_iaa1,
returned_at: inside_iaa1,
profile_verified_at: DateTime.new(2018, 6, 1).utc,
billable: true,
)
end
end

it 'generates a report by iaa + order number and issuer and year month' do
Expand All @@ -165,7 +181,7 @@
aggregate_failures do
row = csv.find { |r| r['issuer'] == iaa1_sp.issuer }
expect(row['iaa_order_number']).to eq('gtc1234-0001')
expect(row['partner']).to eq(nil)
expect(row['partner']).to eq(partner_account1.requesting_agency)
expect(row['iaa_start_date']).to eq('2020-04-15')
expect(row['iaa_end_date']).to eq('2021-04-14')

Expand All @@ -176,25 +192,25 @@
expect(row['year_month_readable']).to eq('April 2020')

expect(row['iaa_ial1_unique_users'].to_i).to eq(1)
expect(row['iaa_ial2_unique_users'].to_i).to eq(0)
expect(row['iaa_ial1_plus_2_unique_users'].to_i).to eq(1)
expect(row['iaa_ial2_unique_users'].to_i).to eq(2)
expect(row['iaa_ial1_plus_2_unique_users'].to_i).to eq(3)
expect(row['partner_ial2_new_unique_users_year1'].to_i).to eq(0)
expect(row['partner_ial2_new_unique_users_year2'].to_i).to eq(0)
expect(row['partner_ial2_new_unique_users_year2'].to_i).to eq(2)
expect(row['partner_ial2_new_unique_users_year3'].to_i).to eq(0)
expect(row['partner_ial2_new_unique_users_year4'].to_i).to eq(0)
expect(row['partner_ial2_new_unique_users_year5'].to_i).to eq(0)
expect(row['partner_ial2_new_unique_users_year_greater_than_5'].to_i).to eq(0)
expect(row['partner_ial2_new_unique_users_unknown'].to_i).to eq(0)

expect(row['issuer_ial1_total_auth_count'].to_i).to eq(1)
expect(row['issuer_ial2_total_auth_count'].to_i).to eq(0)
expect(row['issuer_ial1_plus_2_total_auth_count'].to_i).to eq(1)
expect(row['issuer_ial2_total_auth_count'].to_i).to eq(2)
expect(row['issuer_ial1_plus_2_total_auth_count'].to_i).to eq(3)

expect(row['issuer_ial1_unique_users'].to_i).to eq(1)
expect(row['issuer_ial2_unique_users'].to_i).to eq(0)
expect(row['issuer_ial1_plus_2_unique_users'].to_i).to eq(1)
expect(row['issuer_ial2_unique_users'].to_i).to eq(2)
expect(row['issuer_ial1_plus_2_unique_users'].to_i).to eq(3)
expect(row['issuer_ial2_new_unique_users_year1'].to_i).to eq(0)
expect(row['issuer_ial2_new_unique_users_year2'].to_i).to eq(0)
expect(row['issuer_ial2_new_unique_users_year2'].to_i).to eq(2)
expect(row['issuer_ial2_new_unique_users_year3'].to_i).to eq(0)
expect(row['issuer_ial2_new_unique_users_year4'].to_i).to eq(0)
expect(row['issuer_ial2_new_unique_users_year5'].to_i).to eq(0)
Expand Down
Loading