Skip to content

Commit

Permalink
Merge pull request #14318 from moolitayer/always_evaluate_dwh
Browse files Browse the repository at this point in the history
Always evaluate datawarehouse_alerts
  • Loading branch information
gtanzillo authored Apr 6, 2017
2 parents 1589a3c + 6d0abfa commit aa40496
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
19 changes: 18 additions & 1 deletion app/models/miq_alert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class MiqAlert < ApplicationRecord

validates_presence_of :description, :guid
validates_uniqueness_of :description, :guid
validate :validate_automate_expressions

has_many :miq_alert_statuses, :dependent => :destroy
before_save :set_responds_to_events
Expand Down Expand Up @@ -72,6 +73,22 @@ def set_responds_to_events
self.responds_to_events = events unless events.nil?
end

def validate_automate_expressions
# if always_evaluate = true, delay_next_evaluation must be 0
valid = true
automate_expression = if expression.kind_of?(Hash) && self.class.expression_by_name(expression[:eval_method])
self.class.expression_by_name(expression[:eval_method])
else
{}
end
next_frequency = (options || {}).fetch_path(:notifications, :delay_next_evaluation)
if automate_expression[:always_evaluate] && next_frequency != 0
valid = false
errors.add(:notifications, "Datawarehouse alerts must have a 0 notification frequency")
end
valid
end

def self.assigned_to_target(target, event = nil)
# Get all assigned, enabled alerts based on target class and event

Expand Down Expand Up @@ -442,7 +459,7 @@ def self.automate_expressions
{:name => :value_mw_garbage_collector, :description => _("Duration Per Minute (ms)"), :numeric => true}
]},
{:name => "dwh_generic", :description => _("All Datawarehouse alerts"), :db => ["ContainerNode"], :responds_to_events => "datawarehouse_alert",
:options => []}
:options => [], :always_evaluate => true}
]
end

Expand Down
25 changes: 25 additions & 0 deletions spec/models/miq_alert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -472,4 +472,29 @@
msg.delivered(status, message, result)
end
end

describe '.validate_automate_expressions' do
it 'Does not allow creation of dwh_generic miq_alerts with delay_next_evaluation > 0 ' do
expect do
FactoryGirl.create(
:miq_alert,
:options => {:notifications => {:delay_next_evaluation => 600, :evm_event => {}}},
:expression => {:eval_method => "dwh_generic"}
)
end.to raise_error(
ActiveRecord::RecordInvalid,
'Validation failed: Notifications Datawarehouse alerts must have a 0 notification frequency'
)
end

it 'Does allow creation of hawkular_alert miq_alerts with delay_next_evaluation > 0 ' do
expect do
FactoryGirl.create(
:miq_alert,
:options => {:notifications => {:delay_next_evaluation => 600, :evm_event => {}}},
:expression => {:eval_method => "mw_heap_used"}
)
end.to_not raise_error
end
end
end
4 changes: 2 additions & 2 deletions spec/requests/api/alert_definitions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@
sample_alert_definition = {
:description => "Test Alert Definition",
:db => "ContainerNode",
:expression => { :eval_method => "dwh_generic", :mode => "internal", :options => {} },
:options => { :notifications => {:delay_next_evaluation => 600, :evm_event => {} } },
:expression => { :eval_method => "mw_heap_used", :mode => "internal", :options => {} },
:options => { :notifications => {:delay_next_evaluation => 0, :evm_event => {} } },
:enabled => true
}
updated_options = { :notifications => {:delay_next_evaluation => 60, :evm_event => {} } }
Expand Down

0 comments on commit aa40496

Please sign in to comment.