Skip to content

Commit

Permalink
lint & upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
aksakalli committed Feb 18, 2020
1 parent 4f9e3f4 commit 4adebff
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 449 deletions.
6 changes: 3 additions & 3 deletions lib/gtop.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var procTable = grid.set(8, 6, 4, 6, contrib.table, {
procTable.focus()

screen.render();
screen.on('resize', function(a) {
screen.on('resize', function (a) {
cpuLine.emit('attach');
memLine.emit('attach');
memDonut.emit('attach');
Expand All @@ -79,7 +79,7 @@ screen.on('resize', function(a) {
procTable.emit('attach');
});

screen.key(['escape', 'q', 'C-c'], function(ch, key) {
screen.key(['escape', 'q', 'C-c'], function (ch, key) {
return process.exit(0);
});

Expand All @@ -92,7 +92,7 @@ function init() {
}


process.on('uncaughtException', function(err) {
process.on('uncaughtException', function (err) {
// avoid exiting due to unsupported system resources in Windows
});

Expand Down
21 changes: 11 additions & 10 deletions lib/monitor/cpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ function Cpu(line) {
return {
title: 'CPU' + (i + 1),
style: {
line: colors[i % colors.length]
line: colors[i % colors.length],
},
x: Array(61).fill().map((_, i) => 60 - i),
y: Array(61).fill(0)
}
})
x: Array(61)
.fill()
.map((_, i) => 60 - i),
y: Array(61).fill(0),
};
});
this.updateData(data);
this.interval = setInterval(() => {
si.currentLoad(data => {
this.updateData(data);
})
});
}, 1000);
});
}
Expand All @@ -31,16 +33,15 @@ Cpu.prototype.updateData = function(data) {
while (loadString.length < 6) {
loadString = ' ' + loadString;
}
loadString = loadString + '\%';
loadString = loadString + '%';

this.cpuData[i].title = 'CPU' + (i + 1) + loadString;
this.cpuData[i].y.shift();
this.cpuData[i].y.push(cpu.load);
})
});

this.line.setData(this.cpuData);
this.line.screen.render();
}

};

module.exports = Cpu;
20 changes: 11 additions & 9 deletions lib/monitor/disk.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,30 @@ function Disk(donut) {

si.fsSize(data => {
this.updateData(data);
})
});

this.interval = setInterval(() => {
si.fsSize(data => {
this.updateData(data);
})
});
}, 10000);
}

Disk.prototype.updateData = function(data) {

var disk = data[0];

var label = utils.humanFileSize(disk.used, true) +
var label =
utils.humanFileSize(disk.used, true) +
' of ' +
utils.humanFileSize(disk.size, true);

this.donut.setData([{
percent: disk.use / 100,
label: label,
'color': colors[5]
}, ]);
this.donut.setData([
{
percent: disk.use / 100,
label: label,
color: colors[5],
},
]);
this.donut.screen.render();
};

Expand Down
4 changes: 2 additions & 2 deletions lib/monitor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ module.exports = {
Mem: require('./mem'),
Net: require('./net'),
Disk: require('./disk'),
Proc: require('./proc')
}
Proc: require('./proc'),
};
49 changes: 28 additions & 21 deletions lib/monitor/mem.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,39 @@ function Mem(line, memDonut, swapDonut) {
this.memDonut = memDonut;
this.swapDonut = swapDonut;


si.mem(data => {
this.memData = [{
this.memData = [
{
title: 'Memory',
style: {
line: colors[0]
line: colors[0],
},
x: Array(61).fill().map((_, i) => 60 - i),
y: Array(61).fill(0)
x: Array(61)
.fill()
.map((_, i) => 60 - i),
y: Array(61).fill(0),
},
{
title: 'Swap',
style: {
line: colors[1]
line: colors[1],
},
x: Array(61).fill().map((_, i) => 60 - i),
y: Array(61).fill(0)
}
x: Array(61)
.fill()
.map((_, i) => 60 - i),
y: Array(61).fill(0),
},
];
this.updateData(data);
this.interval = setInterval(() => {
si.mem(data => {
this.updateData(data);
})
});
}, 1000);
});
}

Mem.prototype.updateData = function(data) {

var memPer = (100 * (1 - data.available / data.total)).toFixed();
var swapPer = (100 * (1 - data.swapfree / data.swaptotal)).toFixed();

Expand All @@ -60,16 +63,20 @@ Mem.prototype.updateData = function(data) {
utils.humanFileSize(data.swaptotal);

this.line.setData(this.memData);
this.memDonut.setData([{
percent: memPer / 100,
label: memTitle,
'color': colors[0]
}, ]);
this.swapDonut.setData([{
percent: swapPer / 100,
label: swapTitle,
'color': colors[1]
}, ]);
this.memDonut.setData([
{
percent: memPer / 100,
label: memTitle,
color: colors[0],
},
]);
this.swapDonut.setData([
{
percent: swapPer / 100,
label: swapTitle,
color: colors[1],
},
]);
this.line.screen.render();
};

Expand Down
11 changes: 6 additions & 5 deletions lib/monitor/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ function Net(sparkline) {
si.networkStats(iface, data => {
that.updateData(data[0]);
});
}
};
updater();
this.interval = setInterval(updater, 1000);
})
});
}

Net.prototype.updateData = function(data) {

var rx_sec = Math.max(0, data['rx_sec']);
var tx_sec = Math.max(0, data['tx_sec']);

Expand All @@ -30,12 +29,14 @@ Net.prototype.updateData = function(data) {
this.netData[1].shift();
this.netData[1].push(tx_sec);

rx_label = 'Receiving: ' +
rx_label =
'Receiving: ' +
utils.humanFileSize(rx_sec) +
'/s \nTotal received: ' +
utils.humanFileSize(data['rx_bytes']);

tx_label = 'Transferring: ' +
tx_label =
'Transferring: ' +
utils.humanFileSize(tx_sec) +
'/s \nTotal transferred: ' +
utils.humanFileSize(data['tx_bytes']);
Expand Down
35 changes: 16 additions & 19 deletions lib/monitor/proc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ var colors = utils.colors;
var pars = {
p: 'pid',
c: 'pcpu',
m: 'pmem'
}
m: 'pmem',
};

function Proc(table) {
this.table = table;
Expand All @@ -21,14 +21,12 @@ function Proc(table) {
var updater = function() {
si.processes(data => {
that.updateData(data);
})
}
});
};
updater();
this.interval = setInterval(updater, 3000);
this.table.screen.key(['m', 'c', 'p'], function(ch, key) {

if (pars[ch] == that.pSort) {

that.reverse = !that.reverse;
} else {
that.pSort = pars[ch] || that.pSort;
Expand All @@ -37,7 +35,6 @@ function Proc(table) {
that.reIndex = true;
updater();
});

}

Proc.prototype.updateData = function(data) {
Expand All @@ -52,24 +49,24 @@ Proc.prototype.updateData = function(data) {
p.pid,
p.command, //.slice(0,10),
' ' + p.pcpu.toFixed(1),
p.pmem.toFixed(1)
]

})
p.pmem.toFixed(1),
];
});

var headers = ['PID', 'Command', '%CPU', '%MEM'];

headers[{
pid: 0,
pcpu: 2,
pmem: 3
}[this.pSort]] += this.reverse ? '▲' : '▼';

headers[
{
pid: 0,
pcpu: 2,
pmem: 3,
}[this.pSort]
] += this.reverse ? '▲' : '▼';

this.table.setData({
headers: headers,
data: this.reverse ? data.reverse() : data
})
data: this.reverse ? data.reverse() : data,
});

if (this.reIndex) {
this.table.rows.select(0);
Expand Down
17 changes: 11 additions & 6 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
var utils = {}

var utils = {};

utils.humanFileSize = function(bytes, isDecimal) {
isDecimal = (typeof isDecimal !== 'undefined') ? isDecimal : false;
isDecimal = typeof isDecimal !== "undefined" ? isDecimal : false;
if (bytes == 0) {
return "0.00 B";
}
var base = isDecimal ? 1000 : 1024;
var e = Math.floor(Math.log(bytes) / Math.log(base));
return (bytes / Math.pow(base, e)).toFixed(2) + ' ' + ' KMGTP'.charAt(e) + (isDecimal || e == 0 ? '' : 'i') + 'B';
}
return (
(bytes / Math.pow(base, e)).toFixed(2) +
" " +
" KMGTP".charAt(e) +
(isDecimal || e == 0 ? "" : "i") +
"B"
);
};

utils.colors = ['magenta', 'cyan', 'blue', 'yellow', 'green', 'red'];
utils.colors = ["magenta", "cyan", "blue", "yellow", "green", "red"];

module.exports = utils;
Loading

0 comments on commit 4adebff

Please sign in to comment.