Skip to content

Commit f125e71

Browse files
authored
Merge pull request #105 from stevenstetzler/master
Add CPU display to extension
2 parents bf688a9 + d16501a commit f125e71

File tree

1 file changed

+40
-0
lines changed
  • jupyter_resource_usage/static

1 file changed

+40
-0
lines changed

jupyter_resource_usage/static/main.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,30 @@ define([
1414
.attr('title', 'Actively used Memory (updates every 5s)')
1515
)
1616
);
17+
$('#maintoolbar-container').append(
18+
$('<div>').attr('id', 'jupyter-resource-usage-display-cpu')
19+
.addClass('btn-group')
20+
.addClass('jupyter-resource-usage-hide')
21+
.addClass('pull-right').append(
22+
$('<strong>').text(' CPU: ')
23+
).append(
24+
$('<span>').attr('id', 'jupyter-resource-usage-cpu')
25+
.attr('title', 'Actively used CPU (updates every 5s)')
26+
)
27+
);
1728
// FIXME: Do something cleaner to get styles in here?
1829
$('head').append(
1930
$('<style>').html('.jupyter-resource-usage-warn { background-color: #FFD2D2; color: #D8000C; }')
2031
);
32+
$('head').append(
33+
$('<style>').html('.jupyter-resource-usage-hide { display: none; }')
34+
);
2135
$('head').append(
2236
$('<style>').html('#jupyter-resource-usage-display { padding: 2px 8px; }')
2337
);
38+
$('head').append(
39+
$('<style>').html('#jupyter-resource-usage-display-cpu { padding: 2px 8px; }')
40+
);
2441
}
2542

2643
function humanFileSize(size) {
@@ -54,6 +71,29 @@ define([
5471
}
5572

5673
$('#jupyter-resource-usage-mem').text(display);
74+
75+
// Handle CPU display
76+
var cpuPercent = data['cpu_percent'];
77+
if (cpuPercent) {
78+
// Remove hide CSS class if the metrics API gives us a CPU percent to display
79+
$('#jupyter-resource-usage-display-cpu').removeClass('jupyter-resource-usage-hide');
80+
var maxCpu = data['cpu_count'];
81+
var limits = data['limits'];
82+
// Display CPU usage as "{percent}% ({usedCpu} / {maxCPU})" e.g. "123% (1 / 8)"
83+
var percentString = parseFloat(cpuPercent).toFixed(0);
84+
var usedCpu = Math.round(parseFloat(cpuPercent) / 100).toString();
85+
var display = `${percentString}% (${usedCpu} / ${maxCpu})`;
86+
// Handle limit warning
87+
if (limits['cpu']) {
88+
if (limits['cpu']['warn']) {
89+
$('#jupyter-resource-usage-display-cpu').addClass('jupyter-resource-usage-warn');
90+
} else {
91+
$('#jupyter-resource-usage-display-cpu').removeClass('jupyter-resource-usage-warn');
92+
}
93+
}
94+
95+
$('#jupyter-resource-usage-cpu').text(display);
96+
}
5797
}
5898
});
5999
};

0 commit comments

Comments
 (0)