Class: Graphiti::Debugger

Inherits:
Object show all
Defined in:
lib/graphiti/debugger.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.chunksObject

Returns the value of attribute chunks.



7
8
9
# File 'lib/graphiti/debugger.rb', line 7

def chunks
  @chunks
end

.debug_modelsObject

Returns the value of attribute debug_models.



7
8
9
# File 'lib/graphiti/debugger.rb', line 7

def debug_models
  @debug_models
end

.enabledObject

Returns the value of attribute enabled.



7
8
9
# File 'lib/graphiti/debugger.rb', line 7

def enabled
  @enabled
end

.preserveObject

Returns the value of attribute preserve.



7
8
9
# File 'lib/graphiti/debugger.rb', line 7

def preserve
  @preserve
end

.pryObject

Returns the value of attribute pry.



7
8
9
# File 'lib/graphiti/debugger.rb', line 7

def pry
  @pry
end

Class Method Details

.debugObject



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/graphiti/debugger.rb', line 102

def debug
  if enabled
    begin
      self.chunks = []
      yield
    ensure
      flush
      self.chunks = [] unless preserve
    end
  else
    yield
  end
end

.flushObject



124
125
126
127
128
129
130
131
# File 'lib/graphiti/debugger.rb', line 124

def flush
  Graphiti.broadcast(:flush_debug, {}) do |payload|
    payload[:chunks] = chunks
    graph_statements.each do |chunk|
      flush_chunk(chunk)
    end
  end
end

.on_data(name, start, stop, id, payload) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/graphiti/debugger.rb', line 12

def on_data(name, start, stop, id, payload)
  took = ((stop - start) * 1000.0).round(2)
  params = scrub_params(payload[:params])

  if payload[:exception]
    on_data_exception(payload, params)
  elsif payload[:sideload]
    if payload[:results]
      on_sideload_data(payload, params, took)
    end
  else
    on_primary_data(payload, params, took)
  end
end

.on_data_exception(payload, params) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/graphiti/debugger.rb', line 27

def on_data_exception(payload, params)
  unless payload[:exception_object].instance_variable_get(:@__graphiti_debug)
    add_chunk do |logs, json|
      logs << ["\n=== Graphiti Debug ERROR", :red, true]
      if (sideload = payload[:sideload])
        logs << ["#{sideload.parent_resource.class}: Sideload \"#{sideload.name}\"", :red, true]
        json[:parent_resource] = sideload.parent_resource.class.name
        json[:sideload] = sideload.name
      end
      if params
        query = "#{payload[:resource].class.name}.all(#{JSON.pretty_generate(params)}).data"
        logs << [query, :cyan, true]
        logs << ["The error occurred when running the above query. Copy/paste it into a rake task or Rails console session to reproduce. Keep in mind you may have to set context.", :yellow, true]
      else
        query = "This sideload is done manually via .scope - no debug information available."
        logs << [query, :cyan, true]
      end
      json[:query] = query

      logs << "\n\n"
      payload[:exception_object]&.instance_variable_set(:@__graphiti_debug, json)
    end
  end
end

.on_primary_data(payload, params, took) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/graphiti/debugger.rb', line 75

def on_primary_data(payload, params, took)
  results = results(payload[:results])
  add_chunk(payload[:resource], payload[:parent]) do |logs, json|
    logs << [""]
    logs << ["=== Graphiti Debug", :green, true]
    title = "Top Level Data Retrieval (+ sideloads):"
    logs << [title, :green, true]
    json[:title] = title
    query = "#{payload[:resource].class.name}.all(#{params.inspect})"
    logs << [query, :cyan, true]
    json[:query] = query
    logs << ["Returned Models: #{results}"] if debug_models
    logs << ["Took: #{took}ms", :magenta, true]
    json[:took] = took
  end
end

.on_render(name, start, stop, id, payload) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/graphiti/debugger.rb', line 92

def on_render(name, start, stop, id, payload)
  add_chunk do |logs|
    took = ((stop - start) * 1000.0).round(2)
    logs << [""]
    logs << ["=== Graphiti Debug", :green, true]
    logs << ["Rendering:", :green, true]
    logs << ["Took: #{took}ms", :magenta, true]
  end
end

.on_sideload_data(payload, params, took) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/graphiti/debugger.rb', line 56

def on_sideload_data(payload, params, took)
  sideload = payload[:sideload]
  results = results(payload[:results])
  add_chunk(payload[:resource], payload[:parent]) do |logs, json|
    logs << [" \\_ #{sideload.name}", :yellow, true]
    json[:name] = sideload.name
    if sideload.class.scope_proc
      query = "#{payload[:resource].class.name}: Manual sideload via .scope"
    else
      query = "#{payload[:resource].class.name}.all(#{params.inspect})"
    end
    logs << ["    #{query}", :cyan, true]
    json[:query] = query
    logs << ["    Returned Models: #{results}"] if debug_models
    logs << ["    Took: #{took}ms", :magenta, true]
    json[:took] = took
  end
end

.results(raw_results) ⇒ Object



52
53
54
# File 'lib/graphiti/debugger.rb', line 52

def results(raw_results)
  raw_results.map { |r| "[#{r.class.name}, #{r.id.inspect}]" }.join(", ")
end

.to_aObject



116
117
118
119
120
121
122
# File 'lib/graphiti/debugger.rb', line 116

def to_a
  debugs = []
  graph_statements.each do |chunk|
    debugs << chunk_to_hash(chunk)
  end
  debugs
end