Class: Blazer::Check

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

Instance Method Summary collapse

Instance Method Details

#split_emailsObject



13
14
15
# File 'app/models/blazer/check.rb', line 13

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

#split_slack_channelsObject



17
18
19
20
21
22
23
# File 'app/models/blazer/check.rb', line 17

def split_slack_channels
  if Blazer.slack?
    slack_channels.to_s.downcase.split(",").map(&:strip)
  else
    []
  end
end

#update_state(result) ⇒ Object



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
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/blazer/check.rb', line 25

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
    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 if emails.present?
    Blazer::SlackNotifier.state_change(self, state, state_was, result.rows.size, message, check_type)
  end
  save! if changed?
end