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

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

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.



8
9
10
11
12
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
# File 'lib/mini_profiler/timer_struct/sql.rb', line 8

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 = String.new
     # 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 and !full_backtrace
      if    full_backtrace or
            (
              (
                Rack::MiniProfiler.config.backtrace_includes.nil? or
                Rack::MiniProfiler.config.backtrace_includes.any?{|regex| ln =~ regex}
              ) and
              (
                Rack::MiniProfiler.config.backtrace_ignores.nil? or
                Rack::MiniProfiler.config.backtrace_ignores.none?{|regex| ln =~ regex}
              )
            )
        stack_trace << ln << "\n"
      end
    end
  end

  @parent      = parent
  @page        = page
  start_millis = ((Time.now.to_f * 1000).to_i - page[:started]) - duration_ms
  super(
    :execute_type                      => 3, # TODO
    :formatted_command_string          => query,
    :stack_trace_snippet               => stack_trace,
    :start_milliseconds                => start_millis,
    :duration_milliseconds             => duration_ms,
    :first_fetch_duration_milliseconds => duration_ms,
    :parameters                        => trim_binds(params),
    :parent_timing_id                  => nil,
    :is_duplicate                      => false
  )
end

Instance Method Details

#report_reader_duration(elapsed_ms) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/mini_profiler/timer_struct/sql.rb', line 49

def report_reader_duration(elapsed_ms)
  return if @reported
  @reported = true
  self[:duration_milliseconds]                += elapsed_ms
  @parent[:sql_timings_duration_milliseconds] += elapsed_ms
  @page[:duration_milliseconds_in_sql]        += elapsed_ms
end

#trim_binds(binds) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mini_profiler/timer_struct/sql.rb', line 57

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