-
Notifications
You must be signed in to change notification settings - Fork 167
LG-15559: Add support for A/B tests daily reporting #11835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7350663
LG-15559: Add support for A/B tests daily reporting
aduth 3f87e45
Add expected progress argument value
aduth 4c10d2d
Revise A/B test report options to struct
aduth da8f1db
Return `nil` for report if option not passed
aduth 1333069
Memoize active value without modifying frozen instance
aduth 4653c88
Update hash option to struct value
aduth 93f1a7d
Avoid naming conflicts with existing report class for stubbed data
aduth fcd8b74
Format float_as_percent if between 0 and 1
aduth cf7825d
Handle Float::NAN when formatting float as percent
aduth 28160fb
Use format method to format as percent
aduth 1da1018
Remove unused CLI supports
aduth e625a4e
Remove unused CloudwatchClient options
aduth e91bc13
Add method visibility for public API
aduth ebb07f5
Fix YARDoc param type for queries
aduth d2a453d
Format numbers outside 0-1 as percent
aduth 5f96a9d
Fix spec row label to increment
aduth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'reporting/ab_tests_report' | ||
|
|
||
| module Reports | ||
| class AbTestsReport < BaseReport | ||
| attr_reader :report_date | ||
|
|
||
| def initialize(report_date = nil, *args, **kwargs) | ||
| @report_date = report_date | ||
| super(*args, **kwargs) | ||
| end | ||
|
|
||
| # @param [DateTime] | ||
| def perform(report_date) | ||
| @report_date = report_date | ||
|
|
||
| report_configs.each do |config| | ||
| tables_report(config).deliver_now | ||
| end | ||
| end | ||
|
|
||
| def tables_report(config) | ||
| experiment_name = config.experiment_name | ||
| subject = "A/B Tests Report - #{experiment_name} - #{report_date}" | ||
| reports = ab_tests_report(config).as_emailable_reports | ||
|
|
||
| ReportMailer.tables_report( | ||
| email: config.email, | ||
| subject:, | ||
| message: subject, | ||
| reports:, | ||
| attachment_format: :csv, | ||
| ) | ||
| end | ||
|
|
||
| def ab_tests_report(config) | ||
| Reporting::AbTestsReport.new( | ||
| queries: config.queries, | ||
| time_range: report_date.yesterday..report_date, | ||
| ) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def report_configs | ||
| AbTests | ||
| .all | ||
| .values | ||
| .select { |ab_test| ab_test.report&.email&.present? && ab_test.active? } | ||
| .map(&:report) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'reporting/cloudwatch_client' | ||
| require 'reporting/cloudwatch_query_quoting' | ||
|
|
||
| module Reporting | ||
| class AbTestsReport | ||
| attr_reader :queries, :time_range | ||
|
|
||
| # @param [Array<AbTest::ReportQueryConfig>] queries | ||
| # @param [Range<Time>] time_range | ||
| def initialize( | ||
| queries:, | ||
| time_range: | ||
| ) | ||
| @queries = queries | ||
| @time_range = time_range | ||
| end | ||
|
|
||
| def as_tables | ||
| queries.map(&method(:table_for_query)) | ||
| end | ||
|
|
||
| def as_emailable_reports | ||
| queries.map do |query| | ||
| Reporting::EmailableReport.new( | ||
| title: query.title, | ||
| table: table_for_query(query), | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def table_for_query(query) | ||
| query_data = fetch_results(query: query.query) | ||
| headers = column_labels(query_data.first) | ||
| rows = query_data.map(&:values) | ||
|
|
||
| [ | ||
| headers, | ||
| *format_rows(rows, headers, query.row_labels), | ||
| ] | ||
| end | ||
|
|
||
| def format_rows(rows, headers, row_labels) | ||
| if row_labels | ||
| rows = rows.each_with_index.map do |value, index| | ||
| [row_labels[index], *value.slice(1)] | ||
| end | ||
| end | ||
|
|
||
| headers.each_index.select { |i| headers[i] =~ /percent/i }.each do |percent_index| | ||
| rows.each { |row| row[percent_index] = format_as_percent(row[percent_index]) } | ||
| end | ||
aduth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| rows | ||
| end | ||
|
|
||
| def fetch_results(query:) | ||
| cloudwatch_client.fetch(query:, from: time_range.begin, to: time_range.end) | ||
| end | ||
|
|
||
| def column_labels(row) | ||
| row.keys | ||
| end | ||
|
|
||
| def cloudwatch_client | ||
| @cloudwatch_client ||= Reporting::CloudwatchClient.new( | ||
| progress: false, | ||
| ensure_complete_logs: false, | ||
| ) | ||
| end | ||
|
|
||
| # @return [String] | ||
| def format_as_percent(number) | ||
| format('%.2f%%', number) | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe Reports::AbTestsReport do | ||
| let(:report_date) { Date.new(2023, 12, 25) } | ||
| let(:email) { 'email@example.com' } | ||
| let(:tested_percent) { 1 } | ||
| let(:queries) do | ||
| [ | ||
| { | ||
| title: 'Sign in success rate by CAPTCHA validation performed', | ||
| query: <<~QUERY, | ||
| fields properties.event_properties.captcha_validation_performed as `Captcha Validation Performed` | ||
| | filter name = 'Email and Password Authentication' | ||
| | stats avg(properties.event_properties.success)*100 as `Success Percent` by `Captcha Validation Performed` | ||
| | sort `Captcha Validation Performed` asc | ||
| QUERY | ||
| row_labels: ['Validation Not Performed', 'Validation Performed'], | ||
| }, | ||
| ] | ||
| end | ||
| let(:ab_tests) do | ||
| { | ||
| RECAPTCHA_SIGN_IN: AbTest.new( | ||
| experiment_name: 'reCAPTCHA at Sign-In', | ||
| buckets: { sign_in_recaptcha: tested_percent }, | ||
| report: { email:, queries: }, | ||
| ), | ||
| } | ||
| end | ||
|
|
||
| before do | ||
| allow(AbTests).to receive(:all).and_return(ab_tests) | ||
| end | ||
|
|
||
| describe '#perform' do | ||
| let(:emailable_reports) do | ||
| [ | ||
| Reporting::EmailableReport.new( | ||
| title: 'Sign in success rate by CAPTCHA validation performed', | ||
| table: [ | ||
| ['Captcha Validation Performed', 'Success Percent'], | ||
| ['Validation Not Performed', '90.19%'], | ||
| ['Validation Performed', '85.68%'], | ||
| ], | ||
| ), | ||
| ] | ||
| end | ||
|
|
||
| let(:report) do | ||
| double(Reporting::AbTestsReport, as_emailable_reports: emailable_reports) | ||
| end | ||
|
|
||
| before do | ||
| allow(Reporting::AbTestsReport).to receive(:new).with( | ||
| queries:, | ||
| time_range: report_date.yesterday..report_date, | ||
| ).and_return(report) | ||
|
|
||
| allow(ReportMailer).to receive(:tables_report).and_call_original | ||
| end | ||
|
|
||
| it 'emails the table report with csv' do | ||
| expect(ReportMailer).to receive(:tables_report).with( | ||
| email:, | ||
| subject: "A/B Tests Report - reCAPTCHA at Sign-In - #{report_date}", | ||
| message: "A/B Tests Report - reCAPTCHA at Sign-In - #{report_date}", | ||
| reports: emailable_reports, | ||
| attachment_format: :csv, | ||
| ) | ||
|
|
||
| subject.perform(report_date) | ||
| end | ||
|
|
||
| context 'when associated report email is nil' do | ||
| let(:email) { nil } | ||
|
|
||
| it 'does not email the table report' do | ||
| expect(ReportMailer).not_to receive(:tables_report) | ||
|
|
||
| subject.perform(report_date) | ||
| end | ||
| end | ||
|
|
||
| context 'when a/b test buckets are zero (test is inactive)' do | ||
| let(:tested_percent) { 0 } | ||
|
|
||
| it 'does not email the table report' do | ||
| expect(ReportMailer).not_to receive(:tables_report) | ||
|
|
||
| subject.perform(report_date) | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.