Class: Form1010cg::Auditor

Inherits:
Object
  • Object
show all
Defined in:
app/services/form1010cg/auditor.rb

Constant Summary collapse

STATSD_KEY_PREFIX =
'api.form1010cg'
LOGGER_PREFIX =
'Form 10-10CG'
LOGGER_FILTER_KEYS =
[:veteran_name].freeze
METRICS =
lambda do
  submission_prefix = "#{STATSD_KEY_PREFIX}.submission"

  OpenStruct.new(
    submission: OpenStruct.new(
      attempt: "#{submission_prefix}.attempt",
      caregivers: OpenStruct.new(
        primary_no_secondary: "#{submission_prefix}.caregivers.primary_no_secondary",
        primary_one_secondary: "#{submission_prefix}.caregivers.primary_one_secondary",
        primary_two_secondary: "#{submission_prefix}.caregivers.primary_two_secondary",
        no_primary_one_secondary: "#{submission_prefix}.caregivers.no_primary_one_secondary",
        no_primary_two_secondary: "#{submission_prefix}.caregivers.no_primary_two_secondary"
      ),
      failure: OpenStruct.new(
        client: OpenStruct.new(
          data: "#{submission_prefix}.failure.client.data",
          qualification: "#{submission_prefix}.failure.client.qualification"
        ),
        attachments: "#{submission_prefix}.failure.attachments"
      )
    ),
    pdf_download: "#{STATSD_KEY_PREFIX}.pdf_download"
  )
end.call

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger = Rails.logger) ⇒ Auditor

Returns a new instance of Auditor.



40
41
42
# File 'app/services/form1010cg/auditor.rb', line 40

def initialize(logger = Rails.logger)
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



5
6
7
# File 'app/services/form1010cg/auditor.rb', line 5

def logger
  @logger
end

Class Method Details

.metricsObject



36
37
38
# File 'app/services/form1010cg/auditor.rb', line 36

def self.metrics
  METRICS
end

Instance Method Details

#deep_apply_filter(value) ⇒ Object (private)



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/services/form1010cg/auditor.rb', line 127

def deep_apply_filter(value)
  case value
  when Array
    value.map { |v| deep_apply_filter(v) }
  when Hash
    value.each_with_object({}) do |(key, v), result|
      result[key] = if LOGGER_FILTER_KEYS.include?(key.to_s) || LOGGER_FILTER_KEYS.include?(key.to_sym)
                      ActiveSupport::ParameterFilter::FILTERED
                    else
                      deep_apply_filter(v)
                    end
    end
  else
    value
  end
end

#increment(stat) ⇒ Object (private)



119
120
121
# File 'app/services/form1010cg/auditor.rb', line 119

def increment(stat)
  StatsD.increment stat
end

#increment_no_primary_caregiver_data(secondaries_count) ⇒ Object (private)



109
110
111
112
113
114
115
116
117
# File 'app/services/form1010cg/auditor.rb', line 109

def increment_no_primary_caregiver_data(secondaries_count)
  caregivers = self.class.metrics.submission.caregivers
  case secondaries_count
  when 1
    increment(caregivers.no_primary_one_secondary)
  when 2
    increment(caregivers.no_primary_two_secondary)
  end
end

#increment_primary_caregiver_data(secondaries_count) ⇒ Object (private)



97
98
99
100
101
102
103
104
105
106
107
# File 'app/services/form1010cg/auditor.rb', line 97

def increment_primary_caregiver_data(secondaries_count)
  caregivers = self.class.metrics.submission.caregivers
  case secondaries_count
  when 0
    increment(caregivers.primary_no_secondary)
  when 1
    increment(caregivers.primary_one_secondary)
  when 2
    increment(caregivers.primary_two_secondary)
  end
end

#log(message, context_hash = {}) ⇒ Object (private)



123
124
125
# File 'app/services/form1010cg/auditor.rb', line 123

def log(message, context_hash = {})
  logger.send(:info, "[#{LOGGER_PREFIX}] #{message}", **deep_apply_filter(context_hash))
end

#log_mpi_search_result(claim_guid:, form_subject:, result:) ⇒ Object



89
90
91
92
93
# File 'app/services/form1010cg/auditor.rb', line 89

def log_mpi_search_result(claim_guid:, form_subject:, result:)
  labels = { found: 'found', not_found: 'NOT FOUND', skipped: 'search was skipped' }
  result_label = labels[result]
  log("MPI Profile #{result_label} for #{form_subject.titleize}", { claim_guid: })
end

#record(event, **context) ⇒ Object



44
45
46
47
# File 'app/services/form1010cg/auditor.rb', line 44

def record(event, **context)
  message = "record_#{event}"
  context.any? ? send(message, **context) : send(message)
end

#record_attachments_delivered(claim_guid:, carma_case_id:, attachments:) ⇒ Object



80
81
82
83
84
85
86
87
# File 'app/services/form1010cg/auditor.rb', line 80

def record_attachments_delivered(claim_guid:, carma_case_id:, attachments:)
  log(
    'Attachments Delivered',
    claim_guid:,
    carma_case_id:,
    attachments:
  )
end

#record_caregivers(claim) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/services/form1010cg/auditor.rb', line 53

def record_caregivers(claim)
  secondaries_count = 0
  %w[one two].each do |attr|
    secondaries_count += 1 if claim.public_send("secondary_caregiver_#{attr}_data").present?
  end

  if claim.primary_caregiver_data.present?
    increment_primary_caregiver_data(secondaries_count)
  else
    increment_no_primary_caregiver_data(secondaries_count)
  end
end

#record_pdf_downloadObject



76
77
78
# File 'app/services/form1010cg/auditor.rb', line 76

def record_pdf_download
  increment self.class.metrics.pdf_download
end

#record_submission_attemptObject



49
50
51
# File 'app/services/form1010cg/auditor.rb', line 49

def record_submission_attempt
  increment self.class.metrics.submission.attempt
end

#record_submission_failure_client_data(errors:, claim_guid: nil) ⇒ Object



66
67
68
69
# File 'app/services/form1010cg/auditor.rb', line 66

def record_submission_failure_client_data(errors:, claim_guid: nil)
  increment self.class.metrics.submission.failure.client.data
  log 'Submission Failed: invalid data provided by client', claim_guid:, errors:
end

#record_submission_failure_client_qualification(claim_guid:) ⇒ Object



71
72
73
74
# File 'app/services/form1010cg/auditor.rb', line 71

def record_submission_failure_client_qualification(claim_guid:)
  increment self.class.metrics.submission.failure.client.qualification
  log 'Submission Failed: qualifications not met', claim_guid:
end