Skip to content

Commit 07100ab

Browse files
committed
wip #221
1 parent 9ea1b10 commit 07100ab

File tree

5 files changed

+97
-17
lines changed

5 files changed

+97
-17
lines changed

app/models/import_error.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
# == Schema Information
2+
#
3+
# Table name: import_errors
4+
#
5+
# id :bigint not null, primary key
6+
# error_message :text
7+
# error_type :string not null
8+
# file_name :string
9+
# metadata :json
10+
# created_at :datetime not null
11+
# updated_at :datetime not null
12+
# import_report_id :bigint not null
13+
# topic_id :integer
14+
#
15+
# Indexes
16+
#
17+
# index_import_errors_on_error_type (error_type)
18+
# index_import_errors_on_file_name (file_name)
19+
# index_import_errors_on_import_report_id (import_report_id)
20+
#
21+
# Foreign Keys
22+
#
23+
# fk_rails_... (import_report_id => import_reports.id)
24+
#
125
class ImportError < ApplicationRecord
226
belongs_to :import_report
327

app/models/import_report.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
1+
# == Schema Information
2+
#
3+
# Table name: import_reports
4+
#
5+
# id :bigint not null, primary key
6+
# completed_at :datetime
7+
# error_details :json
8+
# import_type :string not null
9+
# started_at :datetime
10+
# status :string default("pending")
11+
# summary_stats :json
12+
# unmatched_files :json
13+
# created_at :datetime not null
14+
# updated_at :datetime not null
15+
#
16+
# Indexes
17+
#
18+
# index_import_reports_on_import_type (import_type)
19+
#
120
class ImportReport < ApplicationRecord
221
has_many :import_errors, dependent: :destroy
322

423
validates :import_type, presence: true
524

6-
enum :status, { pending: "pending", completed: "completed", failed: "failed" }
25+
enum :status, { pending: "pending", planned: "planned", completed: "completed", failed: "failed" }
726

827
scope :recent, -> { order(created_at: :desc) }
928
scope :by_type, ->(type) { where(import_type: type) }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class CreateImportErrors < ActiveRecord::Migration[7.0]
2+
def change
3+
create_table :import_errors do |t|
4+
t.references :import_report, null: false, foreign_key: true
5+
t.string :error_type, null: false
6+
t.string :file_name
7+
t.integer :topic_id
8+
t.text :error_message
9+
t.json :metadata
10+
11+
t.timestamps
12+
end
13+
14+
add_index :import_errors, :error_type
15+
add_index :import_errors, :file_name
16+
end
17+
end

db/schema.rb

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/autorequire/data_import.rb

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,19 @@ def self.import_training_documents
202202
importable_rows = match_csv_with_azure_files(valid_csv_rows, azure_files)
203203
unmatched_files = collect_unmatched_files(csv_data, azure_files, importable_rows)
204204

205-
log_import_summary(valid_csv_rows, azure_files, importable_rows)
205+
report.update!(
206+
summary_stats: build_summary_stats(import_stats, csv_data, azure_files),
207+
unmatched_files: unmatched_files,
208+
status: "planned"
209+
)
206210

207-
process_document_attachments(importable_rows, import_stats)
211+
process_document_attachments(importable_rows, import_stats, report)
208212

209213
report.update!(
210214
completed_at: Time.current,
211215
status: "completed",
212216
summary_stats: build_summary_stats(import_stats, csv_data, azure_files),
213-
unmatched_files: unmatched_files,
214-
error_details: import_stats[:error_files]
217+
unmatched_files: unmatched_files
215218
)
216219

217220
log_final_results(import_stats)
@@ -254,16 +257,16 @@ def self.match_csv_with_azure_files(csv_rows, azure_files)
254257
end
255258
end
256259

257-
def self.process_document_attachments(rows, stats)
260+
def self.process_document_attachments(rows, stats, report)
258261
rows.each do |row|
259262
topic = Topic.find_by(id: row["Topic_ID"])
260263
next unless topic
261264

262-
attach_document_to_topic(topic, row, stats)
265+
attach_document_to_topic(topic, row, stats, report)
263266
end
264267
end
265268

266-
def self.attach_document_to_topic(topic, row, stats)
269+
def self.attach_document_to_topic(topic, row, stats, report)
267270
file_path = get_file_path(topic.state, topic.language.name)
268271
filename = row["File_Name"]
269272

@@ -285,7 +288,7 @@ def self.attach_document_to_topic(topic, row, stats)
285288
end
286289

287290
rescue AzureFileShares::Errors::ApiError, URI::InvalidURIError => e
288-
handle_attachment_error(topic, filename, e, stats)
291+
handle_attachment_error(topic, filename, e, stats, report)
289292
end
290293
end
291294

@@ -298,12 +301,20 @@ def self.download_azure_file(file_path, filename)
298301
)
299302
end
300303

301-
def self.handle_attachment_error(topic, filename, error, stats)
304+
def self.handle_attachment_error(topic, filename, error, stats, report)
302305
error_info = {
303306
topic: topic,
304307
file: filename,
305308
error: error.message,
306309
}
310+
311+
ImportError.create(
312+
import_report_id: report.id,
313+
topic_id: topic.id,
314+
file_name: filename,
315+
error_message: error_info
316+
)
317+
307318
stats[:error_files] << error_info
308319
puts "Error with file: #{filename} for topic #{topic.title} - #{error.message}"
309320
end
@@ -331,12 +342,6 @@ def self.build_summary_stats(import_stats, csv_data, azure_files)
331342
}
332343
end
333344

334-
def self.log_import_summary(csv_rows, azure_files, importable_rows)
335-
puts "CSV rows with topics: #{csv_rows.size}"
336-
puts "Azure files found: #{azure_files.size}"
337-
puts "Importable files: #{importable_rows.size}"
338-
end
339-
340345
def self.log_final_results(stats)
341346
puts "Topics not found: #{stats[:topics_without_csv].size}"
342347
puts "Successful attachments: #{stats[:successful_attachments].size}"

0 commit comments

Comments
 (0)