Update points relation

This commit is contained in:
Eugene Burmakin 2025-08-29 11:05:25 +02:00
parent 5ab382936d
commit 504bb92cdb
3 changed files with 19 additions and 19 deletions

View file

@ -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]}"

View file

@ -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
end

View file

@ -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
end