Skip to content

Instantly share code, notes, and snippets.

@Juhlinus
Created September 20, 2019 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Juhlinus/20db183b8797872bf0ab72fffc746954 to your computer and use it in GitHub Desktop.
Save Juhlinus/20db183b8797872bf0ab72fffc746954 to your computer and use it in GitHub Desktop.
(function() {
if ($('.sort-queries-by-time').length == 0) {
$('.phpdebugbar-widgets-sqlqueries .phpdebugbar-widgets-status')
.append('<a class="sort-queries-by-time" style="padding: 2px 11px 2px 7px;margin-right: 5px;margin-top: -1px;border-radius: 5px;text-transform: uppercase;font-weight: bold;color: #ffffff;font-size: 11px;background: #f4645f;box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);float: right;line-height: 1.8em;">✌ Duration</a>');
}
function parseToMicroSeconds(time) {
var format = time.replace(/[0-9]/g, '').replace('.', '');
time = parseFloat(time)
switch (format) {
case 's':
return time * 1000000
break;
case 'ms':
return time * 1000
break;
case 'μs':
return time
break;
}
return null;
}
var type = null;
$(".sort-queries-by-time").off('click');
$(".sort-queries-by-time").click(function() {
var $this = $(this);
if (!type) {
$this.text('👆 Duration');
type = 'desc'
}
if (type == 'desc') {
$this.text('👇 Duration');
type = 'asc'
} else {
$this.text('👆 Duration');
type = 'desc'
}
var items = $this.parent().parent().find('.phpdebugbar-widgets-list li').get();
items.sort(function(a, b) {
var valueA = parseToMicroSeconds($(a).find("span.phpdebugbar-widgets-duration").text());
var valueB = parseToMicroSeconds($(b).find("span.phpdebugbar-widgets-duration").text());
if (type == 'desc') {
if (valueA < valueB)
return 1;
if (valueA > valueB)
return -1;
}
if (type == 'asc') {
if (valueA < valueB)
return -1;
if (valueA > valueB)
return 1;
}
return 0;
});
// clear the list and re-add sorted items on button click
$this.parent().parent().find(".phpdebugbar-widgets-list").empty().append(items);
});
}
)()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment