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 = {}.with_indifferent_access
end

Instance Method Details

#[](name) ⇒ Object

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



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

def [](name)
  @statistics[name] = { usages: 0, failures: 0 }.with_indifferent_access if @statistics[name].blank?
  @statistics[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



54
55
56
57
# File 'lib/sail/instrumenter.rb', line 54

def increment_failure_of(setting_name)
  self[setting_name][:failures] += 1
  Sail.reset(setting_name) if self[setting_name][:failures] > Sail.configuration.failures_until_reset
end

#increment_usage_of(setting_name) ⇒ Object

increment_usage

Simply increments the number of times a setting has been called



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

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

#relative_usage_of(setting_name) ⇒ Object

relative_usage_of

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



43
44
45
46
47
# File 'lib/sail/instrumenter.rb', line 43

def relative_usage_of(setting_name)
  return 0.0 if @statistics.empty?

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