Class: Legion::Alerts::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/alerts.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules: []) ⇒ Engine

Returns a new instance of Engine.



33
34
35
36
37
# File 'lib/legion/alerts.rb', line 33

def initialize(rules: [])
  @rules = rules.map { |r| r.is_a?(AlertRule) ? r : AlertRule.new(**r.transform_keys(&:to_sym)) }
  @counters = {}
  @last_fired = {}
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



31
32
33
# File 'lib/legion/alerts.rb', line 31

def rules
  @rules
end

Instance Method Details

#evaluate(event_name, payload = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/legion/alerts.rb', line 39

def evaluate(event_name, payload = {})
  fired = []
  @rules.each do |rule|
    next unless event_matches?(event_name, rule.event_pattern)

    Legion::Logging.debug "[Alerts] evaluating rule=#{rule.name} for event=#{event_name}" if defined?(Legion::Logging)
    next unless condition_met?(rule, event_name)
    next if in_cooldown?(rule)

    fire_alert(rule, event_name, payload)
    fired << rule.name
  end
  fired
end