Class: Graphiti::Debugger

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.debug_models ⇒ Object

Returns the value of attribute debug_models.



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

def debug_models
  @debug_models
end

.enabled ⇒ Object

Returns the value of attribute enabled.



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

def enabled
  @enabled
end

.preserve ⇒ Object

Returns the value of attribute preserve.



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

def preserve
  @preserve
end

.pry ⇒ Object

Returns the value of attribute pry.



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

def pry
  @pry
end

Class Method Details

.chunks ⇒ Object

Pool threads inherit this through the fiber storage copy Scope#future_with_context makes, so their chunks reach the right request.



15
16
17
# File 'lib/graphiti/debugger.rb', line 15

def chunks
  Fiber[CHUNKS] ||= Concurrent::Array.new
end

.chunks=(value) ⇒ Object



19
20
21
# File 'lib/graphiti/debugger.rb', line 19

def chunks=(value)
  Fiber[CHUNKS] = value
end

.debug ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/graphiti/debugger.rb', line 141

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

.flush ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/graphiti/debugger.rb', line 163

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/graphiti/debugger.rb', line 23

def on_data(name, start, stop, id, payload)
  return [] unless enabled

  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_render(name, start, stop, id, payload) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/graphiti/debugger.rb', line 106

def on_render(name, start, stop, id, payload)
  return [] unless enabled

  add_chunk do |logs|
    took = ((stop - start) * 1000.0).round(2)
    logs << [""]
    logs << ["=== Graphiti Debug", :green, true]
    if payload[:proxy]&.cached? && Graphiti.config.cache_rendering?
      logs << ["Rendering (cached):", :green, true]

      Graphiti::Util::CacheDebug.new(payload[:proxy]).analyze do |cache_debug|
        logs << ["Cache key for #{cache_debug.name}", :blue, true]
        logs << if cache_debug.volatile?
          [" \\_ volatile | Request count: #{cache_debug.request_count} | Hit count: #{cache_debug.hit_count}", :red, true]
        else
          [" \\_   stable | Request count: #{cache_debug.request_count} | Hit count: #{cache_debug.hit_count}", :blue, true]
        end

        if cache_debug.changed_key?
          logs << [" [x] cache key changed #{cache_debug.last_version[:etag]} -> #{cache_debug.current_version[:etag]}", :red]
          logs << ["      removed: #{cache_debug.removed_segments}", :red]
          logs << ["        added: #{cache_debug.added_segments}", :red]
        elsif cache_debug.new_key?
          logs << [" [+] cache key added #{cache_debug.current_version[:etag]}", :red, true]
        else
          logs << [" [✓] #{cache_debug.current_version[:etag]}", :green, true]
        end
      end
    else
      logs << ["Rendering:", :green, true]
    end
    logs << ["Took: #{took}ms", :magenta, true]
  end
end

.to_a ⇒ Object



155
156
157
158
159
160
161
# File 'lib/graphiti/debugger.rb', line 155

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