Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/gtop.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var grid = new contrib.grid({
var cpuLine = grid.set(0, 0, 4, 12, contrib.line, {
showNthLabel: 5,
maxY: 100,
label: 'CPU History',
label: 'CPU Load History',
showLegend: true,
});

Expand Down
93 changes: 65 additions & 28 deletions lib/monitor/cpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,80 @@ var colors = utils.colors;

function Cpu(line) {
this.line = line;
this.currentMetric = 'CPU load';

si.cpuTemperature(data => {
this.cpuTemperatureData = data.cores.map((core, i) => this._createCpuSeries(i, colors));
this._updateSeriesData(data.cores, this.cpuTemperatureData, '°C');
this.tempInterval = setInterval(() => {
si.cpuTemperature(data => {
this._updateSeriesData(data.cores, this.cpuTemperatureData, '°C');
});
}, 1000);
});

si.currentLoad(data => {
this.cpuData = data.cpus.map((cpu, i) => {
return {
title: 'CPU' + (i + 1),
style: {
line: colors[i % colors.length],
},
x: Array(61)
.fill()
.map((_, i) => 60 - i),
y: Array(61).fill(0),
};
});
this.updateData(data);
this.interval = setInterval(() => {
this.cpuLoadData = data.cpus.map((cpu, i) => this._createCpuSeries(i, colors));
this._updateSeriesData(data.cpus.map(cpu => cpu.load.toFixed(1)), this.cpuLoadData, '%');
this.loadInterval = setInterval(() => {
si.currentLoad(data => {
this.updateData(data);
this._updateSeriesData(data.cpus.map(cpu => cpu.load.toFixed(1)), this.cpuLoadData, '%');
});
}, 1000);
});

this.line.screen.key(['s', 't'], function(ch, key) {
this._toggleCurrentMetric();
}.bind(this));
}

Cpu.prototype.updateData = function(data) {
data.cpus.forEach((cpu, i) => {
var loadString = cpu.load.toFixed(1).toString();
while (loadString.length < 6) {
loadString = ' ' + loadString;
}
loadString = loadString + '%';
Cpu.prototype._toggleCurrentMetric = function() {
if (this.currentMetric === 'CPU load') {
this.line.options.maxY = 120;
this.line.setLabel('CPU Temperature History');
this.currentMetric = 'CPU temperature';
this.line.setData(this.cpuLoadData);
this.line.screen.render();

this.cpuData[i].title = 'CPU' + (i + 1) + loadString;
this.cpuData[i].y.shift();
this.cpuData[i].y.push(cpu.load);
});
} else {
this.line.options.maxY = 100;
this.line.setLabel('CPU Load History');
this.currentMetric = 'CPU load';
this.line.setData(this.cpuTemperatureData);
this.line.screen.render();
}
}

this.line.setData(this.cpuData);
this.line.screen.render();
Cpu.prototype._createCpuSeries = function (index, colors, length = 61) {
return {
title: 'CPU' + (index + 1),
style: {
line: colors[index % colors.length],
},
x: Array(length).fill().map((_, i) => length - 1 - i),
y: Array(length).fill(0),
};
};

Cpu.prototype._updateSeriesData = function(dataArray, seriesArray, suffix) {
dataArray.forEach((value, i) => {
let str = value.toString();

let gap = 8 - Math.floor(Math.log10(i+1)) - suffix.length;

while (str.length < gap) str = ' ' + str;
str += suffix;

seriesArray[i].title = 'CPU' + (i + 1) + str;
seriesArray[i].y.shift();
seriesArray[i].y.push(value);
});

if ((suffix === '%' && this.currentMetric === 'CPU load') ||
(suffix === '°C' && this.currentMetric === 'CPU temperature')) {
this.line.setData(seriesArray);
this.line.screen.render();
}
}

module.exports = Cpu;