mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-02 17:06:09 +00:00
On a phone the host's section menu wrapped to three lines, each new line starting with a divider. It is now one row that scrolls sideways, with the current section in sight (scroll_rows.js, which also takes over the VM page's tab rows from instance_page.js). The filter list, a filter's rules and the PCI and USB device tables were wider than a phone and hid their actions and users off screen; on a phone they now keep the name, the key column and the actions, as the other lists do. The overview's live CPU and memory figures, the chart axes and tooltips, and the VM page's stats now write numbers as the page's language does (a decimal comma in Turkish), like the cards beside them. The capacity cards said "0 bytes running"; they now say what they count: "0 bytes in running VMs". A pool's type is written as libvirt names it on both the list and its page, and the device search sits where the interfaces search does.
115 lines
5.5 KiB
JavaScript
115 lines
5.5 KiB
JavaScript
// The VM page's Stats tab (stats_tab.html): its charts are polled while the
|
||
// tab is shown and made once; superusers' Logs view loads its rows each time
|
||
// it is shown.
|
||
(function () {
|
||
var pane = document.getElementById("graphics");
|
||
var stats_tab = document.querySelector('#graphics-tab');
|
||
var stats_poll = null;
|
||
// the seconds each CPU point averages
|
||
var cpuWindows = [];
|
||
// the usage sample of the previous answer: the next one averages since then
|
||
var statsSample = null;
|
||
// a reading for the line above a chart: one decimal, a dash where it is missing
|
||
function reading(value) {
|
||
return value == null ? "–" : formatNumber(value);
|
||
}
|
||
stats_tab.addEventListener('hidden.bs.tab', function () {
|
||
stats_poll.stop();
|
||
});
|
||
stats_tab.addEventListener('shown.bs.tab', function () {
|
||
if (stats_poll !== null) {
|
||
stats_poll.start();
|
||
return;
|
||
}
|
||
var cpuChart = lineChart(document.getElementById("cpuChart"), "%", [pane.dataset.usage], {
|
||
max: 100,
|
||
tooltip: function (context, text) {
|
||
var span = cpuWindows[context.dataIndex];
|
||
return span ? text + ' (' + pane.dataset.averageOver + ' ' + formatNumber(span) + ' s)' : text;
|
||
},
|
||
});
|
||
var memChart = lineChart(document.getElementById("memChart"), "MiB", [pane.dataset.usageRss]);
|
||
var diskChart = {};
|
||
pane.querySelectorAll("canvas[data-disk]").forEach(function (canvas) {
|
||
diskChart[canvas.dataset.disk] = lineChart(canvas, "MB/s", [pane.dataset.read, pane.dataset.write]);
|
||
});
|
||
// two NICs with one MAC (a misconfiguration) share the first one's chart
|
||
var netChart = {};
|
||
pane.querySelectorAll("canvas[data-mac]").forEach(function (canvas) {
|
||
var mac = canvas.dataset.mac;
|
||
netChart[mac] = netChart[mac] || lineChart(canvas, "Mbit/s", [pane.dataset.in, pane.dataset.out]);
|
||
});
|
||
|
||
// the first point at once, then every 5 s like the host graph
|
||
stats_poll = pollWhileVisible(5000, function () {
|
||
return $.getJSON(pane.dataset.url, statsSample ? {sample: statsSample} : {})
|
||
.fail(function () {
|
||
statsSample = null;
|
||
});
|
||
}, function graph_usage(data) {
|
||
statsSample = data.sample || null;
|
||
cpuWindows.push(data.window);
|
||
if (cpuWindows.length > CHART_POINTS) {
|
||
cpuWindows.shift();
|
||
}
|
||
addPoint(cpuChart, data.timeline, [data.cpudata]);
|
||
document.getElementById("cpuNow").textContent = reading(data.cpudata) + " %";
|
||
// a VM whose memory could not be read gives total 0: a gap, same axis;
|
||
// the RSS may pass the balloon size: then the axis grows to it
|
||
if (data.memdata.total) {
|
||
var used = Math.round(data.memdata.used / 1024), total = Math.round(data.memdata.total / 1024);
|
||
memChart.options.scales.y.max = Math.max(total, used);
|
||
addPoint(memChart, data.timeline, [used]);
|
||
document.getElementById("memNow").textContent = used + " / " + total + " MiB";
|
||
} else {
|
||
addPoint(memChart, data.timeline, [null]);
|
||
document.getElementById("memNow").textContent = "";
|
||
}
|
||
// a disk or NIC added after the page loaded has no chart until a reload,
|
||
// and one removed keeps its last points
|
||
function push(chart, id, labels, unit, values) {
|
||
if (chart) {
|
||
addPoint(chart, data.timeline, values);
|
||
document.getElementById(id).textContent =
|
||
labels[0] + " " + reading(values[0]) + " · " + labels[1] + " " + reading(values[1]) + " " + unit;
|
||
}
|
||
}
|
||
data.blkdata.forEach(function (disk) {
|
||
push(diskChart[disk.dev], "blk" + disk.dev + "Now", [pane.dataset.read, pane.dataset.write], "MB/s", disk.data);
|
||
});
|
||
data.netdata.forEach(function (nic) {
|
||
push(netChart[nic.mac], "net" + nic.mac + "Now", [pane.dataset.in, pane.dataset.out], "Mbit/s", nic.data);
|
||
});
|
||
}, 0);
|
||
});
|
||
|
||
// a click or the arrow keys: the rows of the moment each time Logs is shown
|
||
var logsTab = document.getElementById("logs-tab");
|
||
if (!logsTab) return;
|
||
var table = document.getElementById("logs_table");
|
||
logsTab.addEventListener("shown.bs.tab", function () {
|
||
var tbody = $(table).children("tbody");
|
||
function note(text) {
|
||
tbody.empty().append($("<tr>").append($("<td colspan='4' class='text-body-secondary'>").text(text)));
|
||
}
|
||
$.getJSON(table.dataset.url, function (data) {
|
||
if (!data.length) {
|
||
note(table.dataset.empty);
|
||
return;
|
||
}
|
||
// Log messages contain user-entered names (snapshots, disks, clones);
|
||
// insert them as text, never as HTML.
|
||
tbody.empty();
|
||
$.each(data, function (id, row) {
|
||
$("<tr>")
|
||
.append($("<td>").addClass("text-nowrap").text(row['date']))
|
||
.append($("<td>").text(row['user']))
|
||
.append($("<td>").text(row['message']))
|
||
.append($("<td>").text(row['result']))
|
||
.appendTo(tbody);
|
||
});
|
||
}).fail(function () {
|
||
note(table.dataset.failed);
|
||
});
|
||
});
|
||
})();
|