1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 11:16:47 +00:00
webvirtcloud/static/js/poll.js
catborise 24231813d7 perf(instances): the VM stats no longer wait a second per poll
Each stats answer carries a signed sample of the counters, bound to the VM,
its compute and UUID. The next poll sends it back and the rates are the
averages since then; a sample from another run of the VM (domain ID),
another host CPU count or device list, or older than 30 s falls back to
one second as before. Windows are measured on the monotonic clock.

Now that a poll is cheap, the Stats tab polls every 5 s like the host
graph, the first time as soon as it opens (pollWhileVisible takes the
delay of the first request).

A counter the host reports as unsupported (-1) or that went back gives no
value instead of 0 or a jump; a NIC without a target keeps its chart, a
pool volume disk is measured, and a disk or NIC attached after the page
loaded no longer stops the other charts. Disk rates are MB/s (they were
MiB/s labelled Mb/s), network Mbit/s; the CPU tooltip names the window.
2026-10-09 11:59:41 +03:00

51 lines
1.7 KiB
JavaScript

// Repeats request() while the page is visible and passes each answer to
// onData. request() returns a jQuery promise (e.g. $.getJSON(url)); onData is
// called after the poller's own bookkeeping, so an error in it does not stop
// the polling. One request at a time: the next one starts
// interval ms after the previous one has finished, so a slow host does not
// pile up requests, each holding a web worker. A hidden page asks nothing;
// when it is shown again it asks at once. stop() pauses, start() resumes
// at once (for example while a tab of the page is shown). The first request
// goes out after firstDelay ms, the interval if it is not given.
function pollWhileVisible(interval, request, onData, firstDelay) {
var timer = null;
var running = false;
var stopped = false;
function run() {
timer = null;
if (stopped || document.hidden) {
return;
}
running = true;
request()
.always(function () {
running = false;
if (!stopped && !document.hidden) {
timer = setTimeout(run, interval);
}
})
.done(onData);
}
function resume() {
if (!stopped && !document.hidden && !running && timer === null) {
run();
}
}
document.addEventListener("visibilitychange", resume);
timer = setTimeout(run, firstDelay === undefined ? interval : firstDelay);
return {
stop: function () {
stopped = true;
clearTimeout(timer);
timer = null;
},
start: function () {
stopped = false;
resume();
},
};
}