Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Performance] MiqEventDefinition.seed speedup #15630

Merged
merged 1 commit into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
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
15 changes: 9 additions & 6 deletions app/models/miq_event_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,23 @@ def self.add_elements(_vm, xmlNode)
end

def self.seed
seed_default_events
seed_default_definitions
event_defs = all.group_by(&:name)
seed_default_events(event_defs)
seed_default_definitions(event_defs)
end

def self.seed_default_events
def self.seed_default_events(event_defs)
event_sets = MiqEventDefinitionSet.all.index_by(&:name)
fname = File.join(FIXTURE_DIR, "#{to_s.pluralize.underscore}.csv")
CSV.foreach(fname, :headers => true, :skip_lines => /^#/, :skip_blanks => true) do |csv_row|
event = csv_row.to_hash
set_type = event.delete('set_type')

rec = find_by(:name => event['name'])
rec = event_defs[event['name']].try(:first)
if rec.nil?
_log.info("Creating [#{event['name']}]")
rec = create(event)
(event_defs[event['name']] ||= []) << rec
else
rec.attributes = event
if rec.changed?
Expand All @@ -144,17 +146,18 @@ def self.seed_default_events
end
end

def self.seed_default_definitions
def self.seed_default_definitions(event_defs)
stats = {:a => 0, :u => 0}

fname = File.join(FIXTURE_DIR, "miq_event_definitions.yml")
defns = YAML.load_file(fname)
defns.each do |event_type, events|
events[:events].each do |e|
event = find_by(:name => e[:name], :event_type => event_type.to_s)
event = (event_defs[e[:name]] || []).detect { |ed| ed.event_type == event_type.to_s }
if event.nil?
_log.info("Creating [#{e[:name]}]")
event = create(e.merge(:event_type => event_type.to_s, :default => true, :enabled => true))
(event_defs[e[:name]] ||= []) << event
stats[:a] += 1
else
event.attributes = e
Expand Down
2 changes: 1 addition & 1 deletion spec/models/bottleneck_event_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe BottleneckEvent do
describe ".future_event_definitions_for_obj" do
it "contains things" do
MiqEventDefinition.seed_default_definitions
MiqEventDefinition.seed_default_definitions(MiqEventDefinition.all.group_by(&:name))
expect(BottleneckEvent.future_event_definitions_for_obj(ManageIQ::Providers::Vmware::InfraManager::Host.new)).not_to be_empty
end
end
Expand Down
7 changes: 4 additions & 3 deletions spec/models/miq_event_definition_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
describe MiqEventDefinition do
let(:event_defs) { MiqEventDefinition.all.group_by(&:name) }
describe '.seed_default_events' do
context 'there are 2 event definition sets' do
let!(:set1) { create_set!('host_operations') }
Expand All @@ -16,7 +17,7 @@
context 'seeding default event definitions from that csv' do
before do
allow(File).to receive(:open).and_return(StringIO.new(csv))
MiqEventDefinition.seed_default_events
MiqEventDefinition.seed_default_events(event_defs)
end

it 'should create an event definition and make it a member of the set' do
Expand All @@ -37,7 +38,7 @@
context 'seeding again' do
before do
allow(File).to receive(:open).and_return(StringIO.new(csv))
MiqEventDefinition.seed_default_events
MiqEventDefinition.seed_default_events(event_defs)
end

it 'should update the membership' do
Expand Down Expand Up @@ -128,7 +129,7 @@ def create_set!(name)
context 'with defaults in db' do
before do
MiqEventDefinitionSet.seed
described_class.seed_default_events
described_class.seed_default_events(event_defs)
end

it "won't update an event with a definition (keyed as a string)" do
Expand Down