Class: AwesomeExplain::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/awesome_explain/config.rb

Constant Summary collapse

DEFAULT_DB_NAME =
:awesome_explain
POSTGRES_DEV_DBNAME =
'awesome_explain_development'
POSTGRES_DEFAULT_HOST =
'localhost'
POSTGRES_DEFAULT_USERNAME =
'postgres'
POSTGRES_DEFAULT_PASSWORD =
'postgres'
DEFAULT_DB_PATH =
'./log'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#active_record_enabledObject Also known as: active_record_enabled?

Returns the value of attribute active_record_enabled.



4
5
6
# File 'lib/awesome_explain/config.rb', line 4

def active_record_enabled
  @active_record_enabled
end

#adapterObject

Returns the value of attribute adapter.



4
5
6
# File 'lib/awesome_explain/config.rb', line 4

def adapter
  @adapter
end

#app_nameObject

Returns the value of attribute app_name.



4
5
6
# File 'lib/awesome_explain/config.rb', line 4

def app_name
  @app_name
end

#db_nameObject

Returns the value of attribute db_name.



4
5
6
# File 'lib/awesome_explain/config.rb', line 4

def db_name
  @db_name
end

#db_pathObject

Returns the value of attribute db_path.



4
5
6
# File 'lib/awesome_explain/config.rb', line 4

def db_path
  @db_path
end

#enabledObject Also known as: enabled?

Returns the value of attribute enabled.



4
5
6
# File 'lib/awesome_explain/config.rb', line 4

def enabled
  @enabled
end

#include_full_planObject

Returns the value of attribute include_full_plan.



4
5
6
# File 'lib/awesome_explain/config.rb', line 4

def include_full_plan
  @include_full_plan
end

#loggerObject

Returns the value of attribute logger.



4
5
6
# File 'lib/awesome_explain/config.rb', line 4

def logger
  @logger
end

#logsObject (readonly)

Returns the value of attribute logs.



4
5
6
# File 'lib/awesome_explain/config.rb', line 4

def logs
  @logs
end

#max_limitObject

Returns the value of attribute max_limit.



4
5
6
# File 'lib/awesome_explain/config.rb', line 4

def max_limit
  @max_limit
end

#queueObject (readonly)

Returns the value of attribute queue.



4
5
6
# File 'lib/awesome_explain/config.rb', line 4

def queue
  @queue
end

#rails_pathObject

Returns the value of attribute rails_path.



4
5
6
# File 'lib/awesome_explain/config.rb', line 4

def rails_path
  @rails_path
end

Class Method Details

.configure(&block) ⇒ Object



27
28
29
30
31
32
33
34
35
# 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?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/awesome_explain/config.rb', line 128

def self.rails4?
  Rails.version.start_with? '4'
end

Instance Method Details

#connectionObject



75
76
77
78
79
80
81
82
83
84
85
86
# 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_configObject



88
89
90
# File 'lib/awesome_explain/config.rb', line 88

def db_config
  adapter == :postgres ? postgres_config : sqlite3_config
end

#initObject



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
# 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

#postgres_configObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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_hostObject



176
177
178
# File 'lib/awesome_explain/config.rb', line 176

def postgres_host
  @postgres_host || 'localhost'
end

#postgres_host=(value = 'localhost') ⇒ Object



172
173
174
# File 'lib/awesome_explain/config.rb', line 172

def postgres_host=(value = 'localhost')
  @postgres_host = value
end

#postgres_passwordObject



192
193
194
# File 'lib/awesome_explain/config.rb', line 192

def postgres_password
  @postgres_password || 'postgres'
end

#postgres_password=(value = 'postgres') ⇒ Object



188
189
190
# File 'lib/awesome_explain/config.rb', line 188

def postgres_password=(value = 'postgres')
  @postgres_password = value
end

#postgres_usernameObject



184
185
186
# File 'lib/awesome_explain/config.rb', line 184

def postgres_username
  @postgres_username || 'postgres'
end

#postgres_username=(value = 'postgres') ⇒ Object



180
181
182
# File 'lib/awesome_explain/config.rb', line 180

def postgres_username=(value = 'postgres')
  @postgres_username = value
end

#sqlite3_configObject



117
118
119
120
121
122
123
124
125
126
# 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