From a4aaa0294f1dcce7f003fdaa76c6fd7091b8052d Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Thu, 24 Oct 2024 17:20:37 +0200 Subject: [PATCH] Limit stats update after import to the timespan of the imported data --- CHANGELOG.md | 10 ++++++++++ app/services/imports/create.rb | 5 ++++- spec/factories/imports.rb | 2 +- spec/jobs/import_job_spec.rb | 7 +++++-- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97fa4052..c6ef10ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +# 0.15.8 - 2024-10-24 + +### Fixed + +- Stats distance calculation now correctly calculates the daily distances. + +### Changed + +- Refactored the stats calculation process to make it more efficient. + # 0.15.8 - 2024-10-22 ### Added diff --git a/app/services/imports/create.rb b/app/services/imports/create.rb index 0464177d..c068b324 100644 --- a/app/services/imports/create.rb +++ b/app/services/imports/create.rb @@ -35,7 +35,10 @@ class Imports::Create end def schedule_stats_creating(user_id) - Stats::CalculatingJob.perform_later(user_id) + start_at = import.points.order(:timestamp).first.recorded_at + end_at = import.points.order(:timestamp).last.recorded_at + + Stats::CalculatingJob.perform_later(user_id, start_at:, end_at:) end def schedule_visit_suggesting(user_id, import) diff --git a/spec/factories/imports.rb b/spec/factories/imports.rb index 42315c38..05be1e9f 100644 --- a/spec/factories/imports.rb +++ b/spec/factories/imports.rb @@ -3,7 +3,7 @@ FactoryBot.define do factory :import do user - name { 'APRIL_2013.json' } + name { 'MARCH_2024.json' } source { Import.sources[:owntracks] } raw_data { OwnTracks::RecParser.new(File.read('spec/fixtures/files/owntracks/2024-03.rec')).call } end diff --git a/spec/jobs/import_job_spec.rb b/spec/jobs/import_job_spec.rb index f8ba3976..45532506 100644 --- a/spec/jobs/import_job_spec.rb +++ b/spec/jobs/import_job_spec.rb @@ -7,14 +7,17 @@ RSpec.describe ImportJob, type: :job do subject(:perform) { described_class.new.perform(user.id, import.id) } let(:user) { create(:user) } - let(:import) { create(:import, user:, name: 'owntracks_export.json') } + let!(:import) { create(:import, user:, name: 'owntracks_export.json') } + let!(:import_points) { create_list(:point, 9, import: import) } + let(:start_at) { Time.zone.at(1_709_283_789) } # Timestamp of the first point in the "2024-03.rec" fixture file + let(:end_at) { import.points.reload.order(:timestamp).last.recorded_at } it 'creates points' do expect { perform }.to change { Point.count }.by(9) end it 'calls Stats::CalculatingJob' do - expect(Stats::CalculatingJob).to receive(:perform_later).with(user.id) + expect(Stats::CalculatingJob).to receive(:perform_later).with(user.id, start_at:, end_at:) perform end