<commit_before>'use strict';
const http = require('http');
const cookie_reader = require('cookie');
const querystring = require('querystring');
const request = require('request');

const NODE_PORT = process.env.NODE_PORT;
const HOST = process.env.NODE_HOST;
const HOST_PORT = process.env.NODE_HOST_PORT;
const DEBUG = false;

const app = http.createServer().listen(NODE_PORT);
const io = require('socket.io')(app);
const connected_users = {};

io.use(function (socket, next) {
  const handshakeData = socket.request;
  if(handshakeData.headers.cookie){
    next();
  }
  next(new Error('not authorized'));
});

io.on('connection', function (socket) {
  const user = cookie_reader.parse(socket.request.headers.cookie).sessionid
  if (user in connected_users) {
    connected_users[user] += 1;
  } else {
    connected_users[user] = 1;
    if (DEBUG) console.log(user + ' connected');
    // Tell django the user has come online
    request({
      url: HOST + '/user/node_api',
      qs: {sessionid: user, status: 'connected'}
    });
  }
  socket.on('disconnect', function () {
    setTimeout(function () {
      if (connected_users[user] === 1) {
        delete connected_users[user];
        if (DEBUG) console.log(user + ' disconnected');
        // Tell django the user is now offline
	request({
	  url: HOST + '/user/node_api',
	  qs: {sessionid: user, status: 'disconnected'}
	});
      } else {
        connected_users[user] -= 1;
      }
    }, 5000);
  });
})<commit_msg>some refactoring<commit_after>'use strict';
const http = require('http');
const cookie_reader = require('cookie');
const request = require('request');

const NODE_PORT = process.env.NODE_PORT;
const HOST = process.env.NODE_HOST;
const DEBUG = false;

const app = http.createServer().listen(NODE_PORT);
const io = require('socket.io')(app);
const connected_users = {};

io.use(function (socket, next) {
  const handshakeData = socket.request;
  if(handshakeData.headers.cookie){
    next();
  }
  next(new Error('not authorized'));
});

function handleConnection(socket, user) {
  if (user in connected_users) {
    connected_users[user] += 1;
  } else {
    connected_users[user] = 1;
    if (DEBUG) console.log(user + ' connected');
    // Tell django the user has come online
    request({
      url: HOST + '/user/node_api',
      qs: {sessionid: user, status: 'connected'}
    });
  }
}

function handleDisconnection(socket, user) {
  socket.on('disconnect', function () {
    setTimeout(function () {
      if (connected_users[user] === 1) {
        delete connected_users[user];
        if (DEBUG) console.log(user + ' disconnected');
        // Tell django the user is now offline
  request({
    url: HOST + '/user/node_api',
    qs: {sessionid: user, status: 'disconnected'}
  });
      } else {
        connected_users[user] -= 1;
      }
    }, 5000);
  });
}

io.on('connection', function (socket) {
  const user = cookie_reader.parse(socket.request.headers.cookie).sessionid;
  handleConnection(socket, user);
  handleDisconnection(socket, user);
});
{'fraction_non_alphanumeric': 0.15182385803483406, 'fraction_numerical': 0.0052579691094314825, 'mean_word_length': 3.654434250764526, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '317888', 'n_tokens_mistral': 970, 'n_tokens_neox': 943, 'n_words': 229}