Skip to content

Commit

Permalink
Add a new event group level
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlleDaniel committed Jun 21, 2018
1 parent f1ef359 commit 45bbdd3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
21 changes: 12 additions & 9 deletions app/models/event_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class EventStream < ApplicationRecord

after_commit :emit_notifications, :on => :create

GROUP_LEVELS = %i(critical detail warning).freeze

def emit_notifications
Notification.emit_for_event(self)
rescue => err
Expand All @@ -54,20 +56,21 @@ def self.event_groups
end

def self.group_and_level(event_type)
group, v = event_groups.find { |_k, v| v[:critical].include?(event_type) || v[:detail].include?(event_type) }
if group.nil?
group, level = :other, :detail
else
level = v[:detail].include?(event_type) ? :detail : :critical
level = :detail # the level is detail as default
group = event_groups.find do |_k, value|
lvl_found = GROUP_LEVELS.detect { |lvl| value[lvl]&.include?(event_type) }
level = lvl_found if lvl_found # update the level variable if found the current level.
lvl_found # the level found or nil
end

group = group.kind_of?(Array) ? group.first : :other
return group, level
end

def self.group_name(group)
return nil if group.nil?
group = event_groups[group.to_sym]
return nil if group.nil?
group[:name]
return if group.nil?
group = event_groups[group]
group.nil? ? 'Other' : group[:name]
end

def group
Expand Down
49 changes: 49 additions & 0 deletions spec/models/ems_event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,27 @@
end

context '.event_groups' do
before(:each) do
stub_settings_merge(
:ems => {
:some_provider => {
:event_handling => {
:event_groups => {
:power => {
:warning => [provider_warning_event],
:critical => [provider_critical_event],
:detail => [provider_detail_event],
}
}
}
}
}
)
end

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
Expand Down Expand Up @@ -297,5 +318,33 @@
expect(described_class.event_groups[:addition][:critical]).to include('CloneTaskEvent')
expect(described_class.event_groups[:addition][:critical]).to include(provider_event)
end

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

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
end

0 comments on commit 45bbdd3

Please sign in to comment.