Add total sql count (#358)

Add total sql count (#358)
  • Add total sql count

  • Changed totalSqlCount to option

From 874ec80a5b50940a2251a4ca9384bb946f767609 Mon Sep 17 00:00:00 2001
From: zaru <zarutofu@gmail.com>
Date: Mon, 23 Jul 2018 15:35:54 +0900
Subject: [PATCH] Add total sql count (#358)

* Add total sql count

* Changed totalSqlCount to option

diff --git a/README.md b/README.md
index b98e37a..03d7662 100644
--- a/README.md
+++ b/README.md
@@ -321,6 +321,7 @@ base_url_path|`'/mini-profiler-resources/'`|Path for assets; added as a prefix w
 collapse_results|`true`|If multiple timing results exist in a single page, collapse them till clicked.
 max_traces_to_show|20|Maximum number of mini profiler timing blocks to show on one page
 html_container|`body`|The HTML container (as a jQuery selector) to inject the mini_profiler UI into
+show_total_sql_count|`false`|Displays the total number of SQL executions.
 
 ### Custom middleware ordering (required if using `Rack::Deflate` with Rails)
 
diff --git a/lib/html/includes.js b/lib/html/includes.js
index 1628ef3..c01688e 100644
--- a/lib/html/includes.js
+++ b/lib/html/includes.js
@@ -11,7 +11,8 @@ var MiniProfiler = (function () {
         totalsControl,
         reqs = 0,
         expandedResults = false,
-        totalTime = 0;
+        totalTime = 0,
+        totalSqlCount = 0
         ;
 
     var hasLocalStorage = function (keyPrefix) {
@@ -146,6 +147,7 @@ var MiniProfiler = (function () {
         var result = renderTemplate(json);
 
         totalTime += parseFloat(json.duration_milliseconds, 10);
+        totalSqlCount += parseInt(json.sql_count);
         reqs++;
 
         if (!controls && reqs > 1 && options.collapseResults && !expandedResults) {
@@ -164,8 +166,10 @@ var MiniProfiler = (function () {
           }
 
           var reqsHtml = reqs > 1 ? ("<span class='profiler-reqs'>" + reqs + "</span>") : "";
+          var sqlHtml = options.showTotalSqlCount && totalSqlCount > 0 ? (" / <span class='profiler-number'>" + totalSqlCount + "</span> <span class='profiler-unit'>sql</span>") : "";
           totalsControl.find('.profiler-button').html("<span class='profiler-number'>" +
                     totalTime.toFixed(1) + "</span> <span class='profiler-unit'>ms</span>" +
+                    sqlHtml +
                     reqsHtml);
 
           result.hide();
@@ -647,6 +651,7 @@ var MiniProfiler = (function () {
                 var trivial = script.getAttribute('data-trivial') === 'true';
                 var children = script.getAttribute('data-children') === 'true';
                 var controls = script.getAttribute('data-controls') === 'true';
+                var totalSqlCount = script.getAttribute('data-total-sql-count') === 'true';
                 var authorized = script.getAttribute('data-authorized') === 'true';
                 var startHidden = script.getAttribute('data-start-hidden') === 'true' || sessionStorage['rack-mini-profiler-start-hidden'] === 'true';
                 var htmlContainer = script.getAttribute('data-html-container');
@@ -661,6 +666,7 @@ var MiniProfiler = (function () {
                     showChildrenTime: children,
                     maxTracesToShow: maxTraces,
                     showControls: controls,
+                    showTotalSqlCount: totalSqlCount,
                     currentId: currentId,
                     authorized: authorized,
                     toggleShortcut: toggleShortcut,
@@ -1034,6 +1040,10 @@ var MiniProfiler = (function () {
 
         formatDuration: function (duration) {
             return (duration || 0).toFixed(1);
+        },
+
+        showTotalSqlCount: function () {
+            return options.showTotalSqlCount
         }
     };
 })();
diff --git a/lib/html/includes.tmpl b/lib/html/includes.tmpl
index 0508610..80e0a99 100644
--- a/lib/html/includes.tmpl
+++ b/lib/html/includes.tmpl
@@ -7,6 +7,11 @@
       <span class="profiler-number">
         ${MiniProfiler.formatDuration(duration_milliseconds)} <span class="profiler-unit">ms</span>
       </span>
+      {{if MiniProfiler.showTotalSqlCount()}}
+      <span class="profiler-number">
+        ${sql_count} <span class="profiler-unit">sql</span>
+      </span>
+      {{/if}}
     </div>
 
     <div class="profiler-popup">
diff --git a/lib/html/profile_handler.js b/lib/html/profile_handler.js
index 41e8f90..5948085 100644
--- a/lib/html/profile_handler.js
+++ b/lib/html/profile_handler.js
@@ -1 +1 @@
-<script async type="text/javascript" id="mini-profiler" src="{path}includes.js?v={version}" data-version="{version}" data-path="{path}" data-current-id="{currentId}" data-ids="{ids}" data-horizontal-position="{horizontalPosition}" data-vertical-position="{verticalPosition}" data-trivial="{showTrivial}" data-children="{showChildren}" data-max-traces="{maxTracesToShow}" data-controls="{showControls}" data-authorized="{authorized}" data-toggle-shortcut="{toggleShortcut}" data-start-hidden="{startHidden}" data-collapse-results="{collapseResults}" data-html-container="{htmlContainer}"></script>
+<script async type="text/javascript" id="mini-profiler" src="{path}includes.js?v={version}" data-version="{version}" data-path="{path}" data-current-id="{currentId}" data-ids="{ids}" data-horizontal-position="{horizontalPosition}" data-vertical-position="{verticalPosition}" data-trivial="{showTrivial}" data-children="{showChildren}" data-max-traces="{maxTracesToShow}" data-controls="{showControls}" data-total-sql-count="{showTotalSqlCount}" data-authorized="{authorized}" data-toggle-shortcut="{toggleShortcut}" data-start-hidden="{startHidden}" data-collapse-results="{collapseResults}" data-html-container="{htmlContainer}"></script>
diff --git a/lib/mini_profiler/config.rb b/lib/mini_profiler/config.rb
index f5be7b6..6caa5db 100644
--- a/lib/mini_profiler/config.rb
+++ b/lib/mini_profiler/config.rb
@@ -39,16 +39,17 @@ module Rack
           @skip_sql_param_names = /password/ # skips parameters with the name password by default
 
           # ui parameters
-          @autorized           = true
-          @collapse_results    = true
-          @max_traces_to_show  = 20
-          @show_children       = false
-          @show_controls       = false
-          @show_trivial        = false
-          @start_hidden        = false
-          @toggle_shortcut     = 'Alt+P'
-          @html_container      = 'body'
-          @position            = "top-left"
+          @autorized            = true
+          @collapse_results     = true
+          @max_traces_to_show   = 20
+          @show_children        = false
+          @show_controls        = false
+          @show_trivial         = false
+          @show_total_sql_count = false
+          @start_hidden         = false
+          @toggle_shortcut      = 'Alt+P'
+          @html_container       = 'body'
+          @position             = "top-left"
 
           self
         }
@@ -64,8 +65,8 @@ module Rack
 
       # ui accessors
       attr_accessor :collapse_results, :max_traces_to_show, :position,
-        :show_children, :show_controls, :show_trivial, :start_hidden,
-        :toggle_shortcut, :html_container
+        :show_children, :show_controls, :show_trivial, :show_total_sql_count,
+        :start_hidden, :toggle_shortcut, :html_container
 
       # Deprecated options
       attr_accessor :use_existing_jquery
diff --git a/lib/mini_profiler/profiler.rb b/lib/mini_profiler/profiler.rb
index 075f9d0..d44c7bc 100644
--- a/lib/mini_profiler/profiler.rb
+++ b/lib/mini_profiler/profiler.rb
@@ -599,6 +599,7 @@ Append the following to your query string:
        :showChildren       => @config.show_children,
        :maxTracesToShow    => @config.max_traces_to_show,
        :showControls       => @config.show_controls,
+       :showTotalSqlCount  => @config.show_total_sql_count,
        :authorized         => true,
        :toggleShortcut     => @config.toggle_shortcut,
        :startHidden        => @config.start_hidden,
diff --git a/lib/mini_profiler/timer_struct/page.rb b/lib/mini_profiler/timer_struct/page.rb
index a14426b..567d690 100644
--- a/

GitHub