Module: ReportsMash::Control::InstanceMethods

Included in:
ReportsMash::Control
Defined in:
lib/reportsmash/control/instance_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#envObject

Returns the value of attribute env.



5
6
7
# File 'lib/reportsmash/control/instance_methods.rb', line 5

def env
  @env
end

Instance Method Details

#init_plugin(options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/reportsmash/control/instance_methods.rb', line 6

def init_plugin(options={})
  # initialize the engine instance
  ReportsMash::Engine.engine = ReportsMash::Engine::Engine.instance
  ReportsMash::Engine.options = options

  $stderr.puts "Initializing plugin ReportsMash - #{Process.pid}\n"
  setup_rack_hooks(options[:config])
  setup_database_hooks(options[:config], ReportsMash::Engine.engine)
  setup_render_hooks(options[:config], ReportsMash::Engine.engine)

  # if server is forking (eg, unicorn), we should not start the worker thread here
  # it will be handled by Engine.transaction_start method
  ReportsMash::Engine.engine.start_worker unless server_is_forking?
end

#object_running?(o) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
# File 'lib/reportsmash/control/instance_methods.rb', line 58

def object_running?(o)
  ObjectSpace.each_object(o) do |exists|
    return true
  end
  return false
end

#server_is_forking?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
# File 'lib/reportsmash/control/instance_methods.rb', line 51

def server_is_forking?
  # check for unicorn server
  return true if defined?(::Unicorn) && defined?(::Unicorn::HttpServer) && object_running?(::Unicorn::HttpServer)

  false
end

#setup_database_hooks(config, engine) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/reportsmash/control/instance_methods.rb', line 31

def setup_database_hooks(config, engine)
  ActiveSupport::Notifications.subscribe('sql.active_record') do |name, start, finish, id, payload|
    unless payload[:name] == 'SCHEMA'
      engine.save_db_transaction(id, (finish-start)*1000.0, payload[:sql], payload[:connection_id])
    end
  end
end

#setup_rack_hooks(config) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/reportsmash/control/instance_methods.rb', line 21

def setup_rack_hooks(config)
    return if config.nil? || !config.respond_to?(:middleware)
    begin
      require 'reportsmash/rack/hooks'
      config.middleware.use ReportsMash::Rack::Hooks
    rescue => e
      #::ReportsMash::Engine.logger.warn("Error installing ReportsMash Rack middleware", e)
    end        
end

#setup_render_hooks(config, engine) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/reportsmash/control/instance_methods.rb', line 39

def setup_render_hooks(config, engine)
  ActiveSupport::Notifications.subscribe('render_template.action_view') do |name, start, finish, id, payload|
    view_name = payload[:identifier].split('app/views')[1]
    engine.save_render_transaction(id, (finish-start)*1000.0, 'app/views'+view_name)
  end
  ActiveSupport::Notifications.subscribe('render_partial.action_view') do |name, start, finish, id, payload|
    view_name = payload[:identifier].split('app/views')[1]
    Rails.logger.debug "#{view_name} - #{(finish-start)*1000.0}"
    engine.save_render_transaction(id, (finish-start)*1000.0, 'app/views'+view_name)
  end
end