<commit_before>var express = require('express')
var http = require('http')
var WebSocketServer = require('ws').Server
var Mixpanel = require('mixpanel')

var logger = require('./logger')
var io = require('socket.io')
var uuid = require('node-uuid')

var targets = {}
var sockets = {}
var sessions = {}
var mixpanel = Mixpanel.init(process.env.MIXPANEL_TOKEN)

logger.info('server.booting')

// HTTP for /json endpoint
logger.info('http.booting')

var app = express()
app.set('port', process.env.PORT || 8000)

app.use(express.static('web'));

app.get('/', function (req, res) {
  logger.info('http.index')
  res.json({
    msg: 'Hello from DevToolsRemote'
  })
})

app.get('/_stats', function (req, res) {
  logger.info('http.stats')
  res.json({
    count: {
      targets: Object.keys(targets).length,
      sockets: Object.keys(sockets).length,
      sessions: Object.keys(sessions).length
    }
  })
})

app.get('/:session/json', function (req, res) {

  var sessionId = req.params.session
  var sessionTargets = targets[sessionId]

  logger.info('http.targets', {
    targets: sessionTargets
  })

  res.send(sessionTargets)
})

var server = http.Server(app)

server.listen(app.get('port'), function () {
  logger.info('http.listening')
  logger.info('- listening on port %d in %s mode', app.get('port'), app.settings.env)
})

// Socket IO for Chrome Extension
logger.info('socket.booting')

var io = require('socket.io')(server)
io.sockets.on('connection', function (socket) {
  var sessionId = uuid()

  logger.info('socket.connection', sessionId)

  targets[sessionId] = []
  sockets[sessionId] = socket
  sessions[sessionId] = {
    startTime: new Date.getTime()
  }

  socket.on('disconnect', function () {
    logger.info('socket.disconnect')

    delete targets[sessionId]
    delete sockets[sessionId]
    delete sessions[sessionId]
  })

  socket.on('error', function (err) {
    logger.error('socket.error', err)
  })

  socket.on('hello', function (data) {
    logger.info('socket.hello', data)

    var webSocketUrl = (process.env.WEBSOCKET_DOMAIN ? process.env.WEBSOCKET_DOMAIN : ('localhost:' + app.get('port'))) + '/devtools/page/' + sessionId

    targets[sessionId].push({
      description: '',
      devtoolsFrontendUrl: '/devtools/devtools.html?ws=' + webSocketUrl,
      devtoolsUrl: 'chrome-devtools://devtools/remote/serve_rev/@06a2e65a4f3610ec17dbc5988c0b16a95825240a/inspector.html?ws=' + webSocketUrl + '&remoteFrontend=true&dockSide=unlocked',
      id: uuid(),
      title: data.title,
      type: 'page',
      url: data.url,
      webSocketDebuggerUrl: 'ws://' + webSocketUrl
    })

    mixpanel.track("sessions_created", {
      sessionId: sessionId
    });

    socket.emit('sessionCreated', sessionId)

  })
})<commit_msg>Try to use Mixpanel time_event to track session durations<commit_after>var express = require('express')
var http = require('http')
var WebSocketServer = require('ws').Server
var Mixpanel = require('mixpanel')

var logger = require('./logger')
var io = require('socket.io')
var uuid = require('node-uuid')

var targets = {}
var sockets = {}
var sessions = {}
var mixpanel = Mixpanel.init(process.env.MIXPANEL_TOKEN)

logger.info('server.booting')

// HTTP for /json endpoint
logger.info('http.booting')

var app = express()
app.set('port', process.env.PORT || 8000)

app.use(express.static('web'));

app.get('/', function (req, res) {
  logger.info('http.index')
  res.json({
    msg: 'Hello from DevToolsRemote'
  })
})

app.get('/_stats', function (req, res) {
  logger.info('http.stats')
  res.json({
    count: {
      targets: Object.keys(targets).length,
      sockets: Object.keys(sockets).length,
      sessions: Object.keys(sessions).length
    }
  })
})

app.get('/:session/json', function (req, res) {

  var sessionId = req.params.session
  var sessionTargets = targets[sessionId]

  logger.info('http.targets', {
    targets: sessionTargets
  })

  res.send(sessionTargets)
})

var server = http.Server(app)

server.listen(app.get('port'), function () {
  logger.info('http.listening')
  logger.info('- listening on port %d in %s mode', app.get('port'), app.settings.env)
})

// Socket IO for Chrome Extension
logger.info('socket.booting')

var io = require('socket.io')(server)
io.sockets.on('connection', function (socket) {
  var sessionId = uuid()

  logger.info('socket.connection', sessionId)

  targets[sessionId] = []
  sockets[sessionId] = socket
  sessions[sessionId] = {
    startTime: new Date.getTime()
  }

  socket.on('disconnect', function () {
    logger.info('socket.disconnect')

    mixpanel.track("sessions_duration", {
      sessionId: sessionId
    })

    delete targets[sessionId]
    delete sockets[sessionId]
    delete sessions[sessionId]
  })

  socket.on('error', function (err) {
    logger.error('socket.error', err)
  })

  socket.on('hello', function (data) {
    logger.info('socket.hello', data)

    var webSocketUrl = (process.env.WEBSOCKET_DOMAIN ? process.env.WEBSOCKET_DOMAIN : ('localhost:' + app.get('port'))) + '/devtools/page/' + sessionId

    targets[sessionId].push({
      description: '',
      devtoolsFrontendUrl: '/devtools/devtools.html?ws=' + webSocketUrl,
      devtoolsUrl: 'chrome-devtools://devtools/remote/serve_rev/@06a2e65a4f3610ec17dbc5988c0b16a95825240a/inspector.html?ws=' + webSocketUrl + '&remoteFrontend=true&dockSide=unlocked',
      id: uuid(),
      title: data.title,
      type: 'page',
      url: data.url,
      webSocketDebuggerUrl: 'ws://' + webSocketUrl
    })

    mixpanel.time_event("sessions_duration");
    mixpanel.track("sessions_created", {
      sessionId: sessionId
    });

    socket.emit('sessionCreated', sessionId)

  })
})
{'fraction_non_alphanumeric': 0.16294603621023027, 'fraction_numerical': 0.01089822464404992, 'mean_word_length': 3.9737762237762237, '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': '6756080', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1878, 'n_words': 356}