Class: Tronprint::Aggregator

Inherits:
Delegator
  • Object
show all
Defined in:
lib/tronprint/aggregator.rb

Overview

Tronprint::Aggregator stores aggregate application statistics to some sort of persistent key/value store. By default, this is a YAML file, but any key/value store supported by moneta will work.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Aggregator) initialize(options = {})

Initialize the Aggregator with the following options:

adapter

The underscore-ized name of the moneta class to use.

All other options are passed to the moneta adapter. You'll have to read moneda's source code for options needed by your desired adapter.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tronprint/aggregator.rb', line 20

def initialize(options = {})
  self.adapter = options.delete :adapter
  self.adapter ||= 'pstore'
  self.adapter = self.adapter.to_s.downcase

  options = process_options(options)

  begin
    require "moneta/#{self.adapter}"
    klass = Moneta.const_get adapter_constant
  rescue LoadError # Bundler hack
    require "moneta/adapters/#{self.adapter}"
    klass = Moneta::Adapters.const_get adapter_constant
  end
  args = self.adapter == 'memory' ? [] : [options]
  instance = klass.new(*args)
  __setobj__ instance  # required in Ruby 1.8.7
  super instance
end

Instance Attribute Details

- (Object) adapter

Returns the value of attribute adapter



12
13
14
# File 'lib/tronprint/aggregator.rb', line 12

def adapter
  @adapter
end

Instance Method Details

- (Object) __getobj__

:nodoc:



50
51
52
# File 'lib/tronprint/aggregator.rb', line 50

def __getobj__ # :nodoc:
  @delegate_sd_obj
end

- (Object) __setobj__(obj)

:nodoc:



53
54
55
# File 'lib/tronprint/aggregator.rb', line 53

def __setobj__(obj) # :nodoc:
  @delegate_sd_obj = obj
end

- (Object) adapter_constant

The class name of the desired moneta adapter



58
59
60
61
62
63
64
65
66
# File 'lib/tronprint/aggregator.rb', line 58

def adapter_constant
  case self.adapter
  when 'pstore' then 'PStore'
  when 'yaml' then 'YAML'
  when 'mongodb' then 'MongoDB'
  else
    self.adapter.split('_').map(&:capitalize).join('')
  end
end

- (Object) path(*args)



78
79
80
# File 'lib/tronprint/aggregator.rb', line 78

def path(*args)
  args.join('/')
end

- (Object) process_options(options)

Handle initializer options for some special cases



41
42
43
44
45
46
47
48
# File 'lib/tronprint/aggregator.rb', line 41

def process_options(options)
  if adapter == 'mongodb'
    options[:pool_size] = 3
    options[:timeout] = 15
    options[:op_timeout] = 10
  end
  options
end

- (Object) range_total(key, from, to)



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/tronprint/aggregator.rb', line 82

def range_total(key, from, to)
  raise "Invalid range" if from > to
  total = 0
  current = from
  while current <= to
    hourly_key = hourly_path(key, year(current), month(current), day(current), hour(current))
    total += self[hourly_key].to_f
    current = current + 3600
  end

  total
end

- (Object) update(key, value)

Increment the total statistic by the given value, specified by the given key.



70
71
72
73
74
75
76
# File 'lib/tronprint/aggregator.rb', line 70

def update(key, value)
  update_total(key, value)
  update_yearly(key, value)
  update_monthly(key, value)
  update_daily(key, value)
  update_hourly(key, value)
end