Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion app/services/fact_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,30 @@ def add_new_fact(name)
end

def add_new_facts
facts_to_create.each { |f| add_new_fact(f) }
compose_names, regular_names = facts_to_create.partition { |name| facts[name].nil? }
compose_names.each { |f| add_new_fact(f) }

if host.new_record?
regular_names.each { |f| add_new_fact(f) }
else
bulk_insert_new_fact_values(regular_names)
end
Comment on lines +133 to +140
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this easier to follow and have the same performance?

Suggested change
compose_names, regular_names = facts_to_create.partition { |name| facts[name].nil? }
compose_names.each { |f| add_new_fact(f) }
if host.new_record?
regular_names.each { |f| add_new_fact(f) }
else
bulk_insert_new_fact_values(regular_names)
end
if host.new_record?
facts_to_create.each { |f| add_new_fact(f) }
else
compose_names, regular_names = facts_to_create.partition { |name| facts[name].nil? }
compose_names.each { |f| add_new_fact(f) }
bulk_insert_new_fact_values(regular_names)
end

@counters[:added] = facts_to_create.size
end

def bulk_insert_new_fact_values(names)
return if names.empty?
time = Time.now.utc
records = names.map do |name|
{ value: facts[name], fact_name_id: fact_names[name].id, host_id: host.id,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still trying to figure out if everything could use a bulk insert for the composite names as well. Did you try passing nil?

Suggested change
{ value: facts[name], fact_name_id: fact_names[name].id, host_id: host.id,
{ value: facts[name], fact_name_id: fact_names[name]&.id, host_id: host.id,

created_at: time, updated_at: time }
end
FactValue.insert_all(records, unique_by: [:fact_name_id, :host_id])
rescue => e
logger.error("Facts could not be imported because of #{e.message}")
@error = e
end

def update_facts
time = Time.now.utc
updated = 0
Expand Down
Loading