Class: Blazer::Check

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/blazer/check.rb

Instance Method Summary collapse

Instance Method Details

#split_emailsObject



11
12
13
# File 'app/models/blazer/check.rb', line 11

def split_emails
  emails.to_s.downcase.split(",").map(&:strip)
end

#update_state(result) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
# File 'app/models/blazer/check.rb', line 15

def update_state(result)
  check_type =
    if respond_to?(:check_type)
      self.check_type
    elsif respond_to?(:invert)
      invert ? "missing_data" : "bad_data"
    else
      "bad_data"
    end

  message = result.error

  self.state =
    if result.timed_out?
      "timed out"
    elsif result.error
      "error"
    elsif check_type == "anomaly"
      anomaly, message = result.detect_anomaly
      if anomaly.nil?
        "error"
      elsif anomaly
        "failing"
      else
        "passing"
      end
    elsif result.rows.any?
      check_type == "missing_data" ? "passing" : "failing"
    else
      check_type == "missing_data" ? "failing" : "passing"
    end

  self.last_run_at = Time.now if respond_to?(:last_run_at=)
  self.message = message if respond_to?(:message=)

  if respond_to?(:timeouts=)
    if result.timed_out?
      self.timeouts += 1
      self.state = "disabled" if timeouts >= 3
    else
      self.timeouts = 0
    end
  end

  # do not notify on creation, except when not passing
  if (state_was != "new" || state != "passing") && state != state_was && emails.present?
    Blazer::CheckMailer.state_change(self, state, state_was, result.rows.size, message, result.columns, result.rows.first(10).as_json, result.column_types, check_type).deliver_now
  end
  save! if changed?
end