Class: VAProfile::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/va_profile/stats.rb

Constant Summary collapse

STATSD_KEY_PREFIX =
'api.va_profile'
FINAL_SUCCESS =
%w[COMPLETED_SUCCESS COMPLETED_NO_CHANGES_DETECTED].freeze
FINAL_FAILURE =
%w[REJECTED COMPLETED_FAILURE].freeze

Class Method Summary collapse

Class Method Details

.bucket_for(status) ⇒ Object (private)



68
69
70
71
72
73
74
# File 'lib/va_profile/stats.rb', line 68

def bucket_for(status)
  if success?(status)
    'success'
  elsif failure?(status)
    'failure'
  end
end

.failure?(status) ⇒ Boolean (private)

Returns:

  • (Boolean)


64
65
66
# File 'lib/va_profile/stats.rb', line 64

def failure?(status)
  FINAL_FAILURE.include? status
end

.final_status?(status) ⇒ Boolean (private)

Returns:

  • (Boolean)


56
57
58
# File 'lib/va_profile/stats.rb', line 56

def final_status?(status)
  status.present? && success?(status) || failure?(status)
end

.increment(*args) ⇒ Object

Triggers the associated StatsD.increment method for the VAProfile buckets that are initialized in the config/initializers/statsd.rb file.

Parameters:

  • *args (String)

    A variable number of string arguments. Each one represents a bucket in StatsD. For example passing in (‘policy’, ‘success’) would increment the ‘api.va_profile.policy.success’ bucket



19
20
21
22
23
# File 'lib/va_profile/stats.rb', line 19

def increment(*args)
  buckets = args.map(&:downcase).join('.')

  StatsD.increment("#{STATSD_KEY_PREFIX}.#{buckets}")
end

.increment_exception(key) ⇒ Object

Increments the associated StatsD bucket with the passed in exception error key.

Parameters:

  • key (String)

    A VAProfile exception key from the locales/exceptions file For example, ‘VET360_ADDR133’.



46
47
48
# File 'lib/va_profile/stats.rb', line 46

def increment_exception(key)
  StatsD.increment("#{STATSD_KEY_PREFIX}.exceptions", tags: ["exception:#{key.downcase}"])
end

.increment_transaction_results(response, bucket1 = 'posts_and_puts') ⇒ Nil

If the passed response contains a transaction status that is in one of the final success or failure states, it increments the associated StatsD bucket.

Parameters:

  • response (FaradayObject)

    The raw response from the Faraday HTTP call

  • bucket1 (String) (defaults to: 'posts_and_puts')

    The VAProfile bucket to increment. This bucket must already be initialized in config/initializers/statsd.rb.

Returns:

  • (Nil)

    Returns nil only if the passed transaction status is not a final status



33
34
35
36
37
38
39
# File 'lib/va_profile/stats.rb', line 33

def increment_transaction_results(response, bucket1 = 'posts_and_puts')
  status = status_in(response)

  return unless final_status?(status)

  increment(bucket1, bucket_for(status))
end

.status_in(response) ⇒ Object (private)



52
53
54
# File 'lib/va_profile/stats.rb', line 52

def status_in(response)
  response&.body&.dig('tx_status')&.upcase
end

.success?(status) ⇒ Boolean (private)

Returns:

  • (Boolean)


60
61
62
# File 'lib/va_profile/stats.rb', line 60

def success?(status)
  FINAL_SUCCESS.include? status
end