Class: Perus::Server::Alert

Inherits:
Sequel::Model
  • Object
show all
Defined in:
lib/perus/server/models/alert.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cache_active_alertsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/perus/server/models/alert.rb', line 30

def self.cache_active_alerts
    puts 'Caching active alerts'
    start = Time.now
    systems = System.all

    Alert.each do |alert|
        begin
            # active_alerts are left as is if they are still active
            currently_active = {}
            alert.active_alerts.each do |active_alert|
                currently_active[active_alert.system_id] = active_alert
            end

            # remove active alerts if they're no longer valid, add new
            # ones as needed.
            systems.each do |system|
                active = alert.execute(system)
                if active && !currently_active.include?(system.id)
                    ActiveAlert.add(alert, system)
                elsif !active && currently_active.include?(system.id)
                    currently_active[system.id].destroy
                end
            end

            # no errors occurred by this point, so remove the error
            # string if it exists from a previous run
            alert.errors = nil
        rescue => e
            alert.errors = e.inspect
        end
        
        # update alert.errors
        alert.save
    end

    puts "Caching complete, took #{Time.now - start}s"
end

Instance Method Details

#after_destroyObject



25
26
27
28
# File 'lib/perus/server/models/alert.rb', line 25

def after_destroy
    super
    active_alerts.each(&:destroy)
end

#execute(system) ⇒ Object



5
6
7
# File 'lib/perus/server/models/alert.rb', line 5

def execute(system)
    system.instance_eval(code)
end

#execute_errorsObject



9
10
11
# File 'lib/perus/server/models/alert.rb', line 9

def execute_errors
    values[:errors]
end

#severity_levelObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/perus/server/models/alert.rb', line 13

def severity_level
    if active_alerts_dataset.empty?
        0
    elsif severity == 'notice'
        1
    elsif severity == 'warning'
        2
    elsif severity == 'error'
        3
    end
end