Merge pull request #747 from okriuchykhin/ok_SCI_101

Improve samples import speed [SCI-101]
This commit is contained in:
okriuchykhin 2017-07-25 14:10:45 +02:00 committed by GitHub
commit 532153b9cb

View file

@ -63,7 +63,7 @@ class Team < ActiveRecord::Base
# -3 == sample_group # -3 == sample_group
# TODO: use constants # TODO: use constants
def import_samples(sheet, mappings, user) def import_samples(sheet, mappings, user)
errors = [] errors = false
nr_of_added = 0 nr_of_added = 0
total_nr = 0 total_nr = 0
@ -72,15 +72,15 @@ class Team < ActiveRecord::Base
sname_index = -1 sname_index = -1
stype_index = -1 stype_index = -1
sgroup_index = -1 sgroup_index = -1
mappings.each.with_index do |(k, v), i| mappings.each.with_index do |(_, v), i|
if v == "-1" if v == '-1'
# Fill blank space, so our indices stay the same # Fill blank space, so our indices stay the same
custom_fields << nil custom_fields << nil
sname_index = i sname_index = i
elsif v == "-2" elsif v == '-2'
custom_fields << nil custom_fields << nil
stype_index = i stype_index = i
elsif v == "-3" elsif v == '-3'
custom_fields << nil custom_fields << nil
sgroup_index = i sgroup_index = i
else else
@ -91,87 +91,70 @@ class Team < ActiveRecord::Base
custom_fields << cf custom_fields << cf
end end
end end
# Now we can iterate through sample data and save stuff into db # Now we can iterate through sample data and save stuff into db
(2..sheet.last_row).each do |i| (2..sheet.last_row).each do |i|
error = []
total_nr += 1 total_nr += 1
sample = Sample.new(name: sheet.row(i)[sname_index],
team: self,
user: user)
sample = Sample.new( sample.transaction do
name: sheet.row(i)[sname_index], unless sample.valid?
team_id: id, errors = true
user: user raise ActiveRecord::Rollback
) end
if sample.save
sheet.row(i).each.with_index do |value, index| sheet.row(i).each.with_index do |value, index|
# We need to have sample saved before messing with custom fields (they
# need sample id)
if index == stype_index if index == stype_index
stype = SampleType.where(name: value, team_id: id).take stype = SampleType.where(name: value, team: self).take
if stype unless stype
stype = SampleType.new(name: value, team: self)
unless stype.save
errors = true
raise ActiveRecord::Rollback
end
end
sample.sample_type = stype sample.sample_type = stype
else
sample.create_sample_type(
name: value,
team_id: id
)
end
sample.save
elsif index == sgroup_index elsif index == sgroup_index
sgroup = SampleGroup.where(name: value, team_id: id).take sgroup = SampleGroup.where(name: value, team: self).take
if sgroup unless sgroup
sample.sample_group = sgroup sgroup = SampleGroup.new(name: value, team: self)
else unless sgroup.save
sample.create_sample_group( errors = true
name: value, raise ActiveRecord::Rollback
team_id: id
)
end end
sample.save end
elsif value and mappings[index.to_s].strip.present? and index != sname_index sample.sample_group = sgroup
if custom_fields[index] elsif value && custom_fields[index]
# we're working with CustomField # we're working with CustomField
scf = SampleCustomField.new( scf = SampleCustomField.new(
sample_id: sample.id, sample: sample,
custom_field_id: custom_fields[index].id, custom_field: custom_fields[index],
value: value value: value
) )
unless scf.valid?
if !scf.save errors = true
error << scf.errors.messages raise ActiveRecord::Rollback
end end
else sample.sample_custom_fields << scf
# This custom_field does not exist
error << {"#{mappings[index]}": "Does not exists"}
end end
end end
if Sample.import([sample],
recursive: true,
validate: false).failed_instances.any?
errors = true
raise ActiveRecord::Rollback
end end
else
error << sample.errors.messages
end
if error.present?
errors << { "#{i}": error}
else
nr_of_added += 1 nr_of_added += 1
end end
end end
if errors.count > 0 then if errors
return { return { status: :error, nr_of_added: nr_of_added, total_nr: total_nr }
status: :error,
errors: errors,
nr_of_added: nr_of_added,
total_nr: total_nr
}
else else
return { return { status: :ok, nr_of_added: nr_of_added, total_nr: total_nr }
status: :ok,
nr_of_added: nr_of_added,
total_nr: total_nr
}
end end
end end