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



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/graphiti/debugger.rb', line 129

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

.flushObject



151
152
153
154
155
156
157
158
# File 'lib/graphiti/debugger.rb', line 151

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
26
27
# File 'lib/graphiti/debugger.rb', line 12

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/graphiti/debugger.rb', line 94

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_aObject



143
144
145
146
147
148
149
# File 'lib/graphiti/debugger.rb', line 143

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