This module contains those methods which can be referenced from aggregation rules.
The sum of two numbers
# File lib/plantwatchdog/aggregation_methods.rb, line 144 def add a,b a + b end
The average of a timeseries.
$ irb >> require 'plantwatchdog/aggregation_methods' >> include PlantWatchdog::Aggregation::Methods >> avg([]) => NaN >> avg([1]) => 1.0 >> avg([1,2]) => 1.5 >> avg([1,2,3]) => 2.0
# File lib/plantwatchdog/aggregation_methods.rb, line 85 def avg timeseries result = sum(timeseries) result.to_f / timeseries.size end
Call aggregation method method with arguments
# File lib/plantwatchdog/aggregation_methods.rb, line 154 def call(method, *args) begin m = self.method method m.call *args rescue logger.debug("Error calling method '#{method}': " + $!.to_s) nil end end
The ratio of two numbers.
# File lib/plantwatchdog/aggregation_methods.rb, line 134 def div a,b a/b end
Calculate the difference between the last and first value of a list. Useful to find the growth within a list of measurements where every entry is already aggregated.
$ irb >> require 'plantwatchdog/aggregation_methods' >> include PlantWatchdog::Aggregation::Methods >> growth([]) => 0 >> growth([1]) => 0 >> growth([1,2]) => 1 >> growth([1,2,3]) => 2
# File lib/plantwatchdog/aggregation_methods.rb, line 67 def growth timeseries return 0 if timeseries.empty? timeseries.last - timeseries.first end
Integrate the values over time.
$ irb >> require 'plantwatchdog/aggregation_methods' >> include PlantWatchdog::Aggregation::Methods >> integrate([],[]) => 0 >> integrate([0],[0]) => 0 >> integrate([0,1],[0,1]) => 0.5 >> integrate([0,2],[0,2]) => 2.0 >> integrate([0,1,2],[0,1,2]) => 2.0 >> integrate([0,1,2],[1,2,3]) => 4.0
# File lib/plantwatchdog/aggregation_methods.rb, line 107 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
The product of two numbers
# File lib/plantwatchdog/aggregation_methods.rb, line 129 def mult a,b a*b end
Pick the n_th_ entry out of array a.
# File lib/plantwatchdog/aggregation_methods.rb, line 149 def pick n,a a[n] end
The difference between two numbers.
# File lib/plantwatchdog/aggregation_methods.rb, line 139 def subtract a,b a - b end
Sum up all entries of an array which can contain nil values.
$ irb >> require 'plantwatchdog/aggregation_methods' >> include PlantWatchdog::Aggregation::Methods >> sum([]) => 0 >> sum([1]) => 1 >> sum([1,nil]) => 1 >> sum([1,2]) => 3
# File lib/plantwatchdog/aggregation_methods.rb, line 124 def sum timeseries timeseries.inject(0) { |a,v| v.nil? ? a : a += v } end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.