Skip to content

Instantly share code, notes, and snippets.

@spacecowb0y
Created January 3, 2013 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spacecowb0y/4448232 to your computer and use it in GitHub Desktop.
Save spacecowb0y/4448232 to your computer and use it in GitHub Desktop.
Quick code to get the most active days by hour.
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