Class: Bodhi::Metric

Inherits:
Object
  • Object
show all
Extended by:
AggregateMetrics, CountMetrics
Includes:
Enumerable
Defined in:
lib/bodhi/metric.rb

Constant Summary collapse

@@metrics =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AggregateMetrics

aggregate, average, maximum, minimum, sum

Constructor Details

#initialize(params) ⇒ Metric

Returns a new instance of Metric.


38
39
40
41
42
# File 'lib/bodhi/metric.rb', line 38

def initialize (params)
  @name = params[:name]
  @model_name = params[:model_name]
  @block = params[:block]
end

Instance Attribute Details

#blockObject

Returns the value of attribute block


36
37
38
# File 'lib/bodhi/metric.rb', line 36

def block
  @block
end

#model_nameObject

Returns the value of attribute model_name


36
37
38
# File 'lib/bodhi/metric.rb', line 36

def model_name
  @model_name
end

#nameObject

Returns the value of attribute name


36
37
38
# File 'lib/bodhi/metric.rb', line 36

def name
  @name
end

Class Method Details

.[](key) ⇒ Object


24
25
26
# File 'lib/bodhi/metric.rb', line 24

def self.[] (key)
  @@metrics[key]
end

.allObject


28
29
30
# File 'lib/bodhi/metric.rb', line 28

def self.all
  @@metrics.each_value
end

.define(*args, &block) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bodhi/metric.rb', line 12

def self.define (*args, &block)
  options = args.extract_options!
  
  options[:name] = args[0] if args[0].is_a? Symbol
  options[:block] ||= block if block

  m = Metric.new (options)
  @@metrics[m.name] = m

  m
end

.generate_allObject


32
33
34
# File 'lib/bodhi/metric.rb', line 32

def self.generate_all
  @@metrics.each_value { |m| m.generate }
end

Instance Method Details

#currentObject


44
45
46
# File 'lib/bodhi/metric.rb', line 44

def current
  @block.call(get_run_opts.merge(:start => Time.now.beginning_of_day, :end => Time.now.end_of_day))
end

#for_period(start_date, end_date = Time.now) ⇒ Object


48
49
50
# File 'lib/bodhi/metric.rb', line 48

def for_period (start_date, end_date = Time.now)
  MetricValue.where(:metric => @name).where("start between ? and ?", start_date, end_date)
end

#generateObject


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bodhi/metric.rb', line 52

def generate
  # Remove any runs from today 
  MetricValue.where(:metric => self.name).where("start >= ?", Date.today).destroy_all

  last_run = MetricValue.maximum(:end, :conditions => ["metric = '#{self.name}'"]) || Date.yesterday
  
  while last_run < Date.today
    start_date = (last_run + 1.day).beginning_of_day
    end_date = start_date + 1.day - 1.second
    value = @block.call(get_run_opts.merge(:start => start_date, :end => end_date))

    MetricValue.create!(:start => start_date, :end => end_date, :value => value, :metric => self.name)

    last_run = start_date
  end
end