Skip to content

Instantly share code, notes, and snippets.

@zimkies
Created March 17, 2015 19:17
Show Gist options
  • Save zimkies/f4d735568cddca4023b9 to your computer and use it in GitHub Desktop.
Save zimkies/f4d735568cddca4023b9 to your computer and use it in GitHub Desktop.
Finding the number of users who have had 2 confirmed grouper requests at any one time
require 'date'
class Range
def intersection(other)
return nil if (self.max < other.begin or other.max < self.begin)
[self.begin, other.begin].max..[self.max, other.max].min
end
alias_method :&, :intersection
end
user_requests = Groupers::Request.all.group_by(&:user_id)
user_requests.select do |user_id, requests|
ranges = requests.map do |r|
cancelled_at = Crews::Cancellation.where(crew_id: r.crew_id).first.try(:created_at)
r.created_at..[r.starts_at, cancelled_at, r.failed_at].compact.min
end
intersections = ranges.combination(2).select { |r1, r2| (r1 & r2).present? }
intersections.count > 0 ? true : false
end
111 # without taking cancelled_at/filed_at into consideration
200 # taking cancelled_at into consideration
257 # taking failed_at into consideration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment