Skip to content

Instantly share code, notes, and snippets.

@benlangfeld
Created May 9, 2011 14:00
Show Gist options
  • Save benlangfeld/962560 to your computer and use it in GitHub Desktop.
Save benlangfeld/962560 to your computer and use it in GitHub Desktop.
def ready_to_call(times={})
@timer ||= CumulativeTimer.new "#{RAILS_ROOT}/log/appointment_call_load_timing.txt"
@timer.dump
times = { :unconfirmed_start => Time.now - 4.hours, :unconfirmed_end => Time.now + 100.days - 4.hours,
:confirmed_start => Time.now - 4.hours, :confirmed_end => Time.now + 100.days - 4.hours }.merge times
uc_start, uc_end = times[:unconfirmed_start].to_i, times[:unconfirmed_end].to_i
c_start, c_end = times[:confirmed_start].to_i, times[:confirmed_end].to_i
unconf_range = [uc_start, uc_end] unless [uc_start,uc_end].include? :skip
conf_range = [c_start, c_end] unless [c_start,c_end].include? :skip
big_time_range = times.values
big_time_range.delete :skip
big_time_range = big_time_range.minmax
blocks = AppointmentTimeBlock.all
@timer.time('loading calls from db') do
@appointments = all(:include => 'lead', :conditions => ["appointments.status = 'waiting' and leads.status in ('Confirmed','Not Conf') and leads.set_for >= ? and leads.set_for <= ?"] + big_time_range)
end
@timer.time('removing calls w/o callable phones') { @appointments = @appointments.reject { |c| c.callable_phones.empty?} }
@timer.time('removing calls with time blocks') { @appointments = @appointments.reject { |c| AppointmentTimeBlock.blocking?(c, blocks) } }
@timer.time('removing calls in the wrong time range') do
@appointments = @appointments.select do |c|
time_range = c.lead.status == 'Confirmed' ? conf_range : unconf_range
time_range and c.cached_appointment_time_i and c.cached_appointment_time_i.between?(*time_range)
end
end
@appointments
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment