-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathmain.js
150 lines (137 loc) · 6.45 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
define([ // eslint-disable-line no-undef
'jquery',
'base/js/utils'
], ($, utils) => {
function setupDOM() {
$('#maintoolbar-container').append(
$('<div>').attr('id', 'jupyter-resource-usage-display-disk')
.addClass('btn-group')
.addClass('jupyter-resource-usage-hide')
.addClass('pull-right').append(
$('<strong>').text(' Disk: ')
).append(
$('<span>').attr('id', 'jupyter-resource-usage-disk')
.attr('title', 'Actively used CPU (updates every 5s)')
)
);
$('#maintoolbar-container').append(
$('<div>').attr('id', 'jupyter-resource-usage-display')
.addClass('btn-group')
.addClass('pull-right')
.append(
$('<strong>').text('Memory: ')
).append(
$('<span>').attr('id', 'jupyter-resource-usage-mem')
.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)')
)
);
$('head').append(
$('<style>').html(`
.jupyter-resource-usage-warn { background-color: #FFD2D2; color: #D8000C; }
.jupyter-resource-usage-hide { display: none; }
#jupyter-resource-usage-display { padding: 2px 8px; }
#jupyter-resource-usage-display-cpu { padding: 2px 8px; }
#jupyter-resource-usage-display-disk { padding: 2px 8px; }
`
));
function humanFileSize(size) {
var i = Math.floor(Math.log(size) / Math.log(1024));
return (size / Math.pow(1024, i)).toFixed(1) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
}
var displayMetrics = function () {
if (document.hidden) { // eslint-disable-line no-undef
// Don't poll when nobody is looking
return;
}
$.getJSON({
url: utils.get_body_data('baseUrl') + 'api/metrics/v1',
success: function (data) {
let value = data['pss'] || data['rss'];
let totalMemoryUsage = humanFileSize(value);
var limits = data['limits'];
var display = totalMemoryUsage;
if (limits['memory']) {
var limit = limits['memory']['pss'] ?? limits['memory']['rss'];
if (limit) {
let maxMemoryUsage = humanFileSize(limit);
display += ' / ' + maxMemoryUsage
}
if (limits['memory']['warn']) {
$('#jupyter-resource-usage-display').addClass('jupyter-resource-usage-warn');
} else {
$('#jupyter-resource-usage-display').removeClass('jupyter-resource-usage-warn');
}
}
$('#jupyter-resource-usage-mem').text(display);
// Handle CPU display
var cpuPercent = data['cpu_percent'];
if (cpuPercent !== undefined) {
// 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');
display = '';
var maxCpu = data['cpu_count'];
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();
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);
}
// Handle Disk display
var maxDisk = data['disk_total'];
if (maxDisk !== undefined) {
// Remove hide CSS class if the metrics API gives us a CPU percent to display
$('#jupyter-resource-usage-display-disk').removeClass('jupyter-resource-usage-hide');
display = '';
var currentDisk = data['disk_used'];
display = humanFileSize(maxDisk) + ' / ' + humanFileSize(currentDisk);
// Handle limit warning
if (limits['disk']) {
if (limits['disk']['warn']) {
$('#jupyter-resource-usage-display-disk').addClass('jupyter-resource-usage-warn');
} else {
$('#jupyter-resource-usage-display-disk').removeClass('jupyter-resource-usage-warn');
}
}
$('#jupyter-resource-usage-disk').text(display);
}
}
});
};
var load_ipython_extension = function () {
setupDOM();
displayMetrics();
// Update every five seconds, eh?
setInterval(displayMetrics, 1000 * 5); // eslint-disable-line no-undef
// eslint-disable-next-line no-undef
document.addEventListener('visibilitychange', () => {
// Update instantly when user activates notebook tab
// FIXME: Turn off update timer completely when tab not in focus
if (!document.hidden) { // eslint-disable-line no-undef
displayMetrics();
}
}, false);
};
return {
load_ipython_extension: load_ipython_extension,
};
}});