Sha256: 9d6a58e8d756feb3bfd7ad9be8d4ad844bfde622ae1a31f633a3f5d3249558c8

Contents?: true

Size: 1.79 KB

Versions: 5

Compression:

Stored size: 1.79 KB

Contents

module Blazer
  class Check < ActiveRecord::Base
    belongs_to :creator, Blazer::BELONGS_TO_OPTIONAL.merge(class_name: Blazer.user_class.to_s) if Blazer.user_class
    belongs_to :query

    validates :query_id, presence: true

    before_validation :set_state

    def set_state
      self.state ||= "new"
    end

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

    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).deliver_later
      end
      save! if changed?
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
blazer-1.7.0 app/models/blazer/check.rb
blazer-1.6.2 app/models/blazer/check.rb
blazer-1.6.1 app/models/blazer/check.rb
blazer-1.6.0 app/models/blazer/check.rb
blazer-1.5.1 app/models/blazer/check.rb