BigW Consortium Gitlab

peek.js 2.31 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * This is a modified version of https://github.com/peek/peek/blob/master/app/assets/javascripts/peek.js
 *
 * - Removed the dependency on jquery.tipsy
 * - Removed the initializeTipsy and toggleBar functions
 * - Customized updatePerformanceBar to handle SQL queries report specificities
 * - Changed /peek/results to /-/peek/results
 * - Removed the keypress, pjax:end, page:change, and turbolinks:load handlers
 */
10
(function($) {
11
  var fetchRequestResults, getRequestId, peekEnabled, updatePerformanceBar;
12
  getRequestId = function() {
13
    return $('#peek').data('request-id');
14 15 16 17 18
  };
  peekEnabled = function() {
    return $('#peek').length;
  };
  updatePerformanceBar = function(results) {
19
    var key, label, data, table, html, tr, duration_td, sql_td, strong;
20

21 22
    Object.keys(results.data).forEach(function(key) {
      Object.keys(results.data[key]).forEach(function(label) {
23
        data = results.data[key][label];
24

25
        if (label == 'queries') {
26 27 28 29
          table = document.createElement('table');

          for (var i = 0; i < data.length; i += 1) {
            tr = document.createElement('tr');
30 31 32 33 34 35 36 37 38 39
            duration_td = document.createElement('td');
            sql_td = document.createElement('td');
            strong = document.createElement('strong');

            strong.append(data[i]['duration'] + 'ms');
            duration_td.appendChild(strong);
            tr.appendChild(duration_td);

            sql_td.appendChild(document.createTextNode(data[i]['sql']));
            tr.appendChild(sql_td);
40 41 42 43

            table.appendChild(tr);
          }

44 45
          table.className = 'table';
          $("[data-defer-to=" + key + "-" + label + "]").html(table);
46
        } else {
47 48
          $("[data-defer-to=" + key + "-" + label + "]").text(results.data[key][label]);
        }
49 50
      });
    });
51 52 53
    return $(document).trigger('peek:render', [getRequestId(), results]);
  };
  fetchRequestResults = function() {
54
    return $.ajax('/-/peek/results', {
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
      data: {
        request_id: getRequestId()
      },
      success: function(data, textStatus, xhr) {
        return updatePerformanceBar(data);
      },
      error: function(xhr, textStatus, error) {}
    });
  };
  $(document).on('peek:update', fetchRequestResults);
  return $(function() {
    if (peekEnabled()) {
      return $(this).trigger('peek:update');
    }
  });
})(jQuery);