Created
January 3, 2013 22:52
-
-
Save spacecowb0y/4448232 to your computer and use it in GitHub Desktop.
Quick code to get the most active days by hour.
This file contains 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
week = Time.now.beginning_of_week - 1.week | |
days = [week.strftime('%Y-%m-%d')] | |
1.upto(6) do |i| | |
days << (week + i.days).strftime('%Y-%m-%d') | |
end | |
data = days.map do |day| | |
by_hour = Array.new(24) { 0 } | |
Activity. | |
where('date(created_at) = ?', day). | |
select('hour(created_at) as hour, count(*) as count'). | |
group('hour(created_at)'). | |
order('hour(created_at)'). | |
map { |activity| by_hour[activity.hour] = activity.count } | |
by_hour | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment