Class: ActionCost::RequestStats

Inherits:
Object
  • Object
show all
Defined in:
lib/action_cost/request_stats.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ RequestStats

Returns a new instance of RequestStats.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/action_cost/request_stats.rb', line 7

def initialize(env)
  begin
    routes_env = { :method => env['REQUEST_METHOD'] }
    request = Rails.application.routes.recognize_path(env['REQUEST_URI'], routes_env)

    @controller_name  = request[:controller]
    @action_name      = request[:action]
  rescue
    @controller_name  = nil
    @action_name      = nil
  end

  @operation_stats  = { :sql => {}, :rc => {} }
  ActionCost::SqlParser::VALID_OPERATIONS.each do |op|
    @operation_stats[:sql][op] = 0
    @operation_stats[:rc][op] = 0
  end

  @table_stats      = { :sql => {}, :rc => {} }
  @join_stats       = { :sql => {}, :rc => {} }

  action_cost_logfile = File.open(Rails.root.join("log", 'action_cost.log'), 'a')
  action_cost_logfile.sync = true
  @logger = Logger.new(action_cost_logfile)
  @logger.level = Logger::DEBUG
end

Instance Attribute Details

#action_nameObject (readonly)

Returns the value of attribute action_name.



4
5
6
# File 'lib/action_cost/request_stats.rb', line 4

def action_name
  @action_name
end

#controller_nameObject (readonly)

Returns the value of attribute controller_name.



4
5
6
# File 'lib/action_cost/request_stats.rb', line 4

def controller_name
  @controller_name
end

#join_statsObject (readonly)

Returns the value of attribute join_stats.



5
6
7
# File 'lib/action_cost/request_stats.rb', line 5

def join_stats
  @join_stats
end

#operation_statsObject (readonly)

Returns the value of attribute operation_stats.



5
6
7
# File 'lib/action_cost/request_stats.rb', line 5

def operation_stats
  @operation_stats
end

#table_statsObject (readonly)

Returns the value of attribute table_stats.



5
6
7
# File 'lib/action_cost/request_stats.rb', line 5

def table_stats
  @table_stats
end

Instance Method Details

#closeObject



52
53
54
# File 'lib/action_cost/request_stats.rb', line 52

def close
  log
end

#logObject



56
57
58
59
60
61
# File 'lib/action_cost/request_stats.rb', line 56

def log
  @logger.debug ""
  @logger.debug "=== ActionCost: #{@controller_name}##{@action_name}"
  log_by_query_type(:rc)
  log_by_query_type(:sql)
end

#push(parser) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/action_cost/request_stats.rb', line 34

def push(parser)
  return unless parser.parse
  parser.log

  return if parser.invalid

  case parser.class.to_s
  when 'ActionCost::SqlParser'          then query_type = :sql
  when 'ActionCost::RecordCacheParser'  then query_type = :rc
  end

  increment_item(@table_stats,     query_type, parser.table_name)
  increment_item(@operation_stats, query_type, parser.operation)
  parser.join_tables.each do |table|
    increment_item(@join_stats, query_type, join_string(parser.table_name, table))
  end
end