Skip to content

Commit

Permalink
Optimize Interactors::MatchLiveTimes; add bib_number to match criteria.
Browse files Browse the repository at this point in the history
  • Loading branch information
moveson committed Feb 16, 2018
1 parent 6bb2f4a commit 82aab1e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions app/models/split_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class SplitTime < ApplicationRecord
scope :from_finished_efforts, -> { joins(effort: {split_times: :split}).where(splits: {kind: 1}) }
scope :visible, -> { includes(effort: {event: :event_group}).where('event_groups.concealed = ?', 'f') }
scope :at_time_point, -> (time_point) { where(lap: time_point.lap, split_id: time_point.split_id, bitkey: time_point.bitkey) }
scope :with_live_time_matchers, -> { joins(effort: :event).select("split_times.*, (events.start_time + efforts.start_offset * interval '1 second' + time_from_start * interval '1 second') as day_and_time, events.home_time_zone as event_home_zone, efforts.bib_number as bib_number")}

# SplitTime::recorded_at_aid functions properly only when called on split_times within an event
# Otherwise it includes split_times from aid_stations other than the given parameter
Expand Down
19 changes: 10 additions & 9 deletions app/services/interactors/match_live_times.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def initialize(args)
@event = args[:event]
@live_times = args[:live_times]
@tolerance = args[:tolerance] || 1.minute
@split_times = event.split_times.with_live_time_matchers
@errors = []
@resources = {matched_live_times: [], unmatched_live_times: []}
validate_setup
Expand All @@ -28,7 +29,7 @@ def perform!

private

attr_reader :event, :live_times, :tolerance, :errors, :resources
attr_reader :event, :live_times, :tolerance, :split_times, :errors, :resources

def match_live_time_to_split_time(live_time)
split_time = matching_split_time(live_time)
Expand All @@ -41,16 +42,16 @@ def match_live_time_to_split_time(live_time)
end

def matching_split_time(live_time)
live_time.absolute_time &&
!live_time.matched? &&
event.split_times.where(match_attributes(live_time)).find { |st| (st.day_and_time - live_time.absolute_time).abs <= tolerance }
live_time.absolute_time && !live_time.matched? && split_times.find { |split_time| matching_record(split_time, live_time) }
end

def match_attributes(live_time)
attributes = {split: live_time.split, bitkey: live_time.bitkey}
attributes[:pacer] = live_time.with_pacer unless live_time.with_pacer.nil?
attributes[:stopped_here] = live_time.stopped_here unless live_time.stopped_here.nil?
attributes
def matching_record(split_time, live_time)
(split_time.split_id == live_time.split_id) &&
(split_time.bitkey == live_time.bitkey) &&
(split_time.bib_number.to_s == live_time.bib_number) &&
(live_time.stopped_here.nil? || (live_time.stopped_here == split_time.stopped_here)) &&
(live_time.with_pacer.nil? || (live_time.with_pacer == split_time.pacer)) &&
(split_time.day_and_time - live_time.absolute_time).abs <= tolerance
end

def matched_live_times
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/active_record_query_trace.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
if Rails.env.development? || Rails.env.test?
ActiveRecordQueryTrace.enabled = true
ActiveRecordQueryTrace.enabled = false
ActiveRecordQueryTrace.level = :app
ActiveRecordQueryTrace.ignore_cached_queries = true
ActiveRecordQueryTrace.colorize = 'light purple'
Expand Down
13 changes: 13 additions & 0 deletions spec/services/interactors/match_live_times_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@
end
end

context 'when all attributes match but bib_number is different' do
let(:matching_split_times) { [split_time_1, split_time_3] }
let(:live_times) { [live_time_1, live_time_2, live_time_3] }
let(:matching_live_times) { [live_time_1, live_time_3] }
let(:non_matching_live_times) { [live_time_2] }

before { live_time_2.update(bib_number: effort.bib_number + 1) }

it 'sets split_time for all live_times' do
verify_live_times
end
end

context 'when split and bitkey are the same but time is outside of tolerance' do
let(:matching_split_times) { [split_time_1, split_time_3] }
let(:live_times) { [live_time_1, live_time_2, live_time_3] }
Expand Down

0 comments on commit 82aab1e

Please sign in to comment.