reorganize code of console module

This commit is contained in:
Jakob Schrettenbrunner 2017-01-26 22:20:04 +01:00
parent 96f5d15811
commit 5142dce0e0

View file

@ -25,6 +25,12 @@ const Console = (function () {
var terminalQueue; var terminalQueue;
var terminal; var terminal;
var cpuChart;
var cpuData;
var memoryChart;
var memoryData;
var timeLabels;
function initConsole() { function initConsole() {
termianlQueue = []; termianlQueue = [];
terminal = $('#terminal').terminal(function (command, term) { terminal = $('#terminal').terminal(function (command, term) {
@ -41,38 +47,9 @@ const Console = (function () {
return false; return false;
} }
}); });
Socket.on('initial status', function (data) {
terminal.clear();
if (data.status === 1 || data.status === 2) {
Socket.emit('send server log');
}
});
} }
function pushOutputQueue() { function initGraphs() {
if (termianlQueue.length > CONSOLE_PUSH_COUNT) {
// console throttled warning show
}
if (termianlQueue.length > 0) {
for (var i = 0; i < CONSOLE_PUSH_COUNT && termianlQueue.length > 0; i++) {
terminal.echo(termianlQueue[0]);
termianlQueue.shift();
}
}
window.setTimeout(pushOutputQueue, CONSOLE_PUSH_FREQ);
}
return {
init: function () {
$('[data-attr="power"]').click(function (event) {
if (! $(this).hasClass('disabled')) {
Socket.emit('set status', $(this).data('action'));
}
});
var ctc = $('#chart_cpu'); var ctc = $('#chart_cpu');
var timeLabels = []; var timeLabels = [];
var cpuData = []; var cpuData = [];
@ -163,6 +140,24 @@ const Console = (function () {
} }
} }
}); });
}
function addSocketListeners() {
// Update Listings on Initial Status
Socket.on('initial status', function (data) {
updateServerPowerControls(data.status);
terminal.clear();
if (data.status === 1 || data.status === 2) {
Socket.emit('send server log');
}
});
// Update Listings on Status
Socket.on('status', function (data) {
updateServerPowerControls(data.status);
});
Socket.on('proc', function (proc) { Socket.on('proc', function (proc) {
if (cpuData.length > 10) { if (cpuData.length > 10) {
cpuData.shift(); cpuData.shift();
@ -180,16 +175,22 @@ const Console = (function () {
CPUChart.update(); CPUChart.update();
MemoryChart.update(); MemoryChart.update();
}); });
}
// Update Listings on Initial Status function pushOutputQueue() {
Socket.on('initial status', function (data) { if (termianlQueue.length > CONSOLE_PUSH_COUNT) {
updateServerPowerControls(data.status); // console throttled warning show
}); }
// Update Listings on Status if (termianlQueue.length > 0) {
Socket.on('status', function (data) { for (var i = 0; i < CONSOLE_PUSH_COUNT && termianlQueue.length > 0; i++) {
updateServerPowerControls(data.status); terminal.echo(termianlQueue[0]);
}); termianlQueue.shift();
}
}
window.setTimeout(pushOutputQueue, CONSOLE_PUSH_FREQ);
}
function updateServerPowerControls (data) { function updateServerPowerControls (data) {
// Server is On or Starting // Server is On or Starting
@ -209,6 +210,20 @@ const Console = (function () {
$('[data-attr="power"][data-action="kill"]').addClass('disabled'); $('[data-attr="power"][data-action="kill"]').addClass('disabled');
} }
} }
return {
init: function () {
initConsole();
pushOutputQueue();
initGraphs();
addSocketListeners();
$('[data-attr="power"]').click(function (event) {
if (! $(this).hasClass('disabled')) {
Socket.emit('set status', $(this).data('action'));
}
});
}, },
getTerminal: function() { getTerminal: function() {