Skip to content

Commit

Permalink
Tidy up queries and assigns notes.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed May 12, 2009
1 parent a9a4385 commit cdc1227
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
1 change: 1 addition & 0 deletions lib/rails-footnotes/footnotes.rb
Expand Up @@ -169,6 +169,7 @@ def insert_styles
#footnotes_debug table {text-align: center;}
#footnotes_debug table td {padding: 0 5px;}
#footnotes_debug tbody {text-align: left;}
#footnotes_debug .name_values td {vertical-align: top;}
#footnotes_debug legend {background-color: #FFF;}
#footnotes_debug fieldset {text-align: left; border: 1px dashed #aaa; padding: 0.5em 1em 1em 1em; margin: 1em 2em; color: #444; background-color: #FFF;}
/* Aditional Stylesheets */
Expand Down
35 changes: 28 additions & 7 deletions lib/rails-footnotes/notes/assigns_note.rb
Expand Up @@ -3,21 +3,42 @@
module Footnotes
module Notes
class AssignsNote < AbstractNote
@@ignored_assigns = %w( @template @_request @db_rt_before_render @db_rt_after_render @view_runtime )
cattr_accessor :ignored_assigns, :instance_writter => false

def initialize(controller)
@controller = controller
end

def legend
"Assigns for #{@controller.class.to_s}"
def title
"Assigns (#{assigns.size})"
end

def valid?
assigns
end

def content
result = [['Name', 'Value']]
variable_detail = @controller.params[:variable_detail] || []
@controller.instance_variables.each {|v| result << [v, variable_detail.index('ALL') || variable_detail.include?(v) ? escape(@controller.instance_variable_get(v).inspect) : @controller.instance_variable_get(v)]}

mount_table(result, :id => "footnotes_assigns")
rows = []
assigns.each do |key|
rows << [ key, assigned_value(key) ]
end
mount_table(rows.unshift(['Name', 'Value']), :class => 'name_values')
end

protected

def assigns
return @assigns if @assigns

@assigns = @controller.instance_variables
@assigns -= @controller.protected_instance_variables
@assigns -= ignored_assigns
end

def assigned_value(key)
escape(@controller.instance_variable_get(key).inspect)
end
end
end
end
26 changes: 15 additions & 11 deletions lib/rails-footnotes/notes/queries_note.rb
Expand Up @@ -3,11 +3,10 @@
module Footnotes
module Notes
class QueriesNote < AbstractNote
@@alert_explain = /ALL/
@@alert_db_time = 0.16
@@alert_sql_number = 16
@@alert_sql_number = 8
@@sql = []
cattr_accessor :sql, :alert_db_time, :alert_sql_number, :alert_explain
cattr_accessor :sql, :alert_db_time, :alert_sql_number, :alert_explain, :instance_writter => false

def self.start!(controller)
@@sql = []
Expand All @@ -20,11 +19,11 @@ def self.to_sym
def title
db_time = @@sql.inject(0){|sum, item| sum += item.time }
query_color = generate_red_color(@@sql.length, alert_sql_number)
db_color = generate_red_color(db_time, alert_db_time)
db_color = generate_red_color(db_time, alert_db_time)

<<-TITLE
<span style="background-color:#{query_color}">Queries (#{@@sql.length})</span>
<span style="background-color:#{db_color}">DB (#{"%.6f" % db_time}s)</span>
<span id="explain_alert" style="display:none;background-color:red">Explain Alert</span>
TITLE
end

Expand Down Expand Up @@ -58,12 +57,11 @@ def content
end

protected
def parse_explain(results, i)
def parse_explain(results)
table = []
table << results.fetch_fields.map(&:name)
results.each do |row|
row[0] += "<script type=\"text/javascript\">document.getElementById('explain_alert').style.display = '';document.getElementById('explain_#{i}').style.background = '#FF0000';</script>" if row.join('|') =~ alert_explain
table << row;
table << row
end
table
end
Expand All @@ -76,24 +74,30 @@ def parse_trace(trace)
end

def print_name_and_time(name, time)
"<span style='background-color:#{generate_red_color(time, alert_db_time/5)}'>#{escape(name || 'SQL')} (#{sprintf('%f', time)}s)</span>"
"<span style='background-color:#{generate_red_color(time, alert_ratio)}'>#{escape(name || 'SQL')} (#{sprintf('%f', time)}s)</span>"
end

def print_query(query)
escape(query.to_s.gsub(/(\s)+/, ' ').gsub('`', ''))
end

def print_explain(i, explain)
mount_table(parse_explain(explain, i), :id => "qtable_#{i}", :style => 'margin:10px;display:none;')
mount_table(parse_explain(explain), :id => "qtable_#{i}", :style => 'margin:10px;display:none;')
end

def generate_red_color(value, alert)
c = ((value.to_f/alert)*16).to_i
c = ((value.to_f/alert).to_i - 1) * 16
c = 0 if c < 0
c = 15 if c > 15

c = (15-c).to_s(16)
"#ff#{c*4}"
end

def alert_ratio
alert_db_time / alert_sql_number
end

end
end

Expand Down

0 comments on commit cdc1227

Please sign in to comment.