summaryrefslogtreecommitdiffstats
path: root/lib/monitor/cpu.js
blob: cc446a88ea417f8a466b697d6adb1c111f55068c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
var si = require('systeminformation'),
  utils = require('../utils');

var colors = utils.colors;

function Cpu(line) {
  this.line = line;
  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(() => {
      si.currentLoad(data => {
        this.updateData(data);
      })
    }, 1000);
  });
}

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 + '\%';

    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;