Skip to content

Commit

Permalink
Refactor UpdateDocumentsStatusService for new logging approach
Browse files Browse the repository at this point in the history
Simplifies the UpdateDocumentsStatusService to use the simplified API of the UploadStatusUpdater. Makes the service responsible for passing the Lighthouse response metadata to the updater class, and logging to statsd metrics based on the outcome
  • Loading branch information
NB28VT committed Mar 6, 2024
1 parent 108091b commit 4d6e21d
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,43 @@ module BenefitsDocuments
module Form526
class UpdateDocumentsStatusService
# Queries the Lighthouse Benefits Document API's uploads/status endpoint
# to check the progression of LighthouseDocumentUploads we uploaded to Lighthouse.
# to check the progression of Lighthouse526DocumentUploads we uploaded to Lighthouse.
#
# After these documents are submitted to Lighthouse, Lighthouse submits them to VBMS,
# and then if that is successful, to BGS as well. All of these steps must complete before
# we consider a document successfully processed. Whenever we submit a document to Lighthouse,
# they return a 'request_id' corresponding to that document. We save this as lighthouse_document_request_id
# on a LighthouseDocumentUpload record.
# on a Lighthouse526DocumentUpload record.
#
# Lighthouse has provided us with an endpoint, uploads/status, which takes an array of request ids and
# returns a detailed JSON representiation of that document's progress in uploading to VBMS and BGS,
# including failures. This service class uses that data to update the status of a LighthouseDocumentUpload,
# and log success and failure metrics to StatsD, where we will use them to drive dashboards
# including failures. This service class uses that data to update the status of a Lighthouse526DocumentUpload,
# and log success, failure and timeout metrics to StatsD, where we will use them to drive dashboards
#
# Documentation for Lighthouse's Benefits Document API:
# https://dev-developer.va.gov/explore/api/benefits-documents/docs?version=current

STATSD_BASE_KEY = 'api.form_526.lighthouse_document_upload_processing_status'

STATSD_DOCUMENT_COMPLETE_KEY = 'complete'
STATSD_DOCUMENT_VBMS_SUBMISSION_FAILED_KEY = 'vbms_submission_failed'
STATSD_DOCUMENT_BGS_SUBMISSION_FAILED_KEY = 'bgs_submission_failed'
STATSD_DOCUMENT_FAILED_KEY = 'failed'

STATSD_DOCUMENT_TYPE_KEY_MAP = {
LighthouseDocumentUpload::VETERAN_UPLOAD_DOCUMENT_TYPE => 'veteran_upload',
LighthouseDocumentUpload::BDD_INSTRUCTIONS_DOCUMENT_TYPE => 'bdd_instructions',
LighthouseDocumentUpload::FORM_0781_DOCUMENT_TYPE => 'form_0781',
LighthouseDocumentUpload::FORM_0781A_DOCUMENT_TYPE => 'form_0781a'
}
Lighthouse526DocumentUpload::VETERAN_UPLOAD_DOCUMENT_TYPE => 'veteran_upload',
Lighthouse526DocumentUpload::BDD_INSTRUCTIONS_DOCUMENT_TYPE => 'bdd_instructions',
Lighthouse526DocumentUpload::FORM_0781_DOCUMENT_TYPE => 'form_0781',
Lighthouse526DocumentUpload::FORM_0781A_DOCUMENT_TYPE => 'form_0781a'
}.freeze

def self.call(args)
new(args).call
end

# @param lighthouse_document_uploads [LighthouseDocumentUpload] a collection of
# LighthouseDocumentUpload records we are polling for status updates on Lighthouse's
# @param lighthouse526_document_uploads [Lighthouse526DocumentUpload] a collection of
# Lighthouse526DocumentUpload records we are polling for status updates on Lighthouse's
# uploads/status endpoint
def initialize(lighthouse_document_uploads)
@lighthouse_document_uploads = lighthouse_document_uploads
def initialize(lighthouse526_document_uploads)
@lighthouse526_document_uploads = lighthouse526_document_uploads
end

def call
Expand All @@ -54,7 +53,7 @@ def call
private

def update_documents_status
request_ids = @lighthouse_document_uploads.pluck(:lighthouse_document_request_id)
request_ids = @lighthouse526_document_uploads.pluck(:lighthouse_document_request_id)
response = BenefitsDocuments::Form526::DocumentsStatusPollingService.call(request_ids)

JSON.parse(response).dig('data', 'statuses').each do |document_progress|
Expand All @@ -63,21 +62,32 @@ def update_documents_status
end

def update_document_status(document_progress)
document_upload = @lighthouse_document_uploads.find_by(lighthouse_document_request_id: document_progress['requestId'])
document_status_updater = BenefitsDocuments::Form526::UploadStatusUpdater.new(document_progress, document_upload)
document_upload = @lighthouse526_document_uploads.find_by!(
lighthouse_document_request_id: document_progress['requestId']
)

# UploadStatusUpdater encapsulates all parsing of a status response from Lighthouse
status_updater = BenefitsDocuments::Form526::UploadStatusUpdater.new(document_progress, document_upload)
status_updater.update_status

statsd_document_type_key = STATSD_DOCUMENT_TYPE_KEY_MAP[document_upload.document_type]
statsd_document_base_key = "#{STATSD_BASE_KEY}.#{statsd_document_type_key}"

if document_status_updater.completed?
if document_upload.completed?
# ex. 'api.form_526.lighthouse_document_upload_processing_status.bdd_instructions.complete'
StatsD.increment("#{statsd_document_base_key}.#{STATSD_DOCUMENT_COMPLETE_KEY}")
elsif document_status_updater.vbms_upload_failed?
StatsD.increment("#{statsd_document_base_key}.#{STATSD_DOCUMENT_VBMS_SUBMISSION_FAILED_KEY}")
elsif document_status_updater.bgs_upload_failed?
StatsD.increment("#{statsd_document_base_key}.#{STATSD_DOCUMENT_BGS_SUBMISSION_FAILED_KEY}")
end
elsif document_upload.failed?
# Since Lighthouse's processing steps are subject to change, we need to make these metrics dyanmic.
# E.g. at the moment this should return either claims_evidence or benefits_gateway_service
failure_step_key = status_updater.get_failure_step.downcase

document_status_updater.update_status
# ex. 'api.form_526.lighthouse_document_upload_processing_status.bdd_instructions.failed.claims_evidence'
StatsD.increment("#{statsd_document_base_key}.#{STATSD_DOCUMENT_FAILED_KEY}.#{failure_step_key}")
elsif status_updater.processing_timeout?
# Triggered when a document is still pending over 24 hours after it was started
# ex. 'api.form_526.lighthouse_document_upload_processing_status.bdd_instructions.processing_timeout'
StatsD.increment("#{statsd_document_base_key}.processing_timeout")
end
end
end
end
Expand Down
Loading

0 comments on commit 4d6e21d

Please sign in to comment.