module Blazer

Constants

BELONGS_TO_OPTIONAL
TIMEOUT_ERRORS
TIMEOUT_MESSAGE
VERSION

Attributes

anomaly_checks[RW]
assignees[RW]
async[RW]
audit[RW]
before_action[RW]
cache[RW]
check_schedules[RW]
forecasting[RW]
from_email[RW]
host[RW]
images[RW]
integration[RW]
mapbox_access_token[RW]
override_csp[RW]
preview_rows_number[RW]
query_creatable[RW]
query_editable[RW]
query_viewable[RW]
slack_webhook_url[RW]
teams[RW]
time_zone[R]
transform_statement[RW]
transform_variable[RW]
user_class[W]
user_method[W]
user_name[RW]

Public Class Methods

adapters() click to toggle source
# File lib/blazer.rb, line 234
def self.adapters
  @adapters ||= {}
end
data_sources() click to toggle source
# File lib/blazer.rb, line 129
def self.data_sources
  @data_sources ||= begin
    ds = Hash.new { |hash, key| raise Blazer::Error, "Unknown data source: #{key}" }
    settings["data_sources"].each do |id, s|
      ds[id] = Blazer::DataSource.new(id, s)
    end
    ds.default = ds.values.first
    ds
  end
end
extract_vars(statement) click to toggle source
# File lib/blazer.rb, line 140
def self.extract_vars(statement)
  # strip commented out lines
  # and regex {1} or {1,2}
  statement.gsub(/\-\-.+/, "").gsub(/\/\*.+\*\//m, "").scan(/\{\w*?\}/i).map { |v| v[1...-1] }.reject { |v| /\A\d+(\,\d+)?\z/.match(v) || v.empty? }.uniq
end
register_adapter(name, adapter) click to toggle source
# File lib/blazer.rb, line 238
def self.register_adapter(name, adapter)
  adapters[name] = adapter
end
run_check(check) click to toggle source
# File lib/blazer.rb, line 160
def self.run_check(check)
  tries = 1

  ActiveSupport::Notifications.instrument("run_check.blazer", check_id: check.id, query_id: check.query.id, state_was: check.state) do |instrument|
    # try 3 times on timeout errors
    data_source = data_sources[check.query.data_source]
    statement = check.query.statement
    Blazer.transform_statement.call(data_source, statement) if Blazer.transform_statement

    while tries <= 3
      result = data_source.run_statement(statement, refresh_cache: true, check: check, query: check.query)
      if result.timed_out?
        Rails.logger.info "[blazer timeout] query=#{check.query.name}"
        tries += 1
        sleep(10)
      elsif result.error.to_s.start_with?("PG::ConnectionBad")
        data_source.reconnect
        Rails.logger.info "[blazer reconnect] query=#{check.query.name}"
        tries += 1
        sleep(10)
      else
        break
      end
    end

    begin
      check.reload # in case state has changed since job started
      check.update_state(result)
    rescue ActiveRecord::RecordNotFound
      # check deleted
    end

    # TODO use proper logfmt
    Rails.logger.info "[blazer check] query=#{check.query.name} state=#{check.state} rows=#{result.rows.try(:size)} error=#{result.error}"

    instrument[:statement] = statement
    instrument[:data_source] = data_source
    instrument[:state] = check.state
    instrument[:rows] = result.rows.try(:size)
    instrument[:error] = result.error
    instrument[:tries] = tries
  end
end
run_checks(schedule: nil) click to toggle source
# File lib/blazer.rb, line 151
def self.run_checks(schedule: nil)
  checks = Blazer::Check.includes(:query)
  checks = checks.where(schedule: schedule) if schedule
  checks.find_each do |check|
    next if check.state == "disabled"
    Safely.safely { run_check(check) }
  end
end
send_failing_after_checks(schedule: nil) click to toggle source
# File lib/blazer.rb, line 146
def self.send_failing_after_checks(schedule: nil)
  run_checks(schedule)
  send_failing_checks
end
send_failing_checks() click to toggle source
# File lib/blazer.rb, line 204
def self.send_failing_checks
  emails = {}
  slack_channels = {}

  Blazer::Check.includes(:query).where(state: ["failing", "error", "timed out", "disabled"]).find_each do |check|
    check.split_emails.each do |email|
      (emails[email] ||= []) << check
    end
    check.split_slack_channels.each do |channel|
      (slack_channels[channel] ||= []) << check
    end
  end

  emails.each do |email, checks|
    Safely.safely do
      Blazer::CheckMailer.failing_checks(email, checks).deliver_now
    end
  end

  slack_channels.each do |channel, checks|
    Safely.safely do
      Blazer::SlackNotifier.failing_checks(channel, checks)
    end
  end
end
settings() click to toggle source
# File lib/blazer.rb, line 118
def self.settings
  @settings ||= begin
    path = Rails.root.join("config", "blazer.yml").to_s
    if File.exist?(path)
      YAML.load(ERB.new(File.read(path)).result)
    else
      {}
    end
  end
end
slack?() click to toggle source
# File lib/blazer.rb, line 230
def self.slack?
  slack_webhook_url.present?
end
time_zone=(time_zone) click to toggle source
# File lib/blazer.rb, line 92
def self.time_zone=(time_zone)
  @time_zone = time_zone.is_a?(ActiveSupport::TimeZone) ? time_zone : ActiveSupport::TimeZone[time_zone.to_s]
end
user_class() click to toggle source
# File lib/blazer.rb, line 101
def self.user_class
  if !defined?(@user_class)
    @user_class = settings.key?("user_class") ? settings["user_class"] : (User.name rescue nil)
  end
  @user_class
end
user_method() click to toggle source
# File lib/blazer.rb, line 108
def self.user_method
  if !defined?(@user_method)
    @user_method = settings["user_method"]
    if user_class
      @user_method ||= "current_#{user_class.to_s.downcase.singularize}"
    end
  end
  @user_method
end