This commit is contained in:
Matt Jankowski 2025-09-03 20:07:44 +00:00 committed by GitHub
commit f0fe2e2b35
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 37 additions and 8 deletions

View File

@ -48,6 +48,14 @@ class BulkImport < ApplicationRecord
scope :archival_completed, -> { where(created_at: ..ARCHIVE_PERIOD.ago) } scope :archival_completed, -> { where(created_at: ..ARCHIVE_PERIOD.ago) }
scope :confirmation_missed, -> { state_unconfirmed.where(created_at: ..CONFIRM_PERIOD.ago) } scope :confirmation_missed, -> { state_unconfirmed.where(created_at: ..CONFIRM_PERIOD.ago) }
def failure_count
processed_items - imported_items
end
def processing_complete?
processed_items == total_items
end
def self.progress!(bulk_import_id, imported: false) def self.progress!(bulk_import_id, imported: false)
# Use `increment_counter` so that the incrementation is done atomically in the database # Use `increment_counter` so that the incrementation is done atomically in the database
BulkImport.increment_counter(:processed_items, bulk_import_id) BulkImport.increment_counter(:processed_items, bulk_import_id)
@ -55,6 +63,6 @@ class BulkImport < ApplicationRecord
# Since the incrementation has been done atomically, concurrent access to `bulk_import` is now bening # Since the incrementation has been done atomically, concurrent access to `bulk_import` is now bening
bulk_import = BulkImport.find(bulk_import_id) bulk_import = BulkImport.find(bulk_import_id)
bulk_import.update!(state: :finished, finished_at: Time.now.utc) if bulk_import.processed_items == bulk_import.total_items bulk_import.update!(state: :finished, finished_at: Time.now.utc) if bulk_import.processing_complete?
end end
end end

View File

@ -20,7 +20,7 @@ class BulkImportService < BaseService
import_lists! import_lists!
end end
@import.update!(state: :finished, finished_at: Time.now.utc) if @import.processed_items == @import.total_items @import.update!(state: :finished, finished_at: Time.now.utc) if @import.processing_complete?
rescue rescue
@import.update!(state: :finished, finished_at: Time.now.utc) @import.update!(state: :finished, finished_at: Time.now.utc)

View File

@ -60,9 +60,6 @@
= l(import.created_at) = l(import.created_at)
%td %td
- num_failed = import.processed_items - import.imported_items - if import.failure_count.positive?
- if num_failed.positive? = link_to_if import.state_finished?, import.failure_count, failures_settings_import_path(import, format: :csv) do
- if import.state_finished? = import.failure_count
= link_to num_failed, failures_settings_import_path(import, format: 'csv')
- else
= num_failed

View File

@ -39,4 +39,28 @@ RSpec.describe BulkImport do
end end
end end
end end
describe '#failure_count' do
subject { described_class.new(processed_items: 100, imported_items: 90).failure_count }
it { is_expected.to eq(10) }
end
describe '#processing_complete?' do
subject { Fabricate.build :bulk_import, processed_items:, total_items: }
context 'when processed and total are the same' do
let(:processed_items) { 100 }
let(:total_items) { 100 }
it { is_expected.to be_processing_complete }
end
context 'when processed and total are not the same' do
let(:processed_items) { 100 }
let(:total_items) { 200 }
it { is_expected.to_not be_processing_complete }
end
end
end end