Files

PlantWatchdog::Aggregation::Methods

This module contains those methods which can be referenced from aggregation rules.

Public Instance Methods

add(a,b) click to toggle source

The sum of two numbers

# File lib/plantwatchdog/aggregation_methods.rb, line 144
      def add a,b
        a + b
      end
avg(timeseries) click to toggle source

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(method, *args) click to toggle source

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
div(a,b) click to toggle source

The ratio of two numbers.

# File lib/plantwatchdog/aggregation_methods.rb, line 134
      def div a,b
        a/b
      end
growth(timeseries) click to toggle source

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(times, values) click to toggle source

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
mult(a,b) click to toggle source

The product of two numbers

# File lib/plantwatchdog/aggregation_methods.rb, line 129
      def mult a,b
        a*b
      end
pick(n,a) click to toggle source

Pick the n_th_ entry out of array a.

# File lib/plantwatchdog/aggregation_methods.rb, line 149
      def pick n,a
        a[n]
      end
subtract(a,b) click to toggle source

The difference between two numbers.

# File lib/plantwatchdog/aggregation_methods.rb, line 139
      def subtract a,b
        a - b
      end
sum(timeseries) click to toggle source

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

Private Instance Methods

logger() click to toggle source

(Not documented)

# File lib/plantwatchdog/aggregation_methods.rb, line 165
      def logger
        return ActiveRecord::Base.logger
      end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.