2017-06-07 17:55:07 +00:00
|
|
|
let requestId = null;
|
2017-05-17 11:20:55 +00:00
|
|
|
|
|
|
|
(function($) {
|
2017-06-07 14:49:36 +00:00
|
|
|
var fetchRequestResults, getRequestId, peekEnabled, toggleBar, updatePerformanceBar;
|
2017-05-17 11:20:55 +00:00
|
|
|
getRequestId = function() {
|
|
|
|
if (requestId != null) {
|
|
|
|
return requestId;
|
|
|
|
} else {
|
|
|
|
return $('#peek').data('request-id');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
peekEnabled = function() {
|
|
|
|
return $('#peek').length;
|
|
|
|
};
|
|
|
|
updatePerformanceBar = function(results) {
|
2017-06-07 14:53:13 +00:00
|
|
|
var key, label, data, table, html, tr, duration_td, sql_td, strong;
|
2017-06-07 17:55:07 +00:00
|
|
|
|
|
|
|
Object.keys(results.data).forEach((key) => {
|
|
|
|
Object.keys(results.data[key]).forEach((label) => {
|
2017-06-06 19:24:08 +00:00
|
|
|
data = results.data[key][label];
|
2017-06-07 17:55:07 +00:00
|
|
|
|
2017-06-07 14:53:13 +00:00
|
|
|
if (label == 'queries') {
|
2017-06-06 19:24:08 +00:00
|
|
|
table = document.createElement('table');
|
|
|
|
|
|
|
|
for (var i = 0; i < data.length; i += 1) {
|
|
|
|
tr = document.createElement('tr');
|
2017-06-07 14:53:13 +00:00
|
|
|
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);
|
2017-06-06 19:24:08 +00:00
|
|
|
|
|
|
|
table.appendChild(tr);
|
|
|
|
}
|
|
|
|
|
2017-06-07 14:53:13 +00:00
|
|
|
table.className = 'table';
|
|
|
|
$("[data-defer-to=" + key + "-" + label + "]").html(table);
|
2017-06-07 17:55:07 +00:00
|
|
|
} else {
|
2017-06-06 19:24:08 +00:00
|
|
|
$("[data-defer-to=" + key + "-" + label + "]").text(results.data[key][label]);
|
|
|
|
}
|
2017-06-07 17:55:07 +00:00
|
|
|
});
|
|
|
|
});
|
2017-05-17 11:20:55 +00:00
|
|
|
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);
|