Class: Eyeballs::Inspector

Inherits:
Object
  • Object
show all
Defined in:
lib/eyeballs/inspector.rb

Constant Summary collapse

OPTIONS =
[:analyze, :verbose, :costs, :buffers]
FORMATS =
[:text, :xml, :json, :yaml]

Instance Method Summary collapse

Constructor Details

#initialize(relation) ⇒ Inspector

Returns a new instance of Inspector.



9
10
11
# File 'lib/eyeballs/inspector.rb', line 9

def initialize(relation)
  @relation = relation
end

Instance Method Details

#explain(format: :text, options: OPTIONS) ⇒ Object



13
14
15
16
17
# File 'lib/eyeballs/inspector.rb', line 13

def explain(format: :text, options: OPTIONS)
  @explain ||= explain_queries(format: format, options: options).map do |query|
    run_query(query)
  end
end

#explain_queries(format: :text, options: OPTIONS) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/eyeballs/inspector.rb', line 19

def explain_queries(format: :text, options: OPTIONS)
  validate_format!(format)
  validate_options!(options)
  @explain_queries ||= queries.map do |query|
    explain_query(query, format, options)
  end
end

#gocmdpevObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/eyeballs/inspector.rb', line 54

def gocmdpev
  to_hash_array.each do |h|
    begin
      tmp = Tempfile.new('pg-eyeballs')
      tmp.write(h.to_json)
      tmp.close
      system("cat #{tmp.path} | gocmdpev")
    ensure
      tmp.close
      tmp.unlink
    end
  end
  nil
end

#inspectObject



45
46
47
# File 'lib/eyeballs/inspector.rb', line 45

def inspect
  "Eyeballs::Inspector: #{@relation.to_s}"
end

#log_json(options: OPTIONS) ⇒ Object



49
50
51
52
# File 'lib/eyeballs/inspector.rb', line 49

def log_json(options: OPTIONS)
  to_hash_array.each { |h| puts "#{h.to_json }" }
  nil
end

#queriesObject



27
28
29
30
31
# File 'lib/eyeballs/inspector.rb', line 27

def queries
  @relation.connection.to_sql(query_array).map { |query|
    build_sql(query)
  }
end

#to_hash_array(options: OPTIONS) ⇒ Object



41
42
43
# File 'lib/eyeballs/inspector.rb', line 41

def to_hash_array(options: OPTIONS)
  to_json(options: options).map { |json| JSON.parse(json) }
end

#to_json(options: OPTIONS) ⇒ Object



37
38
39
# File 'lib/eyeballs/inspector.rb', line 37

def to_json(options: OPTIONS)
  explain(options: options, format: :json)
end

#to_s(options: OPTIONS) ⇒ Object



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

def to_s(options: OPTIONS)
  explain.join("\n\n")
end