<commit_before>var WebSocketClient = require('websocket').client;

var lichessSri = Math.random().toString(36).substring(2);

var url = 'ws://socket.en.lichess.org/lobby/socket';
var client = new WebSocketClient();

var connect = function() {
  console.log('Connect to ' + url);
  client.connect(url + '?sri=' + lichessSri);
};

client.on('connectFailed', function(error) {
  console.log('Connect Error: ' + error.toString());
  setTimeout(connect, 5000);
});

client.on('connect', function(connection) {
  console.log('WebSocket client connected');
  connection.on('error', function(error) {
    console.log("Connection Error: " + error.toString());
  });
  connection.on('close', function() {
    console.log('echo-protocol Connection Closed');
    setTimeout(connect, 5000);
  });
  connection.on('message', function(message) {
    var data = JSON.parse(message.utf8Data);
    if (data.t == 'n') {
      var lag = new Date() - pingAt;
      process.stdout.write(lag + ' ');
      if (lag > 100) console.log('Lag alert: ' + lag);
      setTimeout(ping, 5000);
    }
  });

  var pingAt;
  var ping = function() {
    pingAt = new Date();
    connection.sendUTF(JSON.stringify({
      t: 'p',
      v: 0
    }));
  };
  ping();
});

connect();<commit_msg>improve websocket monitor<commit_after>var WebSocketClient = require('websocket').client;

var lichessSri = Math.random().toString(36).substring(2);

var url = 'ws://socket.en.lichess.org/lobby/socket';
var client = new WebSocketClient();
var averageLag;

var connect = function() {
  console.log('[info] Connect to ' + url);
  client.connect(url + '?sri=' + lichessSri);
};

client.on('connectFailed', function(error) {
  console.log('[error] connectFailed: ' + error.toString());
  setTimeout(connect, 5000);
});

client.on('connect', function(connection) {
  console.log('[info] Client connected. Average lag:');
  connection.on('error', function(error) {
    console.log('[error] ' + error.toString());
  });
  connection.on('close', function() {
    console.log('[info] Connection closed');
    averageLag = null;
    setTimeout(connect, 5000);
  });
  connection.on('message', function(message) {
    var data = JSON.parse(message.utf8Data);
    if (data.t == 'n') {
      var lag = new Date() - pingAt;
      if (lag > 100) console.log('[alert] High lag: ' + lag);
      if (!averageLag) averageLag = lag;
      else averageLag = 0.2 * (lag - averageLag) + averageLag;
      setTimeout(ping, 1000);
    }
  });

  var pingAt;
  var ping = function() {
    pingAt = new Date();
    connection.sendUTF(JSON.stringify({
      t: 'p',
      v: 0
    }));
  };
  ping();
});

connect();

setInterval(function() {
  if (averageLag !== null)
    console.log(Math.round(averageLag * 10) / 10);
}, 10000);
{'fraction_non_alphanumeric': 0.18339978252990213, 'fraction_numerical': 0.018484958318231243, 'mean_word_length': 3.6859083191850592, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3652622', 'n_tokens_mistral': 964, 'n_tokens_neox': 894, 'n_words': 191}