Class: FnordMetric::Metric

Inherits:
Object
  • Object
show all
Defined in:
lib/fnordmetric/metric.rb

Direct Known Subclasses

AverageMetric, CombineMetric, CountMetric, SumMetric

Constant Summary collapse

METRIC_TYPES =
%w(count average sum combine)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Metric

Returns a new instance of Metric.



13
14
15
# File 'lib/fnordmetric/metric.rb', line 13

def initialize(options)
  @options = options
end

Class Method Details

.from_options(options) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fnordmetric/metric.rb', line 5

def self.from_options(options)
  if (klass_name = METRIC_TYPES.detect{ |n| !!options[n.intern] })
    klass = "FnordMetric::#{klass_name.classify}Metric".constantize
    return klass.new(options)
  end
  raise "please provide one of these options: average, sum, count, combine"
end

Instance Method Details

#at(time_or_range) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/fnordmetric/metric.rb', line 21

def at(time_or_range)    
  if cache_this?(time_or_range) && (_v=try_cache(time_or_range))
    _v # cache hit
  else # cache miss
    value_at(time_or_range).tap do |_v| 
      store_cache(time_or_range, _v) if cache_this?(time_or_range)
    end
  end
end

#currentObject



17
18
19
# File 'lib/fnordmetric/metric.rb', line 17

def current
  self.at(Time.now)
end

#eventsObject



35
36
37
38
39
40
41
# File 'lib/fnordmetric/metric.rb', line 35

def events
  _events = FnordMetric::Event
  if @options[:types]
    _events = _events.where(:type.in => [@options[:types]].flatten) 
  end
  _events
end

#events_at(time_or_range) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/fnordmetric/metric.rb', line 43

def events_at(time_or_range)
  if time_or_range.is_a?(Range)
    events.where(:time.lt => time_or_range.last.to_i).where(:time.gt => time_or_range.first.to_i)
  else
    events.where(:time.lt => time_or_range.to_i)
  end
end

#tokenObject



31
32
33
# File 'lib/fnordmetric/metric.rb', line 31

def token
  @options[:name]
end