Skip to content

Commit 28062d8

Browse files
Update missing form status job to inspect batches rather than individual records - IVC CHAMPVA (#21092)
* first pass at implementation * updated documentation * updated to use batches * updated timing * more tests * added new tests * fixing flaky test
1 parent d603704 commit 28062d8

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

modules/ivc_champva/app/jobs/ivc_champva/missing_form_status_job.rb

+32-4
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ class MissingFormStatusJob
1212
def perform # rubocop:disable Metrics/MethodLength
1313
return unless Settings.ivc_forms.sidekiq.missing_form_status_job.enabled
1414

15-
forms = IvcChampvaForm.where(pega_status: nil).where('created_at < ?', 1.minute.ago)
15+
batches = get_nil_batches
1616

17-
return unless forms.any?
17+
return unless batches.any?
1818

1919
# Send the count of forms to DataDog
20-
StatsD.gauge('ivc_champva.forms_missing_status.count', forms.count)
20+
StatsD.gauge('ivc_champva.forms_missing_status.count', batches.count)
2121

2222
current_time = Time.now.utc
23-
forms.each do |form|
23+
24+
batches.each_value do |batch|
25+
form = batch[0] # get a representative form from this submission batch
2426
# Check if we've been missing Pega status for > custom threshold of days:
2527
elapsed_days = (current_time - form.created_at).to_i / 1.day
2628
threshold = Settings.vanotify.services.ivc_champva.failure_email_threshold_days.to_i || 7
@@ -106,6 +108,32 @@ def send_zsf_notification_to_pega(form, template_id)
106108
end
107109
end
108110

111+
##
112+
# Returns form submissions with nil pega statuses created more than 1 minute ago
113+
# organized in batches that correspond to individual form submissions.
114+
#
115+
# @return [Hash] hash of batches where the keys are a batch's `form_uuid`
116+
# and the value is a list of `IvcChampvaForm`s with that form_uuid
117+
# e.g.:
118+
# {
119+
# 'ad6c9181-530c-4a8f-9fbd-5e8e8a400d4a': [<IvcChampvaForm>, <IvcChampvaForm>]
120+
# 'fac6a892-530c-8a40-8afc-9fbd8ad8a40a': [<IvcChampvaForm>]
121+
# }
122+
#
123+
def get_nil_batches
124+
all_nil_statuses = IvcChampvaForm.where(pega_status: nil).where('created_at < ?', 1.minute.ago)
125+
126+
batches = {}
127+
128+
# Group all nil results into batches by form UUID
129+
all_nil_statuses.map do |el|
130+
batch = IvcChampvaForm.where(form_uuid: el.form_uuid)
131+
batches[el.form_uuid] = batch
132+
end
133+
134+
batches
135+
end
136+
109137
def fetch_forms_by_uuid(form_uuid)
110138
@fetch_forms_by_uuid ||= IvcChampvaForm.where(form_uuid:)
111139
end

modules/ivc_champva/spec/jobs/missing_form_status_job_spec.rb

+28-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def days_since_now(created_at)
1616

1717
before do
1818
allow(Settings.ivc_forms.sidekiq.missing_form_status_job).to receive(:enabled).and_return(true)
19+
allow(Flipper).to receive(:enabled?).with(:champva_vanotify_custom_callback, @current_user).and_return(true)
1920
allow(StatsD).to receive(:gauge)
2021
allow(StatsD).to receive(:increment)
2122

@@ -24,12 +25,14 @@ def days_since_now(created_at)
2425
track_failed_send_zsf_notification_to_pega: nil))
2526
# Save the original form creation times so we can restore them later
2627
@original_creation_times = forms.map(&:created_at)
28+
@original_uuids = forms.map(&:form_uuid)
2729
end
2830

2931
after do
30-
# Restore original form creation times for time sensitive tests
32+
# Restore original dummy form created_at/form_uuid props in case we've adjusted them
3133
forms.each_with_index do |form, index|
3234
form.update(created_at: @original_creation_times[index])
35+
form.update(form_uuid: @original_uuids[index])
3336
end
3437
end
3538

@@ -105,6 +108,30 @@ def days_since_now(created_at)
105108
expect(StatsD).to have_received(:gauge).with('ivc_champva.forms_missing_status.count', forms.count - 1)
106109
end
107110

111+
it 'processes nil forms in batches that belong to the same submission' do
112+
# Set shared `form_uuid` so these two now belong to the same batch:
113+
forms[0].update(form_uuid: '78444a0b-3ac8-454d-a28d-8d63cddd0d3b')
114+
forms[1].update(form_uuid: '78444a0b-3ac8-454d-a28d-8d63cddd0d3b')
115+
116+
# Perform the job that checks form statuses
117+
job.perform
118+
119+
# Check that we processed batches rather than individual forms:
120+
expect(StatsD).to have_received(:gauge).with('ivc_champva.forms_missing_status.count', forms.count - 1)
121+
end
122+
123+
it 'groups nil statuses into batches by uuid' do
124+
# Set shared `form_uuid` so these two now belong to the same batch:
125+
forms[0].update(form_uuid: '78444a0b-3ac8-454d-a28d-8d63cddd0d3b')
126+
forms[1].update(form_uuid: '78444a0b-3ac8-454d-a28d-8d63cddd0d3b')
127+
128+
# Perform the job that checks form statuses
129+
batches = job.get_nil_batches
130+
131+
expect(batches.count == forms.count - 1).to be true
132+
expect(batches['78444a0b-3ac8-454d-a28d-8d63cddd0d3b'].count == 2).to be true
133+
end
134+
108135
it 'sends the count of forms to DataDog' do
109136
IvcChampva::MissingFormStatusJob.new.perform
110137

0 commit comments

Comments
 (0)