Class: Rack::MiniProfiler::TimerStruct::Sql

Inherits:
Base
  • Object
show all
Defined in:
lib/mini_profiler/timer_struct/sql.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#[], #[]=, #as_json, #attributes, #to_json

Constructor Details

#initialize(query, duration_ms, page, parent, params = nil, skip_backtrace = false, full_backtrace = false) ⇒ Sql

Returns a new instance of Sql.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mini_profiler/timer_struct/sql.rb', line 13

def initialize(query, duration_ms, page, parent, params = nil, skip_backtrace = false, full_backtrace = false)

  stack_trace = nil
  unless skip_backtrace || duration_ms < Rack::MiniProfiler.config.backtrace_threshold_ms
    # Allow us to filter the stack trace
    stack_trace = "".dup
    # Clean up the stack trace if there are options to do so
    Kernel.caller.each do |ln|
      ln.gsub!(Rack::MiniProfiler.config.backtrace_remove, '') if Rack::MiniProfiler.config.backtrace_remove && !full_backtrace
      if    full_backtrace ||
            (
              (
                Rack::MiniProfiler.config.backtrace_includes.nil? ||
                Rack::MiniProfiler.config.backtrace_includes.any? { |regex| ln =~ regex }
              ) &&
              (
                Rack::MiniProfiler.config.backtrace_ignores.nil? ||
                Rack::MiniProfiler.config.backtrace_ignores.none? { |regex| ln =~ regex }
              )
            )
        stack_trace << ln << "\n"
      end
    end
  end

  @parent      = parent
  @page        = page
  start_millis = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) * 1000).to_i - page[:started]) - duration_ms
  super(
    execute_type: 3, # TODO
    formatted_command_string: query ? ERB::Util.html_escape(query) : nil,
    stack_trace_snippet: stack_trace,
    start_milliseconds: start_millis,
    duration_milliseconds: duration_ms,
    first_fetch_duration_milliseconds: duration_ms,
    parameters: query ? trim_binds(params) : nil,
    parent_timing_id: nil,
    is_duplicate: false
  )
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



11
12
13
# File 'lib/mini_profiler/timer_struct/sql.rb', line 11

def parent
  @parent
end

Instance Method Details

#report_reader_duration(elapsed_ms, row_count = nil, class_name = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/mini_profiler/timer_struct/sql.rb', line 54

def report_reader_duration(elapsed_ms, row_count = nil, class_name = nil)
  return if @reported
  @reported = true
  self[:duration_milliseconds]                += elapsed_ms
  @parent[:sql_timings_duration_milliseconds] += elapsed_ms
  @page[:duration_milliseconds_in_sql]        += elapsed_ms
  self[:row_count] = self[:row_count].to_i + row_count if row_count
  self[:class_name] = class_name if class_name
end

#trim_binds(binds) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mini_profiler/timer_struct/sql.rb', line 64

def trim_binds(binds)
  max_len = Rack::MiniProfiler.config.max_sql_param_length
  return if binds.nil? || max_len == 0
  return binds.map { |(name, val)| [name, val] } if max_len.nil?
  binds.map do |(name, val)|
    val ||= name
    if val.nil? || val == true || val == false || val.kind_of?(Numeric)
      # keep these parameters as is
    elsif val.kind_of?(String)
      val = val[0...max_len] + (max_len < val.length ? '...' : '') if max_len
    else
      val = val.class.name
    end
    if name.kind_of?(String)
      name = name[0...max_len] + (max_len < name.length ? '...' : '') if max_len
    end
    [name, val]
  end
end