module SnowmanIO::Utils

Public Class Methods

avg(arr) click to toggle source
# File lib/snowman-io/utils.rb, line 11
def self.avg(arr)
  if arr.empty?
    0.0
  else
    arr.inject(:+).to_f/arr.length
  end
end
floor_5min(time) click to toggle source
# File lib/snowman-io/utils.rb, line 7
def self.floor_5min(time)
  Time.new(time.year, time.month, time.day, time.hour, (time.min/5)*5)
end
floor_5sec(time) click to toggle source
# File lib/snowman-io/utils.rb, line 3
def self.floor_5sec(time)
  Time.new(time.year, time.month, time.day, time.hour, time.min, (time.sec/5)*5)
end
up(arr) click to toggle source

`up` value is close to 90 percentile by meaning, but it slightly depends of every element (weighted avarage). This metric is especially usefull for small arrays.

# File lib/snowman-io/utils.rb, line 21
def self.up(arr)
  sorted = arr.sort
  if sorted.length <= 1
    sorted[0]
  else
    sum = 0
    amount = 0
    sorted.each_with_index { |e, i|
      w = i.to_f/sorted.length
      if w <= 0.9
        a = w + 0.1
      else
        a = 1.9 - w
      end
      sum += e*a
      amount += a
    }
    sum/amount
  end
end