Created
June 24, 2013 22:57
-
-
Save JamesDullaghan/5854487 to your computer and use it in GitHub Desktop.
Question to IRC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def index | |
# Sleep.all for current_user | |
@sleeps = current_user.sleeps | |
# Sleep grouped and sorted in order by month | |
@sleep_months = @sleeps.group_by { |s| s.beginning_of_month }.sort { |a,b| a[0] <=> b[0] } | |
@array_for_average_sleep_months = @sleep_months.map { |month, sleep| month.strftime("%b") } | |
@array_for_hours_slept_per_month = ????? | |
# ????? for array of hours of sleep per month to | |
@monthly_average_sleep_time = median(@array_for_hours_slept_per_month) | |
raise | |
respond_to do |format| | |
format.html { render action: 'index' } | |
format.js | |
end | |
end | |
def median(array) | |
if array.empty? | |
current_user.sleeps.new | |
else | |
sorted = array.sort | |
len = sorted.length | |
return (sorted[(len - 1) / 2] + sorted[len / 2]) / 2.0 | |
end | |
end | |
# data looks like this from @sleep_months: | |
#[[Tue, 01 Jan 2013, [#<Sleep id: 10, user_id: 1, hours: 6, created_at: "2013-06-24 02:35:15", updated_at: "2013-06-24 02:35:15", sleep_date: "2013-01-01">]], [Fri, 01 Feb 2013, [#<Sleep id: 11, user_id: 1, hours: 9, created_at: "2013-06-24 02:36:38", updated_at: "2013-06-24 02:36:38", sleep_date: "2013-02-06">, #<Sleep id: 15, user_id: 1, hours: 7, created_at: "2013-06-24 02:38:31", updated_at: "2013-06-24 02:38:31", sleep_date: "2013-02-20">]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment