Skip to content

Commit

Permalink
handle track_cpu_percent option
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenstetzler committed Jul 23, 2021
1 parent d3e0f6f commit d16501a
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions jupyter_resource_usage/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ define([
$('#maintoolbar-container').append(
$('<div>').attr('id', 'jupyter-resource-usage-display-cpu')
.addClass('btn-group')
.addClass('jupyter-resource-usage-hide')
.addClass('pull-right').append(
$('<strong>').text(' CPU: ')
).append(
Expand All @@ -28,6 +29,9 @@ define([
$('head').append(
$('<style>').html('.jupyter-resource-usage-warn { background-color: #FFD2D2; color: #D8000C; }')
);
$('head').append(
$('<style>').html('.jupyter-resource-usage-hide { display: none; }')
);
$('head').append(
$('<style>').html('#jupyter-resource-usage-display { padding: 2px 8px; }')
);
Expand Down Expand Up @@ -67,28 +71,29 @@ define([
}

$('#jupyter-resource-usage-mem').text(display);
}
});
$.getJSON({
url: utils.get_body_data('baseUrl') + 'api/metrics/v1',
success: function (data) {
var cpuPercent = data['cpu_percent'];
var maxCpu = data['cpu_count'];
var limits = data['limits'];
var display = parseFloat(cpuPercent).toFixed(0) + "% (";

if (limits['cpu']) {
if (limits['cpu']['cpu']) {
display += (Math.round(parseFloat(cpuPercent) / 100)).toString() + " / " + maxCpu + ")";
}
if (limits['cpu']['warn']) {
$('#jupyter-resource-usage-display-cpu').addClass('jupyter-resource-usage-warn');
} else {
$('#jupyter-resource-usage-display-cpu').removeClass('jupyter-resource-usage-warn');
// Handle CPU display
var cpuPercent = data['cpu_percent'];
if (cpuPercent) {
// Remove hide CSS class if the metrics API gives us a CPU percent to display
$('#jupyter-resource-usage-display-cpu').removeClass('jupyter-resource-usage-hide');
var maxCpu = data['cpu_count'];
var limits = data['limits'];
// Display CPU usage as "{percent}% ({usedCpu} / {maxCPU})" e.g. "123% (1 / 8)"
var percentString = parseFloat(cpuPercent).toFixed(0);
var usedCpu = Math.round(parseFloat(cpuPercent) / 100).toString();
var display = `${percentString}% (${usedCpu} / ${maxCpu})`;
// Handle limit warning
if (limits['cpu']) {
if (limits['cpu']['warn']) {
$('#jupyter-resource-usage-display-cpu').addClass('jupyter-resource-usage-warn');
} else {
$('#jupyter-resource-usage-display-cpu').removeClass('jupyter-resource-usage-warn');
}
}

$('#jupyter-resource-usage-cpu').text(display);
}

$('#jupyter-resource-usage-cpu').text(display);
}
});
};
Expand Down

0 comments on commit d16501a

Please sign in to comment.