class Sail::Instrumenter
Class containing methods to instrument setting usage and provide insights to dashboard users.
Constants
- USAGES_UNTIL_CACHE_EXPIRE
Public Class Methods
new()
click to toggle source
initialize
Declare basic hash containing setting statistics
# File lib/sail/instrumenter.rb, line 16 def initialize @statistics = { settings: {}, profiles: {} }.with_indifferent_access @number_of_settings = Setting.count end
Public Instance Methods
[](name)
click to toggle source
-
Accessor method for setting statistics to guarantee proper initialization of hashes.
# File lib/sail/instrumenter.rb, line 25 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)
click to toggle source
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
# File lib/sail/instrumenter.rb, line 71 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)
click to toggle source
Increments the number of failures for settings while a profile is active
# File lib/sail/instrumenter.rb, line 34 def increment_profile_failure_of(name) @statistics[:profiles][name] ||= 0 @statistics[:profiles][name] += 1 end
increment_usage_of(setting_name)
click to toggle source
increment_usage
Simply increments the number of times a setting has been called
# File lib/sail/instrumenter.rb, line 50 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)
click to toggle source
profile
Profile statistics accessor
# File lib/sail/instrumenter.rb, line 42 def profile(name) @statistics[:profiles][name] ||= 0 end
relative_usage_of(setting_name)
click to toggle source
Calculates the relative usage of a setting compared to all others in percentage
# File lib/sail/instrumenter.rb, line 60 def relative_usage_of(setting_name) return 0.0 if @statistics[:settings].empty? (100.0 * self[setting_name][:usages]) / @statistics[:settings].sum { |_, entry| entry[:usages] } end
relevancy_of(setting_name)
click to toggle source
# File lib/sail/instrumenter.rb, line 80 def relevancy_of(setting_name) (relative_usage_of(setting_name) / @number_of_settings).round(1) end
Private Instance Methods
expire_cache_fragment(setting_name)
click to toggle source
# File lib/sail/instrumenter.rb, line 86 def expire_cache_fragment(setting_name) ActionController::Base.new.expire_fragment(/name: "#{setting_name}"/) end