2013-03-02 12:04:45 -05:00
|
|
|
module Stats
|
|
|
|
|
class TimeToComplete
|
|
|
|
|
|
|
|
|
|
SECONDS_PER_DAY = 86400;
|
|
|
|
|
|
|
|
|
|
attr_reader :actions
|
|
|
|
|
def initialize(actions)
|
|
|
|
|
@actions = actions
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def avg
|
2013-03-02 12:15:30 -05:00
|
|
|
@avg ||= to_days(sum / count)
|
2013-03-02 12:04:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def max
|
2013-03-02 12:15:30 -05:00
|
|
|
@max ||= to_days(max_in_seconds)
|
2013-03-02 12:04:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def min
|
2013-03-02 12:15:30 -05:00
|
|
|
@min ||= to_days(min_in_seconds)
|
2013-03-02 12:04:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def min_sec
|
|
|
|
|
min_sec = arbitrary_day + min_in_seconds # convert to a datetime
|
|
|
|
|
@min_sec = min_sec.strftime("%H:%M:%S")
|
2013-03-02 12:17:59 -05:00
|
|
|
@min_sec = min.floor.to_s + " days " + @min_sec if min >= 1
|
2013-03-02 12:04:45 -05:00
|
|
|
@min_sec
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2013-03-02 12:15:30 -05:00
|
|
|
def to_days(value)
|
|
|
|
|
(value * 10 / SECONDS_PER_DAY).round / 10.0
|
|
|
|
|
end
|
|
|
|
|
|
2013-03-02 12:04:45 -05:00
|
|
|
def min_in_seconds
|
|
|
|
|
@min_in_seconds ||= durations.min || 0
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def max_in_seconds
|
|
|
|
|
@max_in_seconds ||= durations.max || 0
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def count
|
|
|
|
|
actions.empty? ? 1 : actions.size
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def durations
|
|
|
|
|
@durations ||= actions.map do |r|
|
|
|
|
|
(r.completed_at - r.created_at)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def sum
|
|
|
|
|
@sum ||= durations.inject(0) {|sum, d| sum + d}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def arbitrary_day
|
|
|
|
|
@arbitrary_day ||= Time.utc(2000, 1, 1, 0, 0)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
end
|