Skip to content

Commit be0a949

Browse files
committed
DRYed peek-pg/mysql2 views and update peek-rblineprof monkey-patch file
Signed-off-by: Rémy Coutable <[email protected]>
1 parent ef4ccb7 commit be0a949

File tree

4 files changed

+33
-47
lines changed

4 files changed

+33
-47
lines changed
+3-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
1-
%strong
2-
%a#peek-show-queries{ href: '#' }
3-
%span{ data: { defer_to: "#{view.defer_key}-duration" } }...
4-
\/
5-
%span{ data: { defer_to: "#{view.defer_key}-calls" } }...
6-
#modal-peek-pg-queries.modal{ tabindex: -1 }
7-
.modal-dialog
8-
#modal-peek-pg-queries-content.modal-content
9-
.modal-header
10-
%a.close{ href: "#", "data-dismiss" => "modal" } ×
11-
%h4
12-
SQL queries
13-
.modal-body{ data: { defer_to: "#{view.defer_key}-queries" } }...
1+
- local_assigns.fetch(:view)
2+
3+
= render 'peek/views/sql', view: view
144
mysql

app/views/peek/views/_pg.html.haml

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
1-
%strong
2-
%a#peek-show-queries{ href: '#' }
3-
%span{ data: { defer_to: "#{view.defer_key}-duration" } }...
4-
\/
5-
%span{ data: { defer_to: "#{view.defer_key}-calls" } }...
6-
#modal-peek-pg-queries.modal{ tabindex: -1 }
7-
.modal-dialog
8-
#modal-peek-pg-queries-content.modal-content
9-
.modal-header
10-
%a.close{ href: "#", "data-dismiss" => "modal" } ×
11-
%h4
12-
SQL queries
13-
.modal-body{ data: { defer_to: "#{view.defer_key}-queries" } }...
1+
- local_assigns.fetch(:view)
2+
3+
= render 'peek/views/sql', view: view
144
pg

app/views/peek/views/_sql.html.haml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
%strong
2+
%a#peek-show-queries{ href: '#' }
3+
%span{ data: { defer_to: "#{view.defer_key}-duration" } }...
4+
\/
5+
%span{ data: { defer_to: "#{view.defer_key}-calls" } }...
6+
#modal-peek-pg-queries.modal{ tabindex: -1 }
7+
.modal-dialog
8+
#modal-peek-pg-queries-content.modal-content
9+
.modal-header
10+
%a.close{ href: "#", "data-dismiss" => "modal" } ×
11+
%h4
12+
SQL queries
13+
.modal-body{ data: { defer_to: "#{view.defer_key}-queries" } }...

lib/peek/rblineprof/custom_controller_helpers.rb

+14-21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module Rblineprof
33
module CustomControllerHelpers
44
extend ActiveSupport::Concern
55

6+
# This will become useless once https://github.com/peek/peek-rblineprof/pull/5
7+
# is merged
68
def pygmentize(file_name, code, lexer = nil)
79
if lexer.present?
810
Gitlab::Highlight.highlight(file_name, code)
@@ -11,7 +13,7 @@ def pygmentize(file_name, code, lexer = nil)
1113
end
1214
end
1315

14-
# rubocop:disable Metrics/AbcSize
16+
# rubocop:disable all
1517
def inject_rblineprof
1618
ret = nil
1719
profile = lineprof(rblineprof_profiler_regex) do
@@ -26,43 +28,32 @@ def inject_rblineprof
2628

2729
# Sort each file by the longest calculated time
2830
per_file = profile.map do |file, lines|
29-
total, _child, excl, total_cpu, _child_cpu, excl_cpu = lines[0]
31+
total, child, excl, total_cpu, child_cpu, excl_cpu = lines[0]
3032

3133
wall = summary == 'exclusive' ? excl : total
3234
cpu = summary == 'exclusive' ? excl_cpu : total_cpu
3335
idle = summary == 'exclusive' ? (excl - excl_cpu) : (total - total_cpu)
3436

35-
sort_method =
36-
case sort
37-
when 'idle'
38-
idle
39-
when 'cpu'
40-
cpu
41-
else
42-
wall
43-
end
44-
4537
[
4638
file, lines,
4739
wall, cpu, idle,
48-
sort_method
40+
sort == 'idle' ? idle : sort == 'cpu' ? cpu : wall
4941
]
50-
end
51-
per_file = per_file.sort_by { |_a, _b, _c, _d, _e, f| -f }
42+
end.sort_by{ |a,b,c,d,e,f| -f }
5243

5344
output = ''
5445
per_file.each do |file_name, lines, file_wall, file_cpu, file_idle, file_sort|
46+
5547
output << "<div class='peek-rblineprof-file'><div class='heading'>"
5648

5749
show_src = file_sort > min
5850
tmpl = show_src ? "<a href='#' class='js-lineprof-file'>%s</a>" : "%s"
5951

60-
output <<
61-
if mode == 'cpu'
62-
sprintf("<span class='duration'>% 8.1fms + % 8.1fms</span> #{tmpl}", file_cpu / 1000.0, file_idle / 1000.0, file_name.sub(Rails.root.to_s + '/', ''))
63-
else
64-
sprintf("<span class='duration'>% 8.1fms</span> #{tmpl}", file_wall / 1000.0, file_name.sub(Rails.root.to_s + '/', ''))
65-
end
52+
if mode == 'cpu'
53+
output << sprintf("<span class='duration'>% 8.1fms + % 8.1fms</span> #{tmpl}", file_cpu / 1000.0, file_idle / 1000.0, file_name.sub(Rails.root.to_s + '/', ''))
54+
else
55+
output << sprintf("<span class='duration'>% 8.1fms</span> #{tmpl}", file_wall/1000.0, file_name.sub(Rails.root.to_s + '/', ''))
56+
end
6657

6758
output << "</div>" # .heading
6859

@@ -89,6 +80,8 @@ def inject_rblineprof
8980
output << "<pre class='duration'>#{times.join("\n")}</pre>"
9081
# The following line was changed from
9182
# https://github.com/peek/peek-rblineprof/blob/8d3b7a283a27de2f40abda45974516693d882258/lib/peek/rblineprof/controller_helpers.rb#L125
83+
# This will become useless once https://github.com/peek/peek-rblineprof/pull/16
84+
# is merged and is implemented.
9285
output << "<pre class='code highlight white'>#{pygmentize(file_name, code.join, 'ruby')}</pre>"
9386
output << "</div></div>" # .data then .peek-rblineprof-file
9487
end

0 commit comments

Comments
 (0)