optimize queries

This commit is contained in:
Zach Kemp 2018-04-11 15:03:05 -07:00 committed by James Mason
parent 8a422412d6
commit 7d5d935a65
8 changed files with 101 additions and 43 deletions

View file

@ -23,8 +23,17 @@ class EventSchedule < ApplicationRecord
scope :canceled, -> { joins(:event).where('state = ?', 'canceled') }
scope :withdrawn, -> { joins(:event).where('state = ?', 'withdrawn') }
scope :with_event_states, ->(*states){ joins(:event).where('events.state IN (?)', states) }
delegate :guid, to: :room, prefix: true
def self.withdrawn_or_canceled_event_schedules(schedule_ids)
EventSchedule
.unscoped
.where(schedule_id: schedule_ids)
.with_event_states(:withdrawn, :canceled)
end
##
# Returns end of the event
#
@ -36,15 +45,41 @@ class EventSchedule < ApplicationRecord
# Returns event schedules that are scheduled in the same room and start_time as event
#
def intersecting_event_schedules
EventSchedule.unscoped.where(room: room, start_time: start_time, schedule: schedule).where.not(id: id)
EventSchedule
.unscoped
.where(room_id: room_id, start_time: start_time, schedule_id: schedule_id)
.where.not(id: id)
end
def replacement?
event.state == 'confirmed' && (!intersecting_event_schedules.canceled.empty? || !intersecting_event_schedules.withdrawn.empty?)
# event_schedule_source is a cached enumerable object that helps
# avoid repetitive EXISTS queries when rendering the schedule carousel partial
def replacement?(event_schedule_source = nil)
return false unless event.state == 'confirmed'
return replaced_event_schedules.exists? unless event_schedule_source
event_schedule_source.any? { |event_schedule| intersects_with?(event_schedule) }
end
# the event schedule that `self` replaced
def replaced_event_schedule
replaced_event_schedules.first
end
# NOTE: This and `intersecting_event_schedules` share the flaw that they do not
# detect overlapping schedules where the start times are different (i.e., where
# only a portion of the time intersects).
def intersects_with?(other)
other != self &&
other.room_id == room_id &&
other.start_time == start_time &&
other.schedule_id == schedule_id
end
private
def replaced_event_schedules
intersecting_event_schedules.with_event_states(:withdrawn, :canceled)
end
def start_after_end_hour
return unless event && start_time && event.program && event.program.conference && event.program.conference.end_hour
errors.add(:start_time, "can't be after the conference end hour (#{event.program.conference.end_hour})") if start_time.hour >= event.program.conference.end_hour

View file

@ -77,12 +77,14 @@ class Program < ApplicationRecord
validate :check_languages_format
# Returns all event_schedules for the selected schedule ordered by start_time
def selected_event_schedules
event_schedules = selected_schedule.event_schedules.order(start_time: :asc) if selected_schedule
tracks.self_organized.confirmed.order(start_date: :asc).each do |track|
event_schedules += track.selected_schedule.event_schedules.order(start_time: :asc) if track.selected_schedule
def selected_event_schedules(includes: [:event])
event_schedules = []
event_schedules = selected_schedule.event_schedules.includes(*includes).order(start_time: :asc) if selected_schedule
tracks.self_organized.confirmed.includes(selected_schedule: { event_schedules: includes }).order(start_date: :asc).each do |track|
next unless track.selected_schedule
event_schedules += track.selected_schedule.event_schedules
end
event_schedules.sort_by(&:start_time) if event_schedules
event_schedules.sort_by(&:start_time)
end
##
@ -171,7 +173,8 @@ class Program < ApplicationRecord
# * +False+ -> If there is not any event for the given date
def any_event_for_this_date?(date)
parsed_date = DateTime.parse("#{date} 00:00").utc
EventSchedule.where(schedule: selected_schedule).where(start_time: parsed_date..(parsed_date + 1.day)).any?
range = parsed_date..(parsed_date + 1.day)
selected_schedule.event_schedules.any? { |es| range.cover?(es.start_time) }
end
##