Start reverse geocoding after import is finished

This commit is contained in:
Eugene Burmakin 2024-12-25 12:38:32 +01:00
parent 0dfdeac5c5
commit 0276882db1
10 changed files with 47 additions and 17 deletions

View file

@ -1 +1 @@
0.21.1
0.21.2

View file

@ -5,6 +5,12 @@ 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.21.2 - 2024-12-25
### Changed
- Imported points will now be reverse geocoded only after import is finished.
# 0.21.1 - 2024-12-24
### Added

View file

@ -10,31 +10,20 @@ export default class extends Controller {
return
}
// console.log("Imports controller connected", {
// hasIndexTarget: this.hasIndexTarget,
// element: this.element,
// userId: this.element.dataset.userId
// });
this.setupSubscription();
}
setupSubscription() {
const userId = this.element.dataset.userId;
// console.log("Setting up subscription with userId:", userId);
this.channel = consumer.subscriptions.create(
{ channel: "ImportsChannel" },
{
connected: () => {
// console.log("Successfully connected to ImportsChannel");
// Test that we can receive messages
// console.log("Subscription object:", this.channel);
},
disconnected: () => {
// console.log("Disconnected from ImportsChannel");
},
received: (data) => {
// console.log("Received data:", data);
const row = this.element.querySelector(`tr[data-import-id="${data.import.id}"]`);
if (row) {

View file

@ -1,8 +1,6 @@
# frozen_string_literal: true
class Import < ApplicationRecord
# self.ignored_columns = %w[raw_data]
belongs_to :user
has_many :points, dependent: :destroy
@ -17,6 +15,10 @@ class Import < ApplicationRecord
Imports::Create.new(user, self).call
end
def reverse_geocoded_points_count
points.reverse_geocoded.count
end
def years_and_months_tracked
points.order(:timestamp).pluck(:timestamp).map do |timestamp|
time = Time.zone.at(timestamp)

View file

@ -33,8 +33,9 @@ class Point < ApplicationRecord
Time.zone.at(timestamp)
end
def async_reverse_geocode
def async_reverse_geocode(force: false)
return unless REVERSE_GEOCODING_ENABLED
return if import_id.present? && !force
ReverseGeocodingJob.perform_later(self.class.to_s, id)
end

View file

@ -15,6 +15,7 @@ class Imports::Create
schedule_stats_creating(user.id)
schedule_visit_suggesting(user.id, import)
schedule_reverse_geocoding(user.id)
rescue StandardError => e
create_import_failed_notification(import, user, e)
end
@ -47,6 +48,10 @@ class Imports::Create
VisitSuggestingJob.perform_later(user_ids: [user_id], start_at:, end_at:)
end
def schedule_reverse_geocoding(user_id)
EnqueueBackgroundJob.perform_later('continue_reverse_geocoding', user_id)
end
def create_import_finished_notification(import, user)
Notifications::Create.new(
user:,

View file

@ -21,6 +21,8 @@ class Jobs::Create
raise InvalidJobName, 'Invalid job name'
end
points.find_each(batch_size: 1_000, &:async_reverse_geocode)
points.find_each(batch_size: 1_000) do |point|
point.async_reverse_geocode(force: true)
end
end
end

View file

@ -41,6 +41,7 @@
<tr>
<th>Name</th>
<th>Imported points</th>
<th>Reverse geocoded points</th>
<th>Created at</th>
</tr>
</thead>
@ -50,7 +51,9 @@
data-user-id="<%= current_user.id %>"
>
<% @imports.each do |import| %>
<tr data-import-id="<%= import.id %>" id="import-<%= import.id %>">
<tr data-import-id="<%= import.id %>"
id="import-<%= import.id %>"
data-points-total="<%= import.points_count %>">
<td>
<%= link_to import.name, import, class: 'underline hover:no-underline' %>
(<%= import.source %>)
@ -62,6 +65,9 @@
<td data-points-count>
<%= number_with_delimiter import.points_count %>
</td>
<td data-reverse-geocoded-points-count>
<%= number_with_delimiter import.reverse_geocoded_points_count %>
</td>
<td><%= import.created_at.strftime("%d.%m.%Y, %H:%M") %></td>
</tr>
<% end %>

View file

@ -52,6 +52,20 @@ RSpec.describe Point, type: :model do
expect { point.async_reverse_geocode }.to have_enqueued_job(ReverseGeocodingJob)
.with('Point', point.id)
end
context 'when point is imported' do
let(:point) { build(:point, import_id: 1) }
it 'does not enqueue ReverseGeocodeJob' do
expect { point.async_reverse_geocode }.not_to have_enqueued_job(ReverseGeocodingJob)
end
context 'when reverse geocoding is forced' do
it 'enqueues ReverseGeocodeJob' do
expect { point.async_reverse_geocode(force: true) }.to have_enqueued_job(ReverseGeocodingJob)
end
end
end
end
end
end

View file

@ -55,6 +55,11 @@ RSpec.describe Imports::Create do
expect { service.call }.to have_enqueued_job(VisitSuggestingJob)
end
end
it 'schedules reverse geocoding' do
expect { service.call }.to \
have_enqueued_job(EnqueueBackgroundJob).with('continue_reverse_geocoding', user.id)
end
end
context 'when import fails' do