Class: AwesomeExplain::Renderers::ActiveRecord
- Inherits:
 - 
      Object
      
        
- Object
 - AwesomeExplain::Renderers::ActiveRecord
 
 
- Defined in:
 - lib/awesome_explain/renderers/active_record.rb
 
Instance Attribute Summary collapse
- 
  
    
      #query  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    
Returns the value of attribute query.
 - 
  
    
      #result  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    
Returns the value of attribute result.
 - 
  
    
      #sql_explain  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    
Returns the value of attribute sql_explain.
 
Instance Method Summary collapse
- #explain_query ⇒ Object
 - #general_stats_section(t) ⇒ Object
 - #index_stats ⇒ Object
 - #index_stats_section(t) ⇒ Object
 - 
  
    
      #initialize(query, result = nil)  ⇒ ActiveRecord 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    
A new instance of ActiveRecord.
 - #node_type_stats ⇒ Object
 - #node_types_section(t) ⇒ Object
 - #plan_stats ⇒ Object
 - #print ⇒ Object
 - #seq_scans_row ⇒ Object
 - #table_stats ⇒ Object
 - #table_stats_section(t) ⇒ Object
 
Constructor Details
#initialize(query, result = nil) ⇒ ActiveRecord
Returns a new instance of ActiveRecord.
      6 7 8 9  | 
    
      # File 'lib/awesome_explain/renderers/active_record.rb', line 6 def initialize(query, result = nil) @query = query @result = result || explain_query end  | 
  
Instance Attribute Details
#query ⇒ Object (readonly)
Returns the value of attribute query.
      4 5 6  | 
    
      # File 'lib/awesome_explain/renderers/active_record.rb', line 4 def query @query end  | 
  
#result ⇒ Object (readonly)
Returns the value of attribute result.
      4 5 6  | 
    
      # File 'lib/awesome_explain/renderers/active_record.rb', line 4 def result @result end  | 
  
#sql_explain ⇒ Object (readonly)
Returns the value of attribute sql_explain.
      4 5 6  | 
    
      # File 'lib/awesome_explain/renderers/active_record.rb', line 4 def sql_explain @sql_explain end  | 
  
Instance Method Details
#explain_query ⇒ Object
      11 12 13 14 15 16 17 18  | 
    
      # File 'lib/awesome_explain/renderers/active_record.rb', line 11 def explain_query explain = AwesomeExplain::Config.instance.connection.raw_connection.exec( "EXPLAIN (ANALYZE true, COSTS true, FORMAT json) #{query.to_sql}" ) explain = explain.map { |h| h.values.first }.join("\n") @sql_explain = SqlExplain.new(explain_output: explain) end  | 
  
#general_stats_section(t) ⇒ Object
      62 63 64 65 66 67 68 69 70 71 72 73  | 
    
      # File 'lib/awesome_explain/renderers/active_record.rb', line 62 def general_stats_section(t) title = AwesomeExplain::Utils::Color.fg_color :yellow, 'General Stats' t << [{ value: title, alignment: :center, colspan: 2}] t << :separator t << ['Table', 'Count'] t << :separator t << ['Total Rows Planned', plan_stats.total_rows_planned] t << ['Total Rows', plan_stats.total_rows] t << ['Total Loops', plan_stats.total_loops] t << seq_scans_row t << ['Indexes Used', plan_stats.index_stats.size] end  | 
  
#index_stats ⇒ Object
      42 43 44  | 
    
      # File 'lib/awesome_explain/renderers/active_record.rb', line 42 def index_stats @index_stats ||= plan_stats.index_stats end  | 
  
#index_stats_section(t) ⇒ Object
      99 100 101 102 103 104 105 106 107 108 109 110 111  | 
    
      # File 'lib/awesome_explain/renderers/active_record.rb', line 99 def index_stats_section(t) if index_stats.size.positive? title = AwesomeExplain::Utils::Color.fg_color :yellow, 'Index Stats' t << :separator t << [{ value: title, alignment: :center, colspan: 2}] t << :separator t << ['Index Name', 'Count'] t << :separator index_stats.each do |index, stats| t << [index, stats.dig(:count)] end end end  | 
  
#node_type_stats ⇒ Object
      38 39 40  | 
    
      # File 'lib/awesome_explain/renderers/active_record.rb', line 38 def node_type_stats @node_type_stats ||= plan_stats.node_type_stats end  | 
  
#node_types_section(t) ⇒ Object
      87 88 89 90 91 92 93 94 95 96 97  | 
    
      # File 'lib/awesome_explain/renderers/active_record.rb', line 87 def node_types_section(t) title = AwesomeExplain::Utils::Color.fg_color :yellow, 'Node Type Stats' t << :separator t << [{ value: title, alignment: :center, colspan: 2}] t << :separator t << ['Node Type', 'Count'] t << :separator node_type_stats.each do |node_type, stats| t << [node_type, stats.dig(:count)] end end  | 
  
#plan_stats ⇒ Object
      30 31 32  | 
    
      # File 'lib/awesome_explain/renderers/active_record.rb', line 30 def plan_stats @plan_stats ||= @sql_explain.tree.plan_stats end  | 
  
#print ⇒ Object
      20 21 22 23 24 25 26 27 28  | 
    
      # File 'lib/awesome_explain/renderers/active_record.rb', line 20 def print table = Terminal::Table.new do |t| general_stats_section t table_stats_section t node_types_section t index_stats_section t end puts table end  | 
  
#seq_scans_row ⇒ Object
      46 47 48 49 50 51 52 53 54 55 56 57 58 59 60  | 
    
      # File 'lib/awesome_explain/renderers/active_record.rb', line 46 def seq_scans_row color = plan_stats.seq_scans.positive? ? :cyan : :green seq_scans_label = AwesomeExplain::Utils::Color.fg_color( color, 'Seq Scans' ) seq_scans_val = AwesomeExplain::Utils::Color.fg_color( color, plan_stats.seq_scans.to_s ) [seq_scans_label, seq_scans_val] end  | 
  
#table_stats ⇒ Object
      34 35 36  | 
    
      # File 'lib/awesome_explain/renderers/active_record.rb', line 34 def table_stats @table_stats ||= plan_stats.table_stats end  | 
  
#table_stats_section(t) ⇒ Object
      75 76 77 78 79 80 81 82 83 84 85  | 
    
      # File 'lib/awesome_explain/renderers/active_record.rb', line 75 def table_stats_section(t) title = AwesomeExplain::Utils::Color.fg_color :yellow, 'Table Stats' t << :separator t << [{ value: title, alignment: :center, colspan: 2}] t << :separator t << ['Table', 'Count'] t << :separator table_stats.each do |table_name, stats| t << [table_name, stats.dig(:count)] end end  |