Class: Sail::Instrumenter

Inherits:
Object
  • Object
show all
Defined in:
lib/sail/instrumenter.rb

Overview

Instrumenter

Class containing methods to instrument setting usage and provide insights to dashboard users.

Constant Summary collapse

USAGES_UNTIL_CACHE_EXPIRE =
500

Instance Method Summary collapse

Constructor Details

#initializeInstrumenter

initialize

Declare basic hash containing setting statistics



16
17
18
# File 'lib/sail/instrumenter.rb', line 16

def initialize
  @statistics = { settings: {}, profiles: {} }.with_indifferent_access
end

Instance Method Details

#[](name) ⇒ Object

Accessor method for setting statistics to guarantee proper initialization of hashes.



24
25
26
27
# File 'lib/sail/instrumenter.rb', line 24

def [](name)
  @statistics[:settings][name] = { usages: 0, failures: 0 }.with_indifferent_access if @statistics[:settings][name].blank?
  @statistics[:settings][name]
end

#increment_failure_of(setting_name) ⇒ Object

increment_failure_of

Counts the number of failed code block executions enveloped by a given setting. If the number of failures exceeds the amount configured, resets the setting value



70
71
72
73
74
75
76
77
# File 'lib/sail/instrumenter.rb', line 70

def increment_failure_of(setting_name)
  self[setting_name][:failures] += 1

  current_profile = Profile.current
  increment_profile_failure_of(current_profile.name) if current_profile

  Sail.reset(setting_name) if self[setting_name][:failures] > Sail.configuration.failures_until_reset
end

#increment_profile_failure_of(name) ⇒ Object

increment_profile_failure_of

Increments the number of failures for settings while a profile is active



33
34
35
36
# File 'lib/sail/instrumenter.rb', line 33

def increment_profile_failure_of(name)
  @statistics[:profiles][name] ||= 0
  @statistics[:profiles][name] += 1
end

#increment_usage_of(setting_name) ⇒ Object

increment_usage

Simply increments the number of times a setting has been called



49
50
51
52
# File 'lib/sail/instrumenter.rb', line 49

def increment_usage_of(setting_name)
  self[setting_name][:usages] += 1
  expire_cache_fragment(setting_name) if (self[setting_name][:usages] % USAGES_UNTIL_CACHE_EXPIRE).zero?
end

#profile(name) ⇒ Object

profile

Profile statistics accessor



41
42
43
# File 'lib/sail/instrumenter.rb', line 41

def profile(name)
  @statistics[:profiles][name] ||= 0
end

#relative_usage_of(setting_name) ⇒ Object

relative_usage_of

Calculates the relative usage of a setting compared to all others in percentage



59
60
61
62
63
# File 'lib/sail/instrumenter.rb', line 59

def relative_usage_of(setting_name)
  return 0.0 if @statistics[:settings].empty?

  (100.0 * self[setting_name][:usages]) / @statistics[:settings].map { |_, entry| entry[:usages] }.reduce(:+)
end