Module: PlantWatchdog::Aggregation::Methods

Defined in:
lib/plantwatchdog/aggregation_methods.rb

Class Method Summary collapse

Class Method Details

.addObject



65
66
67
# File 'lib/plantwatchdog/aggregation_methods.rb', line 65

def add
  a + b
end

.avg(timeseries) ⇒ Object



38
39
40
41
# File 'lib/plantwatchdog/aggregation_methods.rb', line 38

def avg timeseries
  result = sum(timeseries)
  result.to_f / timeseries.size
end

.call(method, *args) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/plantwatchdog/aggregation_methods.rb', line 73

def call(method, *args)
  begin
    m = Methods.method method
    m.call *args
  rescue
    logger.debug("Error calling method '#{method}': " + $!.to_s)
    nil
  end
end

.div(a, b) ⇒ Object



57
58
59
# File 'lib/plantwatchdog/aggregation_methods.rb', line 57

def div a,b
  a/b
end

.growth(timeseries) ⇒ Object



34
35
36
# File 'lib/plantwatchdog/aggregation_methods.rb', line 34

def growth timeseries
  timeseries.last - timeseries.first
end

.integrate(times, values) ⇒ Object



43
44
45
# File 'lib/plantwatchdog/aggregation_methods.rb', line 43

def integrate times, values
  return [times.each_prior {|x,y| y-x}, values.each_prior {|x,y| (y+x)/2.0}].transpose.inject(0) {|i,a| i + a.first * a.last }
end

.loggerObject



83
84
85
# File 'lib/plantwatchdog/aggregation_methods.rb', line 83

def logger
  return ActiveRecord::Base.logger
end

.mult(a, b) ⇒ Object



53
54
55
# File 'lib/plantwatchdog/aggregation_methods.rb', line 53

def mult a,b
  a*b
end

.pick(n, a) ⇒ Object



69
70
71
# File 'lib/plantwatchdog/aggregation_methods.rb', line 69

def pick n,a
  a[n]
end

.subtract(a, b) ⇒ Object



61
62
63
# File 'lib/plantwatchdog/aggregation_methods.rb', line 61

def subtract a,b
  a - b
end

.sum(timeseries) ⇒ Object



47
48
49
50
51
# File 'lib/plantwatchdog/aggregation_methods.rb', line 47

def sum timeseries
  result = 0
  timeseries.each { |v| result += v if v}
  return result
end