Class: RCS::Stats
- Inherits:
-
Object
- Object
- RCS::Stats
- Defined in:
- lib/rcs-common/stats.rb
Instance Method Summary collapse
- #add(hash) ⇒ Object
- #add_to_section(section, k, v) ⇒ Object
- #calculate ⇒ Object
- #calculate_section(section) ⇒ Object
-
#initialize ⇒ Stats
constructor
A new instance of Stats.
- #sample_limit(index) ⇒ Object
- #stats(section = nil) ⇒ Object
Constructor Details
#initialize ⇒ Stats
Returns a new instance of Stats.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rcs-common/stats.rb', line 10 def initialize @stats = {} # the template can be provided by the subclass @template ||= Hash.new(0) # total statistics ever @stats[:total] = @template.dup # remember the time when the total count was started @stats[:total][:start] = Time.now.getutc # sections to keep stats for # the values are the tipping point @sections ||= {:minutes => 0, :hours => 60, :days => 24, :weeks => 7} # create the templates for each section @sections.each_key do |section| @stats[section] = {} @stats[section][:last] = Array.new(5) { @template.dup } @stats[section][:average] = @template.dup @stats[section][:average][:samples] = 0 end end |
Instance Method Details
#add(hash) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rcs-common/stats.rb', line 40 def add(hash) # for each key sum up the counters in the total and in the current minute hash.each_pair do |k, v| next if @stats[:total][k].nil? or v.nil? @stats[:total][k] += v @sections.keys.each do |section| add_to_section section, k, v end end end |
#add_to_section(section, k, v) ⇒ Object
51 52 53 54 |
# File 'lib/rcs-common/stats.rb', line 51 def add_to_section(section, k, v) @stats[section][:last][0][k] ||= 0 @stats[section][:last][0][k] += v end |
#calculate ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/rcs-common/stats.rb', line 56 def calculate calculate_section @sections.keys.first # each value in the @sections is the number of element of the previous section # needed to form the current section @sections.keys.each_with_index do |section, index| calculate_section(section) if sample_limit(index) end end |
#calculate_section(section) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rcs-common/stats.rb', line 66 def calculate_section(section) # calculate the average for this section @stats[section][:last].first.each_pair do |k, v| @stats[section][:average][k] = ((@stats[section][:average][k].to_f * @stats[section][:average][:samples] + v) / (@stats[section][:average][:samples] + 1)).round(2) end @stats[section][:average][:samples] += 1 # initialize a new element for the current section @stats[section][:last].insert(0, @template.dup) # remove the last one @stats[section][:last].pop end |
#sample_limit(index) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rcs-common/stats.rb', line 78 def sample_limit(index) # tipping point of 0 means never tip return false if @sections[@sections.keys[index]] == 0 # all the previous must be on the tipping point values = [] index.downto(1) do |i| prev = @sections.keys[i - 1] values[i] = @stats[prev][:average][:samples] != 0 && @stats[prev][:average][:samples] % @sections[@sections.keys[i]] == 0 end values.compact.inject(:&) end |
#stats(section = nil) ⇒ Object
36 37 38 |
# File 'lib/rcs-common/stats.rb', line 36 def stats(section = nil) section ? @stats[section] : @stats end |