Move time to complete stats into separate class

This separates out the calculations from the queries so we can get
decent tests around them.
This commit is contained in:
Katrina Owen 2013-03-02 12:04:45 -05:00
parent 0ebb98d49f
commit 62336f94cd
4 changed files with 142 additions and 50 deletions

View file

@ -8,23 +8,8 @@ module Stats
@user = user
end
def avg_ttc
@avg_ttc ||= (sum/count)/SECONDS_PER_DAY
end
def max_ttc
@max_ttc ||= max/SECONDS_PER_DAY
end
def min_ttc
@min_ttc ||= min/SECONDS_PER_DAY
end
def min_ttc_sec
min_ttc_sec = arbitrary_day + min # convert to a datetime
@actions_min_ttc_sec = (min_ttc_sec).strftime("%H:%M:%S")
@actions_min_ttc_sec = (min / SECONDS_PER_DAY).round.to_s + " days " + @actions_min_ttc_sec if min > SECONDS_PER_DAY
@actions_min_ttc_sec
def ttc
@ttc ||= TimeToComplete.new(completed)
end
def done_last30days
@ -69,10 +54,6 @@ module Stats
private
def arbitrary_day
@arbitrary_day ||= Time.utc(2000,1,1,0,0)
end
def one_year
@one_year ||= 12.months.ago.beginning_of_day
end
@ -81,32 +62,6 @@ module Stats
@one_month ||= 1.month.ago.beginning_of_day
end
def completed
@completed ||= user.todos.completed.select("completed_at, created_at")
end
def durations
@durations ||= completed.map do |r|
(r.completed_at - r.created_at)
end
end
def sum
@sum ||= durations.inject(0) {|sum, d| sum + d}
end
def min
@min ||= durations.min || 0
end
def max
@max ||= durations.max || 0
end
def count
completed.empty? ? 1 : completed.size
end
def new_since(cutoff)
user.todos.created_after(cutoff).count
end
@ -115,5 +70,8 @@ module Stats
user.todos.completed.completed_after(cutoff).count
end
def completed
@completed ||= user.todos.completed.select("completed_at, created_at")
end
end
end