summaryrefslogtreecommitdiff
path: root/vendor/assets/javascripts/peek.js
blob: f7e77de34ff36138bf2d754b3664c05ab5d68378 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
(function($) {
  var fetchRequestResults, getRequestId, peekEnabled, toggleBar, updatePerformanceBar;
  getRequestId = function() {
    return $('#peek').data('request-id');
  };
  peekEnabled = function() {
    return $('#peek').length;
  };
  updatePerformanceBar = function(results) {
    var key, label, data, table, html, tr, duration_td, sql_td, strong;

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

        if (label == 'queries') {
          table = document.createElement('table');

          for (var i = 0; i < data.length; i += 1) {
            tr = document.createElement('tr');
            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);

            table.appendChild(tr);
          }

          table.className = 'table';
          $("[data-defer-to=" + key + "-" + label + "]").html(table);
        } else {
          $("[data-defer-to=" + key + "-" + label + "]").text(results.data[key][label]);
        }
      });
    });
    return $(document).trigger('peek:render', [getRequestId(), results]);
  };
  toggleBar = function(event) {
    var wrapper;
    if ($(event.target).is(':input')) {
      return;
    }
    if (event.which === 96 && !event.metaKey) {
      wrapper = $('#peek');
      if (wrapper.hasClass('disabled')) {
        wrapper.removeClass('disabled');
        return document.cookie = "peek=true; path=/";
      } else {
        wrapper.addClass('disabled');
        return document.cookie = "peek=false; path=/";
      }
    }
  };
  fetchRequestResults = function() {
    return $.ajax('/-/peek/results', {
      data: {
        request_id: getRequestId()
      },
      success: function(data, textStatus, xhr) {
        return updatePerformanceBar(data);
      },
      error: function(xhr, textStatus, error) {}
    });
  };
  $(document).on('keypress', toggleBar);
  $(document).on('peek:update', fetchRequestResults);
  return $(function() {
    if (peekEnabled()) {
      return $(this).trigger('peek:update');
    }
  });
})(jQuery);