-
Notifications
You must be signed in to change notification settings - Fork 167
LG-3941 Track statistics for letter responses over time #4550
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
stevegsa
merged 7 commits into
master
from
stevegsa-track-statistics-for-letter-responses
Jan 7, 2021
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6fd3028
LG-3941 Track statistics for letter responses over time
stevegsa 0108545
Remove dev
stevegsa 396a081
More specs
stevegsa 2b7d893
More specs
stevegsa 2dd41c8
Spaces
stevegsa 5cc1912
sql readability
stevegsa 5a9e5ab
Init
stevegsa 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
25 changes: 25 additions & 0 deletions
25
app/services/db/usps_confirmation_code/letters_sent_and_verified_since.rb
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,25 @@ | ||
| module Db | ||
| module UspsConfirmationCode | ||
| class LettersSentAndVerifiedSince | ||
| def self.call(start_date) | ||
| params = { | ||
| start: ActiveRecord::Base.connection.quote(start_date), | ||
| } | ||
| sql = format(<<~SQL, params) | ||
| SELECT COUNT(*) | ||
| FROM profiles | ||
| WHERE active = true | ||
| AND activated_at IS NOT NULL | ||
| AND verified_at IS NOT NULL | ||
| AND id IN ( | ||
| SELECT profile_id | ||
| FROM usps_confirmation_codes | ||
| WHERE %{start} <= created_at | ||
| ) | ||
| SQL | ||
| recs = ActiveRecord::Base.connection.execute(sql) | ||
| recs[0]['count'].to_i | ||
| end | ||
| end | ||
| end | ||
| end |
17 changes: 17 additions & 0 deletions
17
app/services/db/usps_confirmation_code/letters_sent_since.rb
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,17 @@ | ||
| module Db | ||
| module UspsConfirmationCode | ||
| class LettersSentSince | ||
| def self.call(start_date) | ||
| params = { | ||
| start: ActiveRecord::Base.connection.quote(start_date), | ||
| } | ||
| sql = format(<<~SQL, params) | ||
| SELECT COUNT(*) | ||
| FROM usps_confirmation_codes WHERE %{start}<=created_at | ||
| SQL | ||
| recs = ActiveRecord::Base.connection.execute(sql) | ||
| recs[0]['count'].to_i | ||
| end | ||
| 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,43 @@ | ||
| require 'login_gov/hostdata' | ||
|
|
||
| module Reports | ||
| class UspsReport < BaseReport | ||
| REPORT_NAME = 'usps-report'.freeze | ||
|
|
||
| def initialize | ||
| @results = { | ||
| today: Time.zone.today.to_s, | ||
| letters_sent_since_days: {}, | ||
| letters_sent_and_validated_since_days: {}, | ||
| percent_sent_and_validated_since_days: {}, | ||
| } | ||
| end | ||
|
|
||
| def call | ||
| create_reports | ||
| save_report(REPORT_NAME, @results.to_json) | ||
| @results.to_json | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def create_reports | ||
| [7, 14, 30, 60, 90, 10_000].each do |days_ago| | ||
| sent = letters_sent_since(days_ago) | ||
| validated = letters_sent_and_validated_since(days_ago) | ||
| @results[:percent_sent_and_validated_since_days][days_ago] = | ||
| validated.zero? ? 0 : (validated * 100.0 / sent).round(2) | ||
| end | ||
| end | ||
|
|
||
| def letters_sent_since(days_ago) | ||
| @results[:letters_sent_since_days][days_ago] = | ||
| Db::UspsConfirmationCode::LettersSentSince.call(days_ago.days.ago) | ||
| end | ||
|
|
||
| def letters_sent_and_validated_since(days_ago) | ||
| @results[:letters_sent_and_validated_since_days][days_ago] = | ||
| Db::UspsConfirmationCode::LettersSentAndVerifiedSince.call(days_ago.days.ago) | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| require 'rails_helper' | ||
|
|
||
| describe Reports::UspsReport do | ||
| subject { described_class } | ||
| let(:empty_report) do | ||
| { | ||
| 'letters_sent_and_validated_since_days' => { | ||
| '10000'=>0, '14'=>0, '30'=>0, '60'=>0, '7'=>0, '90'=>0 | ||
| }, | ||
| 'letters_sent_since_days' => { | ||
| '10000'=>0, '14'=>0, '30'=>0, '60'=>0, '7'=>0, '90'=>0 | ||
| }, | ||
| 'percent_sent_and_validated_since_days' => { | ||
| '10000'=>0, '14'=>0, '30'=>0, '60'=>0, '7'=>0, '90'=>0 | ||
| }, | ||
| 'today' => Time.zone.today.to_s, | ||
| } | ||
| end | ||
| let(:one_letter_sent_and_verified_report) do | ||
| { | ||
| 'letters_sent_and_validated_since_days' => { | ||
| '10000'=>1, '14'=>1, '30'=>1, '60'=>1, '7'=>1, '90'=>1 | ||
| }, | ||
| 'letters_sent_since_days' => { | ||
| '10000'=>1, '14'=>1, '30'=>1, '60'=>1, '7'=>1, '90'=>1 | ||
| }, | ||
| 'percent_sent_and_validated_since_days' => { | ||
| '10000'=>100.0, '14'=>100.0, '30'=>100.0, '60'=>100.0, '7'=>100.0, '90'=>100.0 | ||
| }, | ||
| 'today' => Time.zone.today.to_s, | ||
| } | ||
| end | ||
| let(:user) { create(:user) } | ||
| let(:profile) { build(:profile, :active, :verified, user: user, pii: { ssn: '1234' }) } | ||
|
|
||
| it 'correctly reports zero letters sent' do | ||
| expect(JSON.parse(subject.new.call)).to eq(empty_report) | ||
| end | ||
|
|
||
| it 'correctly reports one letter sent that was verified' do | ||
| create_ucc_for(profile) | ||
| expect(JSON.parse(subject.new.call)).to eq(one_letter_sent_and_verified_report) | ||
| end | ||
|
|
||
| def create_ucc_for(profile) | ||
| UspsConfirmationCode.create( | ||
| profile: profile, | ||
| otp_fingerprint: 'foo', | ||
| ) | ||
| end | ||
| end |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is 6 x 2 = 12 table scans of the
usps_confirmation_codestable, would it make sense to see if we can parameterize some of this and make one aggregate query? It might be some gnarlyCASEstatementsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i could do a one pass but the usps_confirmation_codes table currently has a few thousand rows. it would need to get several orders of magnitude bigger before it ever took longer than a sub second total.