Skip to content

Commit

Permalink
Add ems_events for ems_event_filter_column for ems cluster subclasses
Browse files Browse the repository at this point in the history
EmsCluster was properly having ems_cluster_id as the ems_event_filter_column,
subclasses had cluster_id as the column.

Implementing a basic events <-> cluster lookup via foreign_key.  If desired
we can add something like event_where_clause has for showing hosts and vms events
in the cluster.  For now, that's too much performance overhead so we're looking
at just cluster events in the timeline.
  • Loading branch information
jrafanie committed Jan 9, 2025
1 parent d4f76b2 commit a55bf58
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
5 changes: 5 additions & 0 deletions app/models/ems_cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ def parent_datacenter
detect_ancestor(:of_type => 'EmsFolder') { |a| a.kind_of?(Datacenter) }
end

# TODO: event_where_clause tacks on additional possibly expensive queries
# such as all events for all hosts or vms in the cluster.
# For now, we're looking at ems events specifically for the ems cluster.
has_many :ems_events, :foreign_key => :ems_cluster_id, :class_name => "EmsEvent"

def event_where_clause(assoc = :ems_events)
return ["ems_cluster_id = ?", id] if assoc.to_sym == :policy_events

Expand Down
32 changes: 19 additions & 13 deletions spec/models/mixins/event_mixin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,26 @@ def event_where_clause(assoc)
VmOrTemplate vm_or_template_id
Vm vm_or_template_id
].each_slice(2) do |klass, column|
it "#{klass} uses #{column} and target_id and target_type" do
obj = FactoryBot.create(klass.tableize.singularize)
expect(obj.event_stream_filters["EmsEvent"]).to eq(column => obj.id)
expect(obj.event_stream_filters.dig("MiqEvent", "target_id")).to eq(obj.id)
expect(obj.event_stream_filters.dig("MiqEvent", "target_type")).to eq(obj.class.base_class.name)
end

it "#{klass} behaves like event_where_clause for ems_events" do
obj = FactoryBot.create(klass.tableize.singularize)
event = FactoryBot.create(:event_stream, column => obj.id)
FactoryBot.create(:event_stream)
expect(EventStream.where(obj.event_stream_filters["EmsEvent"]).to_a).to eq([event])
expect(EventStream.where(obj.event_where_clause(:ems_events)).to_a).to eq([event])
klasses = klass.constantize.descendants.collect(&:name).unshift(klass)
klasses.each do |klass|
it "#{klass} uses #{column} and target_id and target_type" do
begin
obj = FactoryBot.build(klass.tableize.singularize, :name => "test")
rescue NameError
skip "Unable to build factory from name: #{klass}, possibly due to inflections"
end
expect(obj.event_stream_filters["EmsEvent"]).to eq(column => obj.id)
expect(obj.event_stream_filters.dig("MiqEvent", "target_id")).to eq(obj.id)
expect(obj.event_stream_filters.dig("MiqEvent", "target_type")).to eq(obj.class.base_class.name)
end
end
it "#{klass} behaves like event_where_clause for ems_events" do
obj = FactoryBot.create(klass.tableize.singularize)
event = FactoryBot.create(:event_stream, column => obj.id)
FactoryBot.create(:event_stream)
expect(EventStream.where(obj.event_stream_filters["EmsEvent"]).to_a).to eq([event])
expect(EventStream.where(obj.event_where_clause(:ems_events)).to_a).to eq([event])
end

# # TODO: some classes don't have this implemented or don't have columns for this
# # Do we consolidate policy events and miq events?
Expand Down

0 comments on commit a55bf58

Please sign in to comment.