mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-09-08 14:15:35 +08:00
Merge pull request #747 from okriuchykhin/ok_SCI_101
Improve samples import speed [SCI-101]
This commit is contained in:
commit
532153b9cb
1 changed files with 53 additions and 70 deletions
|
@ -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,17 +72,17 @@ 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
|
||||||
cf = CustomField.find_by_id(v)
|
cf = CustomField.find_by_id(v)
|
||||||
|
|
||||||
|
@ -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
|
||||||
sample.sample_type = stype
|
stype = SampleType.new(name: value, team: self)
|
||||||
else
|
unless stype.save
|
||||||
sample.create_sample_type(
|
errors = true
|
||||||
name: value,
|
raise ActiveRecord::Rollback
|
||||||
team_id: id
|
|
||||||
)
|
|
||||||
end
|
|
||||||
sample.save
|
|
||||||
elsif index == sgroup_index
|
|
||||||
sgroup = SampleGroup.where(name: value, team_id: id).take
|
|
||||||
|
|
||||||
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]
|
|
||||||
# we're working with CustomField
|
|
||||||
scf = SampleCustomField.new(
|
|
||||||
sample_id: sample.id,
|
|
||||||
custom_field_id: custom_fields[index].id,
|
|
||||||
value: value
|
|
||||||
)
|
|
||||||
|
|
||||||
if !scf.save
|
|
||||||
error << scf.errors.messages
|
|
||||||
end
|
end
|
||||||
else
|
|
||||||
# This custom_field does not exist
|
|
||||||
error << {"#{mappings[index]}": "Does not exists"}
|
|
||||||
end
|
end
|
||||||
|
sample.sample_type = stype
|
||||||
|
elsif index == sgroup_index
|
||||||
|
sgroup = SampleGroup.where(name: value, team: self).take
|
||||||
|
|
||||||
|
unless sgroup
|
||||||
|
sgroup = SampleGroup.new(name: value, team: self)
|
||||||
|
unless sgroup.save
|
||||||
|
errors = true
|
||||||
|
raise ActiveRecord::Rollback
|
||||||
|
end
|
||||||
|
end
|
||||||
|
sample.sample_group = sgroup
|
||||||
|
elsif value && custom_fields[index]
|
||||||
|
# we're working with CustomField
|
||||||
|
scf = SampleCustomField.new(
|
||||||
|
sample: sample,
|
||||||
|
custom_field: custom_fields[index],
|
||||||
|
value: value
|
||||||
|
)
|
||||||
|
unless scf.valid?
|
||||||
|
errors = true
|
||||||
|
raise ActiveRecord::Rollback
|
||||||
|
end
|
||||||
|
sample.sample_custom_fields << scf
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
if Sample.import([sample],
|
||||||
error << sample.errors.messages
|
recursive: true,
|
||||||
end
|
validate: false).failed_instances.any?
|
||||||
if error.present?
|
errors = true
|
||||||
errors << { "#{i}": error}
|
raise ActiveRecord::Rollback
|
||||||
else
|
end
|
||||||
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
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue