Sha256: 9274581f41eec44d21fd6ea71b298bbd3f96d79ff5c1ea1581ff833d71d77016

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

require "digest"

module Rack
  module Bug

    class SQLPanel < Panel
      require "rack/bug/panels/sql_panel/sql_extension"

      autoload :PanelApp, "rack/bug/panels/sql_panel/panel_app"
      autoload :Query,    "rack/bug/panels/sql_panel/query"

      def panel_app
        PanelApp.new
      end

      def self.record(sql, backtrace = [], &block)
        return block.call unless Rack::Bug.enabled?

        start_time = Time.now
        result = block.call
        queries << Query.new(sql, Time.now - start_time, backtrace)

        return result
      end

      def self.reset
        Thread.current["rack.test.queries"] = []
      end

      def self.queries
        Thread.current["rack.test.queries"] ||= []
      end

      def self.total_time
        (queries.inject(0) { |memo, query| memo + query.time}) * 1_000
      end

      def name
        "sql"
      end

      def heading
        "#{self.class.queries.size} Queries (%.2fms)" % self.class.total_time
      end

      def content
        result = render_template "panels/sql", :queries => self.class.queries
        self.class.reset
        return result
      end

    end

  end
end

Version data entries

3 entries across 3 versions & 3 rubygems

Version Path
jeroenvandijk-rack-bug-0.2.1 lib/rack/bug/panels/sql_panel.rb
wested-rack-bug-0.2.2.1 lib/rack/bug/panels/sql_panel.rb
rack-bug-0.2.1 lib/rack/bug/panels/sql_panel.rb