Method: BBLib::TaskTimer#time

Defined in:
lib/bblib/core/classes/task_timer.rb

#time(task = :default, type = :current) ⇒ Float, ...

Returns an aggregated metric for a given type.

Parameters:

  • task (Symbol) (defaults to: :default)

    The key value of the task to retrieve

  • type (Symbol) (defaults to: :current)

    The metric to return. Options are :avg, :min, :max, :first, :last, :sum, :all and :count.

Returns:

  • (Float, Integer, Array)

    Returns either the aggregation (Numeric) or an Array in the case of :all.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bblib/core/classes/task_timer.rb', line 21

def time(task = :default, type = :current)
  return nil unless tasks.keys.include?(task)
  numbers = tasks[task][:history].map { |v| v[:time] }
  case type
  when :current
    return nil unless tasks[task][:current]
    Time.now.to_f - tasks[task][:current]
  when :min, :max, :first, :last
    numbers.send(type)
  when :avg
    numbers.size.zero? ? nil : numbers.inject { |sum, n| sum + n }.to_f / numbers.size
  when :sum
    numbers.inject { |sum, n| sum + n }
  when :all
    numbers
  when :count
    numbers.size
  end
end