Class: Stats::TimeToComplete

Inherits:
Object
  • Object
show all
Defined in:
app/models/stats/time_to_complete.rb

Constant Summary collapse

SECONDS_PER_DAY =
86400

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actions) ⇒ TimeToComplete

Returns a new instance of TimeToComplete


7
8
9
# File 'app/models/stats/time_to_complete.rb', line 7

def initialize(actions)
  @actions = actions
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions


6
7
8
# File 'app/models/stats/time_to_complete.rb', line 6

def actions
  @actions
end

Instance Method Details

#arbitrary_dayObject (private)


58
59
60
# File 'app/models/stats/time_to_complete.rb', line 58

def arbitrary_day
  @arbitrary_day ||= Time.utc(2000, 1, 1, 0, 0)
end

#avgObject


11
12
13
# File 'app/models/stats/time_to_complete.rb', line 11

def avg
  @avg ||= to_days(sum / count)
end

#countObject (private)


44
45
46
# File 'app/models/stats/time_to_complete.rb', line 44

def count
  actions.empty? ? 1 : actions.size
end

#durationsObject (private)


48
49
50
51
52
# File 'app/models/stats/time_to_complete.rb', line 48

def durations
  @durations ||= actions.map do |r|
    (r.completed_at - r.created_at)
  end
end

#maxObject


15
16
17
# File 'app/models/stats/time_to_complete.rb', line 15

def max
  @max ||= to_days(max_in_seconds)
end

#max_in_secondsObject (private)


40
41
42
# File 'app/models/stats/time_to_complete.rb', line 40

def max_in_seconds
  @max_in_seconds ||= durations.max || 0
end

#minObject


19
20
21
# File 'app/models/stats/time_to_complete.rb', line 19

def min
  @min ||= to_days(min_in_seconds)
end

#min_in_secondsObject (private)


36
37
38
# File 'app/models/stats/time_to_complete.rb', line 36

def min_in_seconds
  @min_in_seconds ||= durations.min || 0
end

#min_secObject


23
24
25
26
27
28
# File 'app/models/stats/time_to_complete.rb', line 23

def min_sec
  min_sec = arbitrary_day + min_in_seconds # convert to a datetime
  @min_sec = min_sec.strftime("%H:%M:%S")
  @min_sec = min.floor.to_s + " days " + @min_sec if min >= 1
  @min_sec
end

#sumObject (private)


54
55
56
# File 'app/models/stats/time_to_complete.rb', line 54

def sum
  @sum ||= durations.inject(0) {|sum, d| sum + d}
end

#to_days(value) ⇒ Object (private)


32
33
34
# File 'app/models/stats/time_to_complete.rb', line 32

def to_days(value)
  (value * 10 / SECONDS_PER_DAY).round / 10.0
end