Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show List of Queries Called #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/assets/javascripts/peek-mysql2.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
$(document).on "click", "#peek-mysql2-queries", (e) ->
e.preventDefault()
$this = $(this)
if $this.text() == 'Show List'
$this.text('Hide List')
else
$this.text('Show List')

$queryList = $("div[data-defer-to=mysql2-queries]")
contentText = $queryList.text()

if contentText.indexOf('<br>') >= 0
$queryList.html($queryList.text())

$queryList.toggle()
return
15 changes: 15 additions & 0 deletions app/assets/stylesheets/peek-mysql2.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#peek-view-mysql2 {
position: relative;
}

div[data-defer-to="mysql2-queries"] {
background-color: rgb(255, 255, 255);
color: rgb(0, 0, 0);
padding: 5px;
position: absolute;
top: 40px;
width: 700px;
z-index: 99999;
border: 2px solid rgba(0, 0, 0, 0.2);
border-radius: 5px;
}
4 changes: 3 additions & 1 deletion app/views/peek/views/_mysql2.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
<strong><span data-defer-to="<%= view.defer_key %>-duration">...</span> / <span data-defer-to="<%= view.defer_key %>-calls">...</span></strong> sql
<strong><span data-defer-to="<%= view.defer_key %>-duration">...</span>
/ <span data-defer-to="<%= view.defer_key %>-calls">...</span></strong> sql
(<a href="#" id="peek-mysql2-queries">Show List</a>) <div data-defer-to="<%= view.defer_key %>-queries" style="display:none;"></div>
14 changes: 12 additions & 2 deletions lib/peek/views/mysql2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
# Instrument SQL time
class Mysql2::Client
class << self
attr_accessor :query_time, :query_count
attr_accessor :query_time, :query_count, :query_list
end
self.query_count = Atomic.new(0)
self.query_time = Atomic.new(0)
self.query_list = Atomic.new('')

def query_with_timing(*args)
start = Time.now
Expand All @@ -16,6 +17,10 @@ def query_with_timing(*args)
duration = (Time.now - start)
Mysql2::Client.query_time.update { |value| value + duration }
Mysql2::Client.query_count.update { |value| value + 1 }
Mysql2::Client.query_list.update { |value|
ms = duration * 1000
value + args.first + '(' + ms.to_s + ' ms)<br>'
}
end
alias_method_chain :query, :timing
end
Expand All @@ -40,8 +45,12 @@ def calls
::Mysql2::Client.query_count.value
end

def queries
::Mysql2::Client.query_list.value
end

def results
{ :duration => formatted_duration, :calls => calls }
{ :duration => formatted_duration, :calls => calls, :queries => queries }
end

private
Expand All @@ -51,6 +60,7 @@ def setup_subscribers
before_request do
::Mysql2::Client.query_time.value = 0
::Mysql2::Client.query_count.value = 0
::Mysql2::Client.query_list.value = ''
end
end
end
Expand Down