log all sis errors and warnings to a file

refs CNVS-37717

test plan
 - run sis import that would generate errors
 - in console it should have an errors_attachment
 - the attachment should contain all the errors

Change-Id: I8e76d29395047b1ee743e42394432baea27d7c3a
Reviewed-on: https://gerrit.instructure.com/116522
Tested-by: Jenkins
Reviewed-by: Tyler Pickett <tpickett@instructure.com>
QA-Review: Tucker McKnight <tmcknight@instructure.com>
Product-Review: Rob Orton <rob@instructure.com>
This commit is contained in:
Rob Orton 2017-06-21 19:24:39 -06:00
parent a05e102e68
commit ff1589f408
3 changed files with 55 additions and 0 deletions

View File

@ -24,6 +24,7 @@ class SisBatch < ActiveRecord::Base
serialize :processing_errors, Array
serialize :processing_warnings, Array
belongs_to :attachment
belongs_to :errors_attachment, class_name: 'Attachment'
belongs_to :generated_diff, class_name: 'Attachment'
belongs_to :batch_mode_term, class_name: 'EnrollmentTerm'
belongs_to :user
@ -416,9 +417,28 @@ class SisBatch < ActiveRecord::Base
Setting.get('sis_batch_max_messages', '1000').to_i
end
def write_warnings_and_errors_to_file
error_count = processing_errors&.size || 0
warning_count = processing_warnings&.size || 0
return unless error_count > 0 || warning_count > 0
temp = Tempfile.open([self.global_id.to_s + '_processing_warnings_and_errors', '.csv'])
file = temp.path
temp.close!
CSV.open(file, "w") do |csv|
processing_warnings.each {|row| csv << row}
processing_errors.each {|row| csv << row}
end
self.errors_attachment = SisBatch.create_data_attachment(
self,
Rack::Test::UploadedFile.new(file, 'csv', true),
t("sis_errors_attachment_%{id}", id: self.id)
)
end
def limit_size_of_messages
max_messages = SisBatch.max_messages
%w[processing_warnings processing_errors].each do |field|
write_warnings_and_errors_to_file
if self.send("#{field}_changed?") && (self.send(field).try(:size) || 0) > max_messages
limit_message = case field
when "processing_warnings"

View File

@ -0,0 +1,26 @@
#
# Copyright (C) 2017 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
class AddProcessingErrorsAttachmentToSisBatch < ActiveRecord::Migration[5.0]
tag :predeploy
def change
add_column :sis_batches, :errors_attachment_id, :integer, :limit => 8
add_foreign_key :sis_batches, :attachments, column: :errors_attachment_id
add_index :sis_batches, :errors_attachment_id
end
end

View File

@ -475,6 +475,15 @@ s2,test_1,section2,active},
expect(batch.processing_errors.last).to eq ['', 'There were 3 more errors']
end
it "should write warnings/errors to a file" do
batch = @account.sis_batches.create!
batch.processing_warnings = [ ['testfile.csv', 'test warning'] ] * 3
batch.processing_errors = [ ['testfile.csv', 'test error'] ] * 3
batch.save!
error_file = batch.errors_attachment
expect(CSV.parse(error_file.open).map.to_a.size).to eq 6
end
context "csv diffing" do
it "should skip diffing if previous diff not available" do
SIS::CSV::DiffGenerator.any_instance.expects(:generate).never