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

Allow mapping event types to groups using regexes #17772

Merged
merged 1 commit into from
Aug 10, 2018
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
18 changes: 13 additions & 5 deletions app/models/event_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,19 @@ def self.event_groups

def self.group_and_level(event_type)
level = :detail # the level is detail as default
group, _ = event_groups.find do |_k, value|
GROUP_LEVELS.detect { |lvl| value[lvl]&.include?(event_type) }.tap do |level_found|
level = level_found if level_found
end
end
egroups = event_groups

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

group ||= egroups.detect do |_, value|
GROUP_LEVELS
.detect { |lvl| value[lvl]&.any? { |typ| typ.kind_of?(Regexp) && typ.match(event_type) } }
.tap { |level_found| level = level_found || level }
end&.first

group ||= :other
return group, level
Expand Down
89 changes: 67 additions & 22 deletions spec/models/ems_event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@
let(:provider_critical_event) { 'SomeCriticalEvent' }
let(:provider_detail_event) { 'SomeDetailEvent' }
let(:provider_warning_event) { 'SomeWarningEvent' }
let(:provider_event) { 'SomeSpecialProviderEvent' }

it 'returns a list of expected groups' do
event_group_names = [
Expand All @@ -298,30 +297,11 @@
expect(described_class.event_groups.keys).to match_array(event_group_names)
expect(described_class.event_groups[:addition]).to include(:name => 'Creation/Addition')
expect(described_class.event_groups[:addition][:critical]).to include('CloneTaskEvent')
expect(described_class.event_groups[:addition][:critical]).not_to include(provider_event)
end

it 'returns the provider event if configured' do
stub_settings_merge(
:ems => {
:some_provider => {
:event_handling => {
:event_groups => {
:addition => {
:critical => [provider_event]
}
}
}
}
}
)

expect(described_class.event_groups[:addition][:critical]).to include('CloneTaskEvent')
expect(described_class.event_groups[:addition][:critical]).to include(provider_event)
expect(described_class.event_groups[:addition][:critical]).not_to include('BogueEvent')
end

it 'returns the group, level and group name of an unknown event' do
group, level = described_class.group_and_level(provider_event)
group, level = described_class.group_and_level('BogusEvent')
expect(group).to eq(:other)
expect(level).to eq(:detail)
expect(described_class.group_name(group)).to eq('Other')
Expand All @@ -347,5 +327,70 @@
expect(level).to eq(:detail)
expect(described_class.group_name(group)).to eq('Power Activity')
end

context 'with provider events' do
before(:each) do
stub_settings_merge(
:ems => {
:some_provider => {
:event_handling => {
:event_groups => {
:addition => {
:warning => [provider_regex],
:critical => [provider_event]
}
}
}
}
}
)
end

let(:provider_event) { 'SomeSpecialProviderEvent' }
let(:provider_regex) { /Some.+Event/ }

it 'returns the provider event if configured' do
expect(described_class.event_groups[:addition][:critical]).to include('CloneTaskEvent')
expect(described_class.event_groups[:addition][:critical]).to include(provider_event)
expect(described_class.event_groups[:addition][:warning]).to include(provider_regex)
end

# Make sure explicitly named event types take precedence over regex
it 'returns the group, level and group name of a warning event' do
group, level = described_class.group_and_level(provider_warning_event)
expect(group).to eq(:power)
expect(level).to eq(:warning)
expect(described_class.group_name(group)).to eq('Power Activity')
end

it 'returns the group, level and group name of a critical event' do
group, level = described_class.group_and_level(provider_critical_event)
expect(group).to eq(:power)
expect(level).to eq(:critical)
expect(described_class.group_name(group)).to eq('Power Activity')
end

it 'returns the group, level and group name of a detail event' do
group, level = described_class.group_and_level(provider_detail_event)
expect(group).to eq(:power)
expect(level).to eq(:detail)
expect(described_class.group_name(group)).to eq('Power Activity')
end
# End make sure explicitly named event types take precedence over regex

it 'returns the group, level and group name of a regex-matched event' do
group, level = described_class.group_and_level('SomeMatchingEvent')
expect(group).to eq(:addition)
expect(level).to eq(:warning)
expect(described_class.group_name(group)).to eq('Creation/Addition')
end

it 'returns the group, level and group name of an unknown event' do
group, level = described_class.group_and_level('BogusEvent')
expect(group).to eq(:other)
expect(level).to eq(:detail)
expect(described_class.group_name(group)).to eq('Other')
end
end
end
end