Class: RuntimeProfiler::SqlEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/runtime_profiler/events/sql_event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args:, trace:) ⇒ SqlEvent

Returns a new instance of SqlEvent.



7
8
9
10
# File 'lib/runtime_profiler/events/sql_event.rb', line 7

def initialize(args: , trace: )
  _name, @started_at, @finished_at, _unique_id, @payload = args
  @trace = sanitize_trace!(trace)
end

Instance Attribute Details

#finished_atObject (readonly)

Returns the value of attribute finished_at.



5
6
7
# File 'lib/runtime_profiler/events/sql_event.rb', line 5

def finished_at
  @finished_at
end

#payloadObject (readonly)

Returns the value of attribute payload.



5
6
7
# File 'lib/runtime_profiler/events/sql_event.rb', line 5

def payload
  @payload
end

#started_atObject (readonly)

Returns the value of attribute started_at.



5
6
7
# File 'lib/runtime_profiler/events/sql_event.rb', line 5

def started_at
  @started_at
end

#traceObject (readonly)

Returns the value of attribute trace.



5
6
7
# File 'lib/runtime_profiler/events/sql_event.rb', line 5

def trace
  @trace
end

Instance Method Details

#keyObject



34
35
36
# File 'lib/runtime_profiler/events/sql_event.rb', line 34

def key
  @key ||= Digest::MD5.hexdigest(sql.downcase)
end

#recordable?Boolean

Returns:

  • (Boolean)


12
13
14
15
# File 'lib/runtime_profiler/events/sql_event.rb', line 12

def recordable?
  return true unless RuntimeProfiler.profiled_sql_commands.respond_to?(:join)
  profiled_sql_matcher =~ sql
end

#sanitized_sqlObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/runtime_profiler/events/sql_event.rb', line 21

def sanitized_sql
  sql.squish!

  sql.gsub!(/(\s(=|>|<|>=|<=|<>|!=)\s)('[^']+'|[\$\+\-\w\.]+)/, '\1xxx')
  sql.gsub!(/(\sIN\s)\([^\(\)]+\)/i, '\1(xxx)')
  sql.gsub!(/(\sBETWEEN\s)('[^']+'|[\+\-\w\.]+)(\sAND\s)('[^']+'|[\+\-\w\.]+)/i, '\1xxx\3xxx')
  sql.gsub!(/(\sVALUES\s)\(.+\)/i, '\1(xxx)')
  sql.gsub!(/(\s(LIKE|ILIKE|SIMILAR TO|NOT SIMILAR TO)\s)('[^']+')/i, '\1xxx')
  sql.gsub!(/(\s(LIMIT|OFFSET)\s)(\d+)/i, '\1xxx')

  sql
end

#total_runtimeObject



17
18
19
# File 'lib/runtime_profiler/events/sql_event.rb', line 17

def total_runtime
  1000.0 * (@finished_at - @started_at)
end