summaryrefslogtreecommitdiffstats
path: root/lib/monitor/mem.js
blob: b190535bb9b95bb3243c637a253bcc869fe4fcaa (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var si = require('systeminformation'),
  utils = require('../utils');

var colors = utils.colors;

function Mem(line, memDonut, swapDonut) {
  this.line = line;
  this.memDonut = memDonut;
  this.swapDonut = swapDonut;


  si.mem(data => {
    this.memData = [{
        title: 'Memory',
        style: {
          line: colors[0]
        },
        x: Array(61).fill().map((_, i) => 60 - i),
        y: Array(61).fill(0)
      },
      {
        title: 'Swap',
        style: {
          line: colors[1]
        },
        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();

  swapPer = isNaN(swapPer) ? 0 : swapPer;

  this.memData[0].y.shift();
  this.memData[0].y.push(memPer);

  this.memData[1].y.shift();
  this.memData[1].y.push(swapPer);

  var memTitle =
    utils.humanFileSize(data.total - data.available) +
    ' of ' +
    utils.humanFileSize(data.total);

  var swapTitle =
    utils.humanFileSize(data.swaptotal - data.swapfree) +
    ' of ' +
    utils.humanFileSize(data.swaptotal);

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

module.exports = Mem;