class AwesomeExplain::Config

Constants

DEFAULT_DB_NAME
DEFAULT_DB_PATH
POSTGRES_DEFAULT_HOST
POSTGRES_DEFAULT_PASSWORD
POSTGRES_DEFAULT_USERNAME
POSTGRES_DEV_DBNAME

Attributes

active_record_enabled[R]
active_record_enabled?[R]
adapter[R]
app_name[R]
db_name[R]
db_path[R]
enabled[R]
enabled?[R]
include_full_plan[R]
logger[R]
logs[R]
max_limit[R]
queue[R]
rails_path[R]

Public Class Methods

configure(&block) click to toggle source
# File lib/awesome_explain/config.rb, line 27
def self.configure(&block)
  raise NoBlockGivenException unless block_given?

  instance = Config.instance
  instance.instance_eval(&block)
  instance.init

  instance
end
rails4?() click to toggle source
# File lib/awesome_explain/config.rb, line 128
def self.rails4?
  Rails.version.start_with? '4'
end

Public Instance Methods

active_record_enabled=(value = false) click to toggle source
# File lib/awesome_explain/config.rb, line 148
def active_record_enabled=(value = false)
  @active_record_enabled = value
end
adapter=(value = :sqlite) click to toggle source
# File lib/awesome_explain/config.rb, line 168
def adapter=(value = :sqlite)
  @adapter = value
end
app_name=(value = :rails) click to toggle source
# File lib/awesome_explain/config.rb, line 160
def app_name=(value = :rails)
  @app_name = value
end
connection() click to toggle source
# File lib/awesome_explain/config.rb, line 75
def connection
  # TODO: Improve this condition
  if AwesomeExplain::Config.rails4?
    connection = ::ActiveRecord::Base.connection_handler.connection_pools.first.last.connection
  else
    connection = ::ActiveRecord::Base.connection_handler.connection_pools.select do |cp|
      !cp.connection.current_database.match(/awesome_explain/)
    end.first.connection
  end

  connection
end
db_config() click to toggle source
# File lib/awesome_explain/config.rb, line 88
def db_config
  adapter == :postgres ? postgres_config : sqlite3_config
end
db_name=(value = DEFAULT_DB_NAME) click to toggle source
# File lib/awesome_explain/config.rb, line 132
def db_name=(value = DEFAULT_DB_NAME)
  @db_name = "#{value.to_s}.db"
end
db_path=(value = DEFAULT_DB_PATH) click to toggle source
# File lib/awesome_explain/config.rb, line 136
def db_path=(value = DEFAULT_DB_PATH)
  @db_path = value
end
enabled=(value = false) click to toggle source
# File lib/awesome_explain/config.rb, line 144
def enabled=(value = false)
  @enabled = value
end
include_full_plan=(value = false) click to toggle source
# File lib/awesome_explain/config.rb, line 152
def include_full_plan=(value = false)
  @include_full_plan = value
end
init() click to toggle source
# File lib/awesome_explain/config.rb, line 37
def init
  return unless enabled
  @logs = []

  if Rails.env.development?
    # Misc
    ActiveSupport::Notifications.subscribe 'start_processing.action_controller' do |*args|
      data = args.extract_options!
      unless data[:controller] =~ /AwesomeExplain/ || data[:controller] =~ /ErrorsController/ || data[:path] =~ /awesome_explain/
        Thread.current[:ae_controller_data] = data
      end
      Thread.current[:ae_session_id] = SecureRandom.uuid
    end

    ActiveSupport::Notifications.subscribe 'process_action.action_controller' do |*args|
      Thread.current[:ae_session_id] = nil
    end

    # Mongoid
    if Rails.const_defined?('Mongo') && Rails.const_defined?('Mongoid')
      command_subscribers = Mongo::Monitoring::Global.subscribers.dig('Command')
      if command_subscribers.nil? || !command_subscribers.collect(&:class).include?(AwesomeExplain::Subscribers::CommandSubscriber)
        command_subscriber = AwesomeExplain::Subscribers::CommandSubscriber.new
        begin
          Mongoid.default_client.subscribe(Mongo::Monitoring::COMMAND, command_subscriber)
        rescue => exception
          Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::COMMAND, command_subscriber)
        end
      end
    end

    # ActiveRecord
    if active_record_enabled
      ::AwesomeExplain::Subscribers::ActiveRecordSubscriber.attach_to :active_record
    end
  end
end
logger=(value = nil) click to toggle source
# File lib/awesome_explain/config.rb, line 164
def logger=(value = nil)
  @logger = value.nil? ? Logger.new(STDOUT) : value
end
max_limit=(value = :unlimited) click to toggle source
# File lib/awesome_explain/config.rb, line 156
def max_limit=(value = :unlimited)
  @max_limit = value
end
postgres_config() click to toggle source
# File lib/awesome_explain/config.rb, line 92
def postgres_config
  {
    ae_development: {
      adapter: 'postgresql',
      encoding: 'utf8',
      host: postgres_host || POSTGRES_DEFAULT_HOST,
      database: POSTGRES_DEV_DBNAME,
      username: postgres_username || POSTGRES_DEFAULT_USERNAME,
      password: postgres_password || POSTGRES_DEFAULT_PASSWORD,
      pool: 5,
      timeout: 5000,
    },
    ae_staging: {
      adapter: 'postgresql',
      encoding: 'utf8',
      host: postgres_host || POSTGRES_DEFAULT_HOST,
      database: POSTGRES_DEV_DBNAME,
      username: postgres_username || POSTGRES_DEFAULT_USERNAME,
      password: postgres_password || POSTGRES_DEFAULT_PASSWORD,
      pool: 5,
      timeout: 5000,
    }
  }.with_indifferent_access["ae_#{Rails.env}"]
end
postgres_host() click to toggle source
# File lib/awesome_explain/config.rb, line 176
def postgres_host
  @postgres_host || 'localhost'
end
postgres_host=(value = 'localhost') click to toggle source
# File lib/awesome_explain/config.rb, line 172
def postgres_host=(value = 'localhost')
  @postgres_host = value
end
postgres_password() click to toggle source
# File lib/awesome_explain/config.rb, line 192
def postgres_password
  @postgres_password || 'postgres'
end
postgres_password=(value = 'postgres') click to toggle source
# File lib/awesome_explain/config.rb, line 188
def postgres_password=(value = 'postgres')
  @postgres_password = value
end
postgres_username() click to toggle source
# File lib/awesome_explain/config.rb, line 184
def postgres_username
  @postgres_username || 'postgres'
end
postgres_username=(value = 'postgres') click to toggle source
# File lib/awesome_explain/config.rb, line 180
def postgres_username=(value = 'postgres')
  @postgres_username = value
end
rails_path=(value) click to toggle source
# File lib/awesome_explain/config.rb, line 140
def rails_path=(value)
  @rails_path = value
end
sqlite3_config() click to toggle source
# File lib/awesome_explain/config.rb, line 117
def sqlite3_config
  {
    ae_development: {
      adapter: 'sqlite3',
      database: "#{db_path || './log'}/ae.db",
      pool: 5,
      timeout: 5000,
    }
  }.with_indifferent_access["ae_#{Rails.env}"]
end