<commit_before>var subs = (function() {

  // What server shall we connect to by default?
  var server;

  // Subscription ID counter.
  var id_counter = -1;

  // Subscriptions
  var subs = {};
  
  // Switch to turn on/off event processing
  var active = true;
 
  // Error queue for notification
  var errorQueue = [];

  // Instrumentation
  var load1 = profile.load(1000);
  var load5 = profile.load(5000);

  // Get a new subscription ID.
  var newId = function() {
    return id_counter += 1;
  }

  // Close a subscription's websocket channel.
  var close = function(sub) {
    return sub.close();
  }

  // Closes a subscription and deletes it from the subscription manager.
  var unsubscribe = function(sub) {
    delete subs[sub.id];
    return sub.close();
  }

  // Unsubscribe from all subscriptions.
  var unsubscribeAll = function() {
    _.each(subs, unsubscribe);
  }

  // Open a subscription's websocket channel.
  var open = function(sub) {
    return sub.open();
  }

  var Subscription = Backbone.Model.extend({

    initialize: function(id, query, f) {
      this.id = id;
      this.query = query;
      this.f = f;
    },

    isOpen: function() {
      return this.ws && (this.ws.readyState != WebSocket.CLOSED)
    },
    isClosed: function() { return !this.isOpen() },

    url: function() {
      var queryString = "query=" + encodeURI(this.query);
      return "ws://" + server + "/index?subscribe=true&" + queryString;
    },

    open: function() {
      if (this.isOpen()) return this;
      var ws = this.ws = new WebSocket(this.url());

      ws.onopen = _.bind(function() {
        console.log("Socket opened", this.query);
      }, this);

      ws.onclose = _.bind(function(e) {
        console.log("Socket closed", this.query);
        this.ws = null;
      }, this);

      ws.onerror = _.bind(function(e) {
        console.log("Socket error", this.query);
        errorQueue.push(e);
        this.close();
      }, this);

      ws.onmessage = _.bind(function(e) {
        t1 = Date.now();
        if (active) {
          this.f(JSON.parse(e.data));
        }
        load1(t1, Date.now());
        load5(t1, Date.now());
      }, this);

      return this;

    },

    close: function() {
      if (this.ws) {
        this.ws.close();
        this.ws = void 0;
      }
      return this;
    }
  });

  // Add a subscription. Returns a subscription object. Subscriptions are
  // opened immediately.
  var subscribe = function(query, f) {
    var sub = new Subscription(newId(), query, f).open();
    subs[sub.id] = sub;
    return sub;
  }

  // Reconnect all inactive subs.
  var converge = function() {
    var closed = _.filter(subs, function(sub) {
      return sub.isClosed();
    });
    if (_.isEmpty(closed)) {
      // Done here.
      return;
    }
  
    // Display reconnection notice
    toastr.warning(_.size(closed) + " lost connections");

    // Reopen
    _.each(closed, function(sub) {<commit_msg>Lost connections message tells you to check the server field.<commit_after>var subs = (function() {

  // What server shall we connect to by default?
  var server;

  // Subscription ID counter.
  var id_counter = -1;

  // Subscriptions
  var subs = {};
  
  // Switch to turn on/off event processing
  var active = true;
 
  // Error queue for notification
  var errorQueue = [];

  // Instrumentation
  var load1 = profile.load(1000);
  var load5 = profile.load(5000);

  // Get a new subscription ID.
  var newId = function() {
    return id_counter += 1;
  }

  // Close a subscription's websocket channel.
  var close = function(sub) {
    return sub.close();
  }

  // Closes a subscription and deletes it from the subscription manager.
  var unsubscribe = function(sub) {
    delete subs[sub.id];
    return sub.close();
  }

  // Unsubscribe from all subscriptions.
  var unsubscribeAll = function() {
    _.each(subs, unsubscribe);
  }

  // Open a subscription's websocket channel.
  var open = function(sub) {
    return sub.open();
  }

  var Subscription = Backbone.Model.extend({

    initialize: function(id, query, f) {
      this.id = id;
      this.query = query;
      this.f = f;
    },

    isOpen: function() {
      return this.ws && (this.ws.readyState != WebSocket.CLOSED)
    },
    isClosed: function() { return !this.isOpen() },

    url: function() {
      var queryString = "query=" + encodeURI(this.query);
      return "ws://" + server + "/index?subscribe=true&" + queryString;
    },

    open: function() {
      if (this.isOpen()) return this;
      var ws = this.ws = new WebSocket(this.url());

      ws.onopen = _.bind(function() {
        console.log("Socket opened", this.query);
      }, this);

      ws.onclose = _.bind(function(e) {
        console.log("Socket closed", this.query);
        this.ws = null;
      }, this);

      ws.onerror = _.bind(function(e) {
        console.log("Socket error", this.query);
        errorQueue.push(e);
        this.close();
      }, this);

      ws.onmessage = _.bind(function(e) {
        t1 = Date.now();
        if (active) {
          this.f(JSON.parse(e.data));
        }
        load1(t1, Date.now());
        load5(t1, Date.now());
      }, this);

      return this;

    },

    close: function() {
      if (this.ws) {
        this.ws.close();
        this.ws = void 0;
      }
      return this;
    }
  });

  // Add a subscription. Returns a subscription object. Subscriptions are
  // opened immediately.
  var subscribe = function(query, f) {
    var sub = new Subscription(newId(), query, f).open();
    subs[sub.id] = sub;
    return sub;
  }

  // Reconnect all inactive subs.
  var converge = function() {
    var closed = _.filter(subs, function(sub) {
      return sub.isClosed();
    });
    if (_.isEmpty(closed)) {
      // Done here.
      return;
    }
  
    // Display reconnection notice
    toastr.warning(_.size(closed) + " lost connections&mdash;check the server field above.");

    // Reopen
    _.each(closed, function(sub) {
{'fraction_non_alphanumeric': 0.14285714285714285, 'fraction_numerical': 0.005994005994005994, 'mean_word_length': 2.236530172413793, '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': '3286925', 'n_tokens_mistral': 1944, 'n_tokens_neox': 1852, 'n_words': 501}