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

Fix group level and names for miq event #21569

Merged
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
10 changes: 7 additions & 3 deletions app/models/ems_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ class EmsEvent < EventStream
'Rename_Task',
]

CLASS_GROUP_LEVELS = %i[critical warning].freeze
Copy link
Member

Choose a reason for hiding this comment

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

For i18n, I think the better way to do this might be to make this:

Suggested change
CLASS_GROUP_LEVELS = %i[critical warning].freeze
CLASS_GROUP_LEVELS = [
N_("Critical"),
N_("Warning")
].freeze

Then later, or perhaps in another constant, do CLASS_GROUP_LEVELS.map { |l| l.downcase.to_sym }

This way, the strings are in the translation catalog, but the code can still use symbols.


def self.description
_("Management Events")
end

def self.class_group_levels
CLASS_GROUP_LEVELS
end

def self.group_names_and_levels
result = {:description => description}
event_groups.each_with_object(result) do |(group_name, group_details), hash|
hash[:group_names] ||= {}
event_groups.each_with_object(default_group_names_and_levels) do |(group_name, group_details), hash|
hash[:group_names][group_name] = group_details[:name]

group_details.each_key do |level|
Expand Down
30 changes: 23 additions & 7 deletions app/models/event_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@ class EventStream < ApplicationRecord

after_commit :emit_notifications, :on => :create

# TODO: Consider moving since this is EmsEvent specific. group, group_level and group_name exposed as a virtual columns for reports/api.
GROUP_LEVELS = %i(critical detail warning).freeze
Copy link
Member Author

Choose a reason for hiding this comment

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

☝️ This was mixing EmsEvent group levels with EventStream. Critical and warning were moved to EmsEvent, detail remains as a default here.

DEFAULT_GROUP_NAME = :other
DEFAULT_GROUP_LEVEL = :detail
Comment on lines +38 to +39
Copy link
Member

Choose a reason for hiding this comment

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

Similarly, for i18n here, you'd do something like:

Suggested change
DEFAULT_GROUP_NAME = :other
DEFAULT_GROUP_LEVEL = :detail
DEFAULT_GROUP_NAME = N_("Other")
DEFAULT_GROUP_LEVEL = N_("Detail")

Then later or perhaps in another constant you'd do DEFAULT_GROUP_NAME.downcase.to_sym


def self.description
raise NotImplementedError, "Description must be implemented in a subclass"
end

def self.class_group_levels
[]
end

def self.group_levels
class_group_levels + [DEFAULT_GROUP_LEVEL]
end

def emit_notifications
Notification.emit_for_event(self)
rescue => err
Expand All @@ -64,29 +72,29 @@ def self.event_groups

# TODO: Consider moving since this is EmsEvent specific. group, group_level and group_name exposed as a virtual columns for reports/api.
def self.group_and_level(event_type)
level = :detail # the level is detail as default
level = DEFAULT_GROUP_LEVEL # the level is detail as default
egroups = event_groups

group = egroups.detect do |_, value|
GROUP_LEVELS
group_levels
.detect { |lvl| value[lvl]&.any? { |typ| !typ.starts_with?("/") && typ == event_type } }
.tap { |level_found| level = level_found || level }
end&.first

group ||= egroups.detect do |_, value|
GROUP_LEVELS
group_levels
.detect { |lvl| value[lvl]&.any? { |typ| typ.starts_with?("/") && Regexp.new(typ[1..-2]).match?(event_type) } }
.tap { |level_found| level = level_found || level }
end&.first

group ||= :other
group ||= DEFAULT_GROUP_NAME
return group, level
end

def self.group_name(group)
return if group.nil?
group = event_groups[group.to_sym]
group.nil? ? 'Other' : group[:name]
group.nil? ? DEFAULT_GROUP_NAME.to_s.capitalize : group[:name]
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if this will break I18N - we may have to figure that out in a follow up.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I don't know how to test it. I can ask around and do it in a followup.

end

# TODO: Consider moving since this is EmsEvent specific. group, group_level and group_name exposed as a virtual columns for reports/api.
Expand All @@ -112,6 +120,14 @@ def self.timeline_classes
EventStream.subclasses.select { |e| e.respond_to?(:group_names_and_levels) }
end

def self.default_group_names_and_levels
{
:description => description,
:group_names => {DEFAULT_GROUP_NAME => DEFAULT_GROUP_NAME.to_s.capitalize},
:group_levels => {}
}.freeze
end

def self.timeline_options
timeline_classes.map { |c| [c.name.to_sym, c.group_names_and_levels] }.to_h
end
Expand Down
18 changes: 15 additions & 3 deletions app/models/miq_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,26 @@ class MiqEvent < EventStream
ContainerReplicator, ContainerGroup, ContainerProject,
ContainerNode, ContainerImage, PhysicalServer].freeze

CLASS_GROUP_LEVELS = [MiqPolicy::CONDITION_SUCCESS, MiqPolicy::CONDITION_FAILURE].collect { |level| level.downcase.to_sym }

def self.description
_("Management Events")
end

def self.class_group_levels
CLASS_GROUP_LEVELS
end

def self.description
_("Policy Events")
end

def self.group_names_and_levels
hash = {:description => description}
hash[:group_names] = MiqEventDefinitionSet.all.pluck(:name, :description).to_h.symbolize_keys
hash[:group_levels] = [MiqPolicy::CONDITION_SUCCESS, MiqPolicy::CONDITION_FAILURE].index_by { |c| c.downcase.to_sym }
hash = default_group_names_and_levels
hash[:group_names].merge!(MiqEventDefinitionSet.all.pluck(:name, :description).to_h.symbolize_keys)
group_levels.each do |level|
hash[:group_levels][level] ||= level.to_s.capitalize
Copy link
Member

Choose a reason for hiding this comment

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

Similar here - this may cause i18n issues unless the target string is already in the .po.

end
hash
end

Expand Down
12 changes: 8 additions & 4 deletions spec/models/event_stream_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
RSpec.describe EventStream do
describe ".event_groups" do
EventStream.event_groups.each do |group_name, group_data|
EventStream::GROUP_LEVELS.each do |level|
(EmsEvent.group_levels + MiqEvent.group_levels).each do |level|
group_data[level]&.each do |typ|
it ":#{group_name}/:#{level}/#{typ} is string or regex", :providers_common => true do
expect(typ.kind_of?(Regexp) || typ.kind_of?(String)).to eq(true)
Expand Down Expand Up @@ -37,12 +37,16 @@
expect(options[:EmsEvent].keys.sort).to eq %i[description group_levels group_names]
expect(options[:EmsEvent][:description]).to eq(EmsEvent.description)
expect(options[:EmsEvent][:group_levels].keys.sort).to eq %i[critical detail warning]
expect(options[:EmsEvent][:group_names].keys).to include(*%i[addition configuration console deletion devices])
expect(options[:EmsEvent][:group_levels].values.sort).to eq %w[Critical Detail Warning]
expect(options[:EmsEvent][:group_names].keys).to include(*%i[addition configuration console deletion devices other])
expect(options[:EmsEvent][:group_names].values).to include(*%w[Network Status Other])

expect(options[:MiqEvent].keys.sort).to eq %i[description group_levels group_names]
expect(options[:MiqEvent][:description]).to eq(MiqEvent.description)
expect(options[:MiqEvent][:group_levels].keys.sort).to eq %i[failure success]
expect(options[:MiqEvent][:group_names].keys).to include(*%i[auth_validation authentication compliance container_operations ems_operations evm_operations])
expect(options[:MiqEvent][:group_levels].keys.sort).to eq %i[detail failure success]
expect(options[:MiqEvent][:group_levels].values.sort).to eq %w[Detail Failure Success]
expect(options[:MiqEvent][:group_names].keys).to include(*%i[auth_validation authentication compliance container_operations ems_operations evm_operations other])
expect(options[:MiqEvent][:group_names].values).to include(*%w[Other Compliance])
end
end
end