From 504bb92cdbfd96b6aa8f94471ff57e749879392b Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Fri, 29 Aug 2025 11:05:25 +0200 Subject: [PATCH] Update points relation --- app/jobs/tracks/time_chunk_processor_job.rb | 4 ++-- app/services/tracks/time_chunker.rb | 16 ++++++++-------- spec/services/tracks/time_chunker_spec.rb | 18 +++++++++--------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/app/jobs/tracks/time_chunk_processor_job.rb b/app/jobs/tracks/time_chunk_processor_job.rb index e2bbe10f..9cbca77b 100644 --- a/app/jobs/tracks/time_chunk_processor_job.rb +++ b/app/jobs/tracks/time_chunk_processor_job.rb @@ -62,7 +62,7 @@ class Tracks::TimeChunkProcessorJob < ApplicationJob end def load_chunk_points - user.tracked_points + user.points .where(timestamp: chunk_data[:buffer_start_timestamp]..chunk_data[:buffer_end_timestamp]) .order(:timestamp) end @@ -99,7 +99,7 @@ class Tracks::TimeChunkProcessorJob < ApplicationJob begin # Calculate distance using Geocoder with validation distance = Point.calculate_distance_for_array_geocoder(points, :km) - + # Additional validation for the distance result if !distance.finite? || distance < 0 Rails.logger.error "Invalid distance calculated (#{distance}) for #{points.size} points in chunk #{chunk_data[:chunk_id]}" diff --git a/app/services/tracks/time_chunker.rb b/app/services/tracks/time_chunker.rb index 9b4d3af2..56db25dd 100644 --- a/app/services/tracks/time_chunker.rb +++ b/app/services/tracks/time_chunker.rb @@ -25,10 +25,10 @@ class Tracks::TimeChunker while current_time < end_time chunk_end = [current_time + chunk_size, end_time].min - + chunk = create_chunk(current_time, chunk_end, start_time, end_time) chunks << chunk if chunk_has_points?(chunk) - + current_time = chunk_end end @@ -44,14 +44,14 @@ class Tracks::TimeChunker when start_at [start_at.to_time, Time.current] when end_at - first_point_time = user.tracked_points.minimum(:timestamp) + first_point_time = user.points.minimum(:timestamp) return nil unless first_point_time [Time.at(first_point_time), end_at.to_time] else # Get full range from user's points - first_point_time = user.tracked_points.minimum(:timestamp) - last_point_time = user.tracked_points.maximum(:timestamp) - + first_point_time = user.points.minimum(:timestamp) + last_point_time = user.points.maximum(:timestamp) + return nil unless first_point_time && last_point_time [Time.at(first_point_time), Time.at(last_point_time)] end @@ -77,8 +77,8 @@ class Tracks::TimeChunker def chunk_has_points?(chunk) # Check if there are any points in the buffer range to avoid empty chunks - user.tracked_points + user.points .where(timestamp: chunk[:buffer_start_timestamp]..chunk[:buffer_end_timestamp]) .exists? end -end \ No newline at end of file +end diff --git a/spec/services/tracks/time_chunker_spec.rb b/spec/services/tracks/time_chunker_spec.rb index 4209f9e7..5a38a052 100644 --- a/spec/services/tracks/time_chunker_spec.rb +++ b/spec/services/tracks/time_chunker_spec.rb @@ -19,7 +19,7 @@ RSpec.describe Tracks::TimeChunker do it 'accepts custom options' do start_time = 1.week.ago end_time = Time.current - + custom_chunker = described_class.new( user, start_at: start_time, @@ -75,7 +75,7 @@ RSpec.describe Tracks::TimeChunker do # Buffer zones should be at or beyond chunk boundaries (may be constrained by global boundaries) expect(chunk[:buffer_start_time]).to be <= chunk[:start_time] expect(chunk[:buffer_end_time]).to be >= chunk[:end_time] - + # Verify buffer timestamps are consistent expect(chunk[:buffer_start_timestamp]).to eq(chunk[:buffer_start_time].to_i) expect(chunk[:buffer_end_timestamp]).to eq(chunk[:buffer_end_time].to_i) @@ -96,7 +96,7 @@ RSpec.describe Tracks::TimeChunker do :buffer_start_time, :buffer_end_time ) - + expect(chunk[:chunk_id]).to match(/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/) end end @@ -148,7 +148,7 @@ RSpec.describe Tracks::TimeChunker do # Should create more chunks with smaller chunk size expect(chunks.size).to be > 2 - + # Each chunk should be approximately 12 hours chunk = chunks.first duration = chunk[:end_time] - chunk[:start_time] @@ -165,7 +165,7 @@ RSpec.describe Tracks::TimeChunker do chunk = chunks.first buffer_start_diff = chunk[:start_time] - chunk[:buffer_start_time] buffer_end_diff = chunk[:buffer_end_time] - chunk[:end_time] - + expect(buffer_start_diff).to be <= 1.hour expect(buffer_end_diff).to be <= 1.hour end @@ -200,7 +200,7 @@ RSpec.describe Tracks::TimeChunker do expect(chunks).not_to be_empty chunks.each do |chunk| # Verify each chunk has points in its buffer range - points_exist = user.tracked_points + points_exist = user.points .where(timestamp: chunk[:buffer_start_timestamp]..chunk[:buffer_end_timestamp]) .exists? expect(points_exist).to be true @@ -270,14 +270,14 @@ RSpec.describe Tracks::TimeChunker do it 'handles all time range scenarios correctly' do test_start_time = 2.days.ago test_end_time = Time.current - + # Both provided chunker_both = described_class.new(user, start_at: test_start_time, end_at: test_end_time) result_both = chunker_both.send(:determine_time_range) expect(result_both[0]).to be_within(1.second).of(test_start_time.to_time) expect(result_both[1]).to be_within(1.second).of(test_end_time.to_time) - # Only start provided + # Only start provided chunker_start = described_class.new(user, start_at: test_start_time) result_start = chunker_start.send(:determine_time_range) expect(result_start[0]).to be_within(1.second).of(test_start_time.to_time) @@ -306,4 +306,4 @@ RSpec.describe Tracks::TimeChunker do end end end -end \ No newline at end of file +end