Skip to content

Commit

Permalink
Penison 91844 Failed Benefits Intake Remediation Reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
wayne-weibel committed Sep 5, 2024
1 parent 4d2ca74 commit 9850890
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
98 changes: 98 additions & 0 deletions app/sidekiq/benefits_intake_remediation_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# frozen_string_literal: true

require 'lighthouse/benefits_intake/service'

class BenefitsIntakeRemediationJob
include Sidekiq::Job

sidekiq_options retry: false

STATS_KEY = 'api.benefits_intake.remediation_status'
BATCH_SIZE = Settings.lighthouse.benefits_intake.report.batch_size || 1000

attr_reader :batch_size

def initialize(batch_size: BATCH_SIZE)
@batch_size = batch_size
end

def perform
Rails.logger.info('BenefitsIntakeRemediationJob started')


Check failure on line 22 in app/sidekiq/benefits_intake_remediation_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/EmptyLines: Extra blank line detected.
start_dates = FormSubmission.group(:form_type).minimum(:created_at)

Check failure on line 23 in app/sidekiq/benefits_intake_remediation_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Lint/UselessAssignment: Useless assignment to variable - `start_dates`.
failed = FormSubmission.joins(:form_submission_attempts).where(form_type: '21P-527EZ', form_submission_attempts: {aasm_state: 'failure'})

Check failure on line 24 in app/sidekiq/benefits_intake_remediation_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Lint/UselessAssignment: Useless assignment to variable - `failed`.

Check failure on line 24 in app/sidekiq/benefits_intake_remediation_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/SpaceInsideHashLiteralBraces: Space inside { missing.

Check failure on line 24 in app/sidekiq/benefits_intake_remediation_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/LineLength: Line is too long. [141/120]

Check failure on line 24 in app/sidekiq/benefits_intake_remediation_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/SpaceInsideHashLiteralBraces: Space inside } missing.

pending_form_submissions = FormSubmission
.joins(:form_submission_attempts)
.where(form_submission_attempts: { aasm_state: 'pending' })
total_handled, result = batch_process(pending_form_submissions)

Rails.logger.info('BenefitsIntakeRemediationJob ended', total_handled:) if result
end

private

def batch_process(pending_form_submissions)
total_handled = 0
intake_service = BenefitsIntake::Service.new

pending_form_submissions.each_slice(batch_size) do |batch|
batch_uuids = batch.map(&:benefits_intake_uuid)
response = intake_service.bulk_status(uuids: batch_uuids)
raise response.body unless response.success?

total_handled += handle_response(response, batch)
end

[total_handled, true]
rescue => e
Rails.logger.error('Error processing Intake Status batch', class: self.class.name, message: e.message)
[total_handled, false]
end

# rubocop:disable Metrics/MethodLength
def handle_response(response, pending_form_submissions)
total_handled = 0

response.body['data']&.each do |submission|
uuid = submission['id']
form_submission = pending_form_submissions.find do |submission_from_db|
submission_from_db.benefits_intake_uuid == uuid
end
form_id = form_submission.form_type

form_submission_attempt = form_submission.latest_pending_attempt

# https://developer.va.gov/explore/api/benefits-intake/docs
status = submission.dig('attributes', 'status')
if %w[error expired].include?(status)
# Error - Indicates that there was an error. Refer to the error code and detail for further information.
# Expired - Indicate that documents were not successfully uploaded within the 15-minute window.
form_submission_attempt.fail!
log_result('failure', form_id, uuid, time_to_transition)
elsif status == 'vbms'
# submission was successfully uploaded into a Veteran's eFolder within VBMS
form_submission_attempt.vbms!
log_result('success', form_id, uuid, time_to_transition)
elsif time_to_transition > STALE_SLA.days
# exceeds SLA (service level agreement) days for submission completion
log_result('stale', form_id, uuid, time_to_transition)
else
# no change being tracked
log_result('pending', form_id, uuid)
end

total_handled += 1
end

total_handled
end
# rubocop:enable Metrics/MethodLength

def log_result(result, form_id, uuid, time_to_transition = nil)
StatsD.increment("#{STATS_KEY}.#{form_id}.#{result}")
StatsD.increment("#{STATS_KEY}.all_forms.#{result}")
Rails.logger.info('BenefitsIntakeStatusJob', result:, form_id:, uuid:, time_to_transition:)
end
end
5 changes: 4 additions & 1 deletion lib/periodic_jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@
# Update static data cache
mgr.register('0 0 * * *', 'Crm::TopicsDataJob')

# Update static data cache for form 526
# Update FormSubmissionAttempt status from Lighthouse Benefits Intake API
mgr.register('0 0 * * *', 'BenefitsIntakeStatusJob')

# Generate FormSubmissionAttempt rememdiation statistics from Lighthouse Benefits Intake API
mgr.register('0 0 * * *', 'BenefitsIntakeRemediationJob')

# Update Lighthouse526DocumentUpload statuses according to Lighthouse Benefits Documents service tracking
mgr.register('15 * * * *', 'Form526DocumentUploadPollingJob')

Expand Down

0 comments on commit 9850890

Please sign in to comment.