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
8 changes: 4 additions & 4 deletions app/jobs/reports/authentication_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ def perform(report_date)
subject = "Weekly Authentication Report - #{report_date}"

report_configs.each do |report_hash|
tables = weekly_authentication_report_tables(report_hash['issuers'])
reports = weekly_authentication_emailable_reports(report_hash['issuers'])

report_hash['emails'].each do |email|
ReportMailer.tables_report(
email:,
subject:,
message:,
tables:,
reports:,
attachment_format: :csv,
).deliver_now
end
Expand All @@ -30,11 +30,11 @@ def perform(report_date)

private

def weekly_authentication_report_tables(issuers)
def weekly_authentication_emailable_reports(issuers)
Reporting::AuthenticationReport.new(
issuers:,
time_range: report_date.all_week,
).as_tables_with_options
).as_emailable_reports
end

def report_configs
Expand Down
8 changes: 2 additions & 6 deletions app/jobs/reports/monthly_key_metrics_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ def perform(date = Time.zone.today)
]

reports.each do |report|
upload_to_s3(report.table, report_name: report.csv_name)
end

email_tables = reports.map do |report|
[report.email_options, *report.table]
upload_to_s3(report.table, report_name: report.filename)
end

email_message = "Report: #{REPORT_NAME} #{date}"
Expand All @@ -56,7 +52,7 @@ def perform(date = Time.zone.today)
email: email_addresses,
subject: "Monthly Key Metrics Report - #{date}",
message: email_message,
tables: email_tables,
reports: reports,
attachment_format: :xlsx,
).deliver_now
end
Expand Down
30 changes: 12 additions & 18 deletions app/mailers/report_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def warn_error(email:, error:, env: Rails.env)
# @param [String] subject
# @param [String] env name of current deploy environment
# @param [:csv,:xlsx] attachment_format
# @param [Array<Array<Hash,Array<String>>>] tables
# @param [Array<EmailableReport>] reports
# an array of tables (which are arrays of rows (arrays of strings))
# each table can have a first "row" that is a hash with options
# @option opts [Boolean] :float_as_percent whether or not to render floats as percents
Expand All @@ -49,40 +49,34 @@ def tables_report(
email:,
subject:,
message:,
tables:,
reports:,
attachment_format:,
env: Identity::Hostdata.env || 'local'
)
@message = message

@tables = tables.map(&:dup).each_with_index.map do |table, index|
options = table.first.is_a?(Hash) ? table.shift : {}

options[:title] ||= "Table #{index + 1}"

[options, *table]
@reports = reports.map(&:dup).each_with_index do |report, index|
report.title ||= "Table #{index + 1}"
end

case attachment_format
when :csv
@tables.each do |options_and_table|
options, *table = options_and_table

title = "#{options[:title].parameterize}.csv"
@reports.each do |report|
filename = "#{report.filename || report.title.parameterize}.csv"

attachments[title] = CSV.generate do |csv|
table.each do |row|
attachments[filename] = CSV.generate do |csv|
report.table.each do |row|
csv << row
end
end
end
when :xlsx
Axlsx::Package.new do |package|
@tables.each do |options_and_table|
options, *table = options_and_table
@reports.each do |report|
name = report.title.byteslice(0...31)

package.workbook.add_worksheet(name: options[:title].byteslice(0...31)) do |sheet|
table.each do |row|
package.workbook.add_worksheet(name: name) do |sheet|
report.table.each do |row|
sheet.add_row(row)
end
end
Expand Down
10 changes: 4 additions & 6 deletions app/services/reporting/account_deletion_rate_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ def account_deletion_report

def account_deletion_emailable_report
EmailableReport.new(
email_options: {
title: 'Account deletion rate (last 30 days)',
float_as_percent: true,
precision: 4,
},
title: 'Account deletion rate (last 30 days)',
float_as_percent: true,
precision: 4,
table: account_deletion_report,
csv_name: 'account_deletion_rate',
filename: 'account_deletion_rate',
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ def account_reuse_report

def account_reuse_emailable_report
EmailableReport.new(
email_options: {
title: "IDV app reuse rate #{stats_month}",
float_as_percent: true,
precision: 4,
},
csv_name: 'account_reuse',
title: "IDV app reuse rate #{stats_month}",
float_as_percent: true,
precision: 4,
filename: 'account_reuse',
table: account_reuse_report,
)
end
Expand All @@ -49,9 +47,9 @@ def total_identities_report

def total_identities_emailable_report
EmailableReport.new(
email_options: { title: 'Total proofed identities' },
title: 'Total proofed identities',
table: total_identities_report,
csv_name: 'total_profiles',
filename: 'total_profiles',
)
end

Expand Down
22 changes: 21 additions & 1 deletion app/services/reporting/emailable_report.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
module Reporting
EmailableReport = Struct.new(:email_options, :table, :csv_name)
# Represents tabular data and how to render the table in an email body and
# name it as an attachment
# @!attribute [rw] table
# @return [Array<Array<String>>]
# @!attribute [rw] filename
# @return [String] filename for attachment
# @!attribute [rw] title
# @return [String] title for table in email body
# @!attribute [rw] float_as_percent
# @return [Boolean] whether or not floating point values should be rendered as percents
# @!attribute [rw] precision
# @return [Integer] number of digits of precision for rendering as percent
EmailableReport = Struct.new(
:table,
:filename,
:title,
:float_as_percent,
:precision,
) do
alias_method :float_as_percent?, :float_as_percent
end
end
6 changes: 2 additions & 4 deletions app/services/reporting/monthly_active_users_count_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ def monthly_active_users_count_report

def monthly_active_users_count_emailable_report
EmailableReport.new(
email_options: {
title: "#{report_month_year} Active Users",
},
title: "#{report_month_year} Active Users",
table: monthly_active_users_count_report,
csv_name: 'monthly_active_users_count',
filename: 'monthly_active_users_count',
)
end

Expand Down
4 changes: 2 additions & 2 deletions app/services/reporting/total_user_count_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def total_user_count_report

def total_user_count_emailable_report
EmailableReport.new(
email_options: { title: 'Total user count (all-time)' },
title: 'Total user count (all-time)',
table: total_user_count_report,
csv_name: 'total_user_count',
filename: 'total_user_count',
)
end

Expand Down
13 changes: 6 additions & 7 deletions app/views/report_mailer/tables_report.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<%#
- @message: text to put in the beginning of the email
- @tables: an array of tables, expects first "row" to be an options hash
- @reports: an array of EmailableReport
%>
<%= @message %><br>
<% @tables.each do |table| %>
<% options, header, *rows = table %>

<h2><%= options[:title] %></h2>
<% @reports.each do |report| %>
<% header, *rows = report.table %>
<h2><%= report.title %></h2>

<table class="usa-table">
<thead>
Expand All @@ -21,8 +20,8 @@
<tr>
<% row.each do |cell| %>
<td <%= cell.is_a?(Numeric) ? 'class=table-number' : nil %> >
<% if cell.is_a?(Float) && options[:float_as_percent] %>
<%= number_to_percentage(cell * 100, precision: options[:precision] || 2) %>
<% if cell.is_a?(Float) && report.float_as_percent? %>
<%= number_to_percentage(cell * 100, precision: report.precision || 2) %>
<% else %>
<%= number_with_delimiter(cell) %>
<% end %>
Expand Down
12 changes: 9 additions & 3 deletions lib/reporting/authentication_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,16 @@ def as_tables
]
end

def as_tables_with_options
def as_emailable_reports
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @Sgtpluck tagging you on this PR just as a heads up that I refactored this report you added

[
[{ title: 'Overview' }, *overview_table],
[{ title: 'Authentication Funnel Metrics' }, *funnel_metrics_table],
Reporting::EmailableReport.new(
title: 'Overview',
table: overview_table,
),
Reporting::EmailableReport.new(
title: 'Authentication Funnel Metrics',
table: funnel_metrics_table,
),
]
end

Expand Down
6 changes: 3 additions & 3 deletions lib/reporting/monthly_proofing_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def self.all_events
end

# @param [Array<String>] issuers
# @param [Range<Time>] date
# @param [Range<Time>] time_range
def initialize(
time_range:,
verbose: false,
Expand All @@ -55,9 +55,9 @@ def progress?

def document_upload_proofing_emailable_report
EmailableReport.new(
email_options: { title: 'Document upload proofing rates' },
title: 'Document upload proofing rates',
table: proofing_report,
csv_name: 'document_upload_proofing',
filename: 'document_upload_proofing',
)
end

Expand Down
44 changes: 24 additions & 20 deletions spec/jobs/reports/authentication_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,33 @@
end

describe '#perform' do
let(:tables) do
let(:reports) do
[
[
{ title: 'Overview' },
['Report Timeframe', '2023-10-01 00:00:00 UTC to 2023-10-01 23:59:59 UTC'],
['Report Generated', '2023-10-02'],
['Issuer', 'some:issuer'],
['Total # of IAL1 Users', '75'],
],
[
{ title: 'Authentication Metrics Report' },
['Metric', 'Number of accounts', '% of total from start'],
['New Users Started IAL1 Verification', '100', '100%'],
['New Users Completed IAL1 Password Setup', '85', '85%'],
['New Users Completed IAL1 MFA', '80', '80%'],
['New IAL1 Users Consented to Partner', '75', '75%'],
['AAL2 Authentication Requests from Partner', '12', '12%'],
['AAL2 Authenticated Requests', '50', '50%'],
],
Reporting::EmailableReport.new(
title: 'Overview',
table: [
['Report Timeframe', '2023-10-01 00:00:00 UTC to 2023-10-01 23:59:59 UTC'],
['Report Generated', '2023-10-02'],
['Issuer', 'some:issuer'],
['Total # of IAL1 Users', '75'],
],
),
Reporting::EmailableReport.new(
title: 'Authentication Metrics Report',
table: [
['Metric', 'Number of accounts', '% of total from start'],
['New Users Started IAL1 Verification', '100', '100%'],
['New Users Completed IAL1 Password Setup', '85', '85%'],
['New Users Completed IAL1 MFA', '80', '80%'],
['New IAL1 Users Consented to Partner', '75', '75%'],
['AAL2 Authentication Requests from Partner', '12', '12%'],
['AAL2 Authenticated Requests', '50', '50%'],
],
),
]
end

let(:weekly_report) { double(Reporting::AuthenticationReport, as_tables_with_options: tables) }
let(:weekly_report) { double(Reporting::AuthenticationReport, as_emailable_reports: reports) }

before do
expect(Reporting::AuthenticationReport).to receive(:new).with(
Expand All @@ -61,7 +65,7 @@
email:,
subject: "Weekly Authentication Report - #{report_date}",
message: "Report: authentication-report #{report_date}",
tables:,
reports:,
attachment_format: :csv,
)

Expand Down
4 changes: 2 additions & 2 deletions spec/jobs/reports/monthly_key_metrics_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
message: 'Report: monthly-key-metrics-report 2021-03-02',
email: [agnes_email],
subject: 'Monthly Key Metrics Report - 2021-03-02',
tables: anything,
reports: anything,
attachment_format: :xlsx,
).and_call_original

Expand All @@ -72,7 +72,7 @@
message: 'Report: monthly-key-metrics-report 2021-03-01',
email: [agnes_email, feds_email],
subject: 'Monthly Key Metrics Report - 2021-03-01',
tables: anything,
reports: anything,
attachment_format: :xlsx,
).and_call_original

Expand Down
8 changes: 4 additions & 4 deletions spec/lib/reporting/authentication_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
end
end

describe '#as_tables_with_names' do
describe '#as_emailable_reports' do
it 'adds a "first row" hash with a title for tables_report mailer' do
tables = report.as_tables_with_options
reports = report.as_emailable_reports
aggregate_failures do
tables.each do |table|
expect(table[0][:title]).to_not be nil
reports.each do |report|
expect(report.title).to be_present
end
end
end
Expand Down
Loading