Class: SlowActions

Inherits:
Object
  • Object
show all
Defined in:
lib/slow_actions.rb,
lib/slow_actions/slow_actions_action.rb,
lib/slow_actions/slow_actions_parser.rb,
lib/slow_actions/slow_actions_session.rb,
lib/slow_actions/slow_actions_log_entry.rb,
lib/slow_actions/slow_actions_controller.rb

Overview

SlowActions class that is the master controller for processing slow actions

Defined Under Namespace

Classes: Action, Controller, LogEntry, Parser, Session

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ SlowActions

Takes an options hash where you can specify :start_date and :end_date as “YYYY-MM-DD” strings



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/slow_actions.rb', line 11

def initialize(opts = {})
  @log_entries = []
  if opts[:start_date]
    @start_date = opts[:start_date]
  else
    @start_date = '0000-00-00' 
  end
  if opts[:end_date]
    @end_date = opts[:end_date]
  else
    @end_date = "#{Date.today.year}-#{Date.today.month}-#{Date.today.day}"
  end
end

Instance Method Details

#actionsObject

All the #Action objects



113
114
115
# File 'lib/slow_actions.rb', line 113

def actions
  @actions.values
end

#controllersObject

All the #Controller objects.



108
109
110
# File 'lib/slow_actions.rb', line 108

def controllers
  @controllers.values
end

#log_entriesObject

All the #LogEntry objects



33
34
35
# File 'lib/slow_actions.rb', line 33

def log_entries
  return @log_entries
end

#parse_file(file_path, *args) ⇒ Object

Parse the file found at “file_path” and add the log entries to its collection of entries.



26
27
28
29
30
# File 'lib/slow_actions.rb', line 26

def parse_file(file_path, *args)
  parser = Parser.new(file_path, @start_date, @end_date)
  @log_entries += parser.parse(*args)
  process
end

Print out all the actions and their statistics

:mincost  Lower bound on the cost of this action. See Computable.
:minavg   Lower bound on the average amount of time this action ever took
:minmax   Lower bound on the maximum amount of time this action ever took


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/slow_actions.rb', line 42

def print_actions(opts = {})
  str = ""
  str += "           Cost    Average Max\n"
  actions.sort{|x,y| y.total_cost <=> x.total_cost}.each do |a|
    next if opts[:mincost] and a.total_cost < opts[:mincost]
    next if opts[:minavg] and a.total_avg < opts[:minavg]
    next if opts[:minmax] and a.total_max < opts[:minmax]
    str += "- #{a.controller.name} : #{a.name} "
    str += "(#{a.log_entries.size} entries, #{(a.error_avg*100).to_i}% Error)\n"
    str += "  Total:   #{ftos a.total_cost}#{ftos a.total_avg}#{ftos a.total_max}\n"
    str += "  Render:  #{ftos a.render_cost}#{ftos a.render_avg}#{ftos a.render_max}\n"
    str += "  DB:      #{ftos a.db_cost}#{ftos a.db_avg}#{ftos a.db_max}\n"
    str += "\n"
  end
  return str
end

Print out all the controllers and their statistics, nesting actions as a tree. See #print_actions for options.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/slow_actions.rb', line 60

def print_controller_tree(opts = {})
  str = ""
  str += "             Cost    Average Max\n"
  controllers.sort{|x,y| y.total_cost <=> x.total_cost}.each do |c|
    next if opts[:mincost] and c.total_cost < opts[:mincost]
    next if opts[:minavg] and c.total_avg < opts[:minavg]
    next if opts[:minmax] and c.total_max < opts[:minmax]
    str += "+ #{c.name} (#{c.log_entries.size} entries, #{(c.error_avg*100).to_i}% Error)\n"
    str += "| Total:     #{ftos c.total_cost}#{ftos c.total_avg}#{ftos c.total_max}\n"
    str += "| Render:    #{ftos c.render_cost}#{ftos c.render_avg}#{ftos c.render_max}\n"
    str += "| DB:        #{ftos c.db_cost}#{ftos c.db_avg}#{ftos c.db_max}\n"
    c.actions.sort{|x,y| y.total_cost <=> x.total_cost}.each do |a|
      next if opts[:mincost] and a.total_cost < opts[:mincost]
      next if opts[:minavg] and a.total_avg < opts[:minavg]
      next if opts[:minmax] and a.total_max < opts[:minmax]
      str += "|-+ #{a.name} (#{a.log_entries.size} entries, #{(a.error_avg*100).to_i}% Error)\n"
      str += "| | Total:   #{ftos a.total_cost}#{ftos a.total_avg}#{ftos a.total_max}\n"
      str += "| | Render:  #{ftos a.render_cost}#{ftos a.render_avg}#{ftos a.render_max}\n"
      str += "| | DB:      #{ftos a.db_cost}#{ftos a.db_avg}#{ftos a.db_max}\n"
    end
    str += "\n"
  end
  return str
end

Print out all the session_ids and their statistics. See #print_actions for options.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/slow_actions.rb', line 86

def print_sessions(opts = {})
  str = ""
  str += "           Cost    Average Max\n"
  sessions.sort{|x,y| y.total_cost <=> x.total_cost}.each do |s|
    next if opts[:mincost] and s.total_cost < opts[:mincost]
    next if opts[:minavg] and s.total_avg < opts[:minavg]
    next if opts[:minmax] and s.total_max < opts[:minmax]
    str += "+ #{s.name} (#{s.log_entries.size} entries, #{(s.error_avg*100).to_i}% Error)\n"
    str += "| Total:   #{ftos s.total_cost}#{ftos s.total_avg}#{ftos s.total_max}\n"
    str += "| Render:  #{ftos s.render_cost}#{ftos s.render_avg}#{ftos s.render_max}\n"
    str += "| DB:      #{ftos s.db_cost}#{ftos s.db_avg}#{ftos s.db_max}\n"
    str += "\n"
  end
  return str
end

#sessionsObject

All the #Session objects



118
119
120
# File 'lib/slow_actions.rb', line 118

def sessions
  @sessions.values
end

#to_htmlObject

Print out the stats as html. Not implemented.



103
104
105
# File 'lib/slow_actions.rb', line 103

def to_html
  raise "Not Implemented"
end