Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for grouping by every N number of seconds/minutes/etc. #23

Closed
tybro0103 opened this issue Oct 14, 2013 · 13 comments
Closed

Comments

@tybro0103
Copy link

I don't currently see any way to group by something like every 10 minutes. Should be simple to add... just do a mod N after the date_trunc.

@jeffblake
Copy link

This would be fantastic! Right now I'm doing some crazy stuff and think a more SQL based refactor is in order.

 def self.checkin_array(event, minute_interval)
return if event.tickets.redeemed.empty?
tz = event.venue.time_zone_name
tx = event.tickets.redeemed.sort_by { |t| -t.checkin_at.to_i }
start_time = tx.first.checkin_at
end_time = tx.last.checkin_at

result = []
bin = []
tx.each do |t|
  time = start_time - t.checkin_at
  if (time/60) < minute_interval
    bin << t.id
  else
    result << [start_time.to_i*1000, bin.size]
    start_time = t.checkin_at
    bin = [t.id]
  end
end
result << [start_time.to_i*1000, bin.size]
result.sort_by { |t| t[0] }
  end

@dja
Copy link

dja commented Jun 10, 2015

+1 Would love this too.

@jbcarpanelli
Copy link

+1, this feature would be awesome!

@mustela
Copy link

mustela commented Aug 5, 2015

+1, would be great to have this one!

@pchaganti
Copy link

+1, this would be awesome!

@alexkrolick
Copy link

alexkrolick commented May 23, 2016

I'd really like to be able to be group by a customizable amount of days. E.g.:

Thing.group_by_days(:date_column, params[:number_of_days]).count

or perhaps

Thing.group_by_duration(:date, 3.day)

@alexkrolick
Copy link

@ankane this is a really old request, any thoughts on closing it or are you still considering it?

@edwardmp
Copy link

edwardmp commented Oct 22, 2016

This would be great, as right now there is no option to group by a month exactly from the current date. For instance, I have a record created on the 26th of september, and I want to group only the last 30 days. Right now, if I group by a month this record would be excluded as the group period would start at october 1st. But what I really want is all records created in the last 30 days, starting at 30 days ago and not necessarily begging from the 1st day of the month.

I believe #147 would also add this functionality

@ankane
Copy link
Owner

ankane commented Oct 26, 2017

Quick update: still happy to review a PR for this. My recommended interface would be

group_by_duration(5.minutes, column, options)

@ankane
Copy link
Owner

ankane commented Aug 6, 2018

Closing stale feature requests

@ankane
Copy link
Owner

ankane commented Dec 10, 2019

Just added this on the group_by_duration branch.

gem 'groupdate', github: 'ankane/groupdate', branch: 'group_by_duration'

You can use it with:

User.group_by_duration(10.minutes, :created_at).count

For those still interested in this feature, please let me know your feedback.


Notes

  • Duration is specified in seconds. Groups are calculated based on seconds since the Unix epoch. Leap seconds are excluded from the Unix epoch, so this works fairly well for values that evenly divide an hour (1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30 seconds/minutes) in time zones that strictly have hour DST offsets.
  • group_by_duration(1.day) equals group_by_duration(86400) and not group_by_day(), since some days have more or less than 86,400 seconds based on DST.

My main hesitation with this feature is users will likely be confused by group_by_duration(2.hours), group_by_duration(1.day), group_by_duration(3.days), and group_by_duration(2.months). It probably makes sense to limit it to the above durations if this feature is merged.

@alexanderadam
Copy link

For those still interested in this feature, please let me know your feedback.

IMHO this is a great thing! I just can't test in the nearer future.

@ankane
Copy link
Owner

ankane commented Jul 23, 2020

Update: Decided to go with a new option instead of a new method to avoid the confusion mentioned above. group_by_minute and group_by_second now have an n option.

User.group_by_minute(:created_at, n: 10).count # 10 minutes

It's currently on master with the plan to be released next week. Please let me know if you have any feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants