Skip to content

Instantly share code, notes, and snippets.

@matthutchinson
Created May 22, 2013 16:49
Show Gist options
  • Save matthutchinson/5629080 to your computer and use it in GitHub Desktop.
Save matthutchinson/5629080 to your computer and use it in GitHub Desktop.
# Returns a score for last 10 bookings' acceptance rate
# If 1 booking rejected over past 10 bookings, return 0.5
# If 2 bookings rejected, return 0.0
def booking_acceptance_score
total_bookings_to_be_considered = 10
booking_ids = []
offset = 0
while booking_ids.length < total_bookings_to_be_considered do
# Fetch histories one by one
result = all_booking_histories_as_host.requested.get_last(1).offset(offset).first
offset += 1
# If result is nil
# means there are no histories
# to look at
break if result.nil?
booking_id = result.booking_id
created_at = result.created_at
# Check if there is a reject story for this booking
history = BookingHistory.rejected.first(:conditions => { :booking_id => booking_id })
# If history is nil means it was not
# rejected and we should count it as relevant
# so we push it
if history.nil?
booking_ids << booking_id
next
end
# created_at variable holds the timestamp for when the request turned
# out to be an yellow flag and we compare it agaisn't the associated
# booking offer and check timestamps. If more than 7 days have
# passed do not consider this as relevant booking
#
# Also be aware of instant bookings (booking_offer is nil)
booking = Booking.find(booking_id)
booking_offer = booking.booking_offer
if booking_offer && booking_offer.created_at.advance(:days => 7) < created_at
next
else
booking_ids << booking_id
end
end
rejected = BookingHistory.rejected.scoped(:conditions => { :booking_id => booking_ids }).count
score = if rejected == 0
1.0
elsif rejected == 1
0.5
else
0.0
end
[rejected,score]
end
memoize :booking_acceptance_score
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment