summaryrefslogtreecommitdiffstats
path: root/lib/monitor/net.js
blob: 474e11c8c49881d8da8f754519c12b35b091017a (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
var si = require('systeminformation'),
  utils = require('../utils');

var colors = utils.colors;

function Net(sparkline) {
  this.sparkline = sparkline;
  this.netData = [Array(61).fill(0), Array(61).fill(0)];

  si.networkInterfaceDefault(iface => {
    var that = this;
    var updater = function() {
      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']);

  this.netData[0].shift();
  this.netData[0].push(rx_sec);

  this.netData[1].shift();
  this.netData[1].push(tx_sec);

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

  tx_label =
    'Transferring:      ' +
    utils.humanFileSize(tx_sec) +
    '/s \nTotal transferred: ' +
    utils.humanFileSize(data['tx_bytes']);

  this.sparkline.setData([rx_label, tx_label], this.netData);
  this.sparkline.screen.render();
};

module.exports = Net;