Skip to content

Commit

Permalink
Merge pull request #105 from stevenstetzler/master
Browse files Browse the repository at this point in the history
Add CPU display to extension
  • Loading branch information
jtpio authored Dec 3, 2021
2 parents bf688a9 + d16501a commit f125e71
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions jupyter_resource_usage/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,30 @@ define([
.attr('title', 'Actively used Memory (updates every 5s)')
)
);
$('#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(
$('<span>').attr('id', 'jupyter-resource-usage-cpu')
.attr('title', 'Actively used CPU (updates every 5s)')
)
);
// FIXME: Do something cleaner to get styles in here?
$('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; }')
);
$('head').append(
$('<style>').html('#jupyter-resource-usage-display-cpu { padding: 2px 8px; }')
);
}

function humanFileSize(size) {
Expand Down Expand Up @@ -54,6 +71,29 @@ define([
}

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

// 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);
}
}
});
};
Expand Down

0 comments on commit f125e71

Please sign in to comment.