Improve samples import speed [SCI-101]

This commit is contained in:
Oleksii Kriuchykhin 2017-07-24 17:27:50 +02:00
parent a49c35df9f
commit 67b0c78593

View file

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