Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CPU display to extension #105

Merged
merged 2 commits into from
Dec 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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