<commit_before>(function( document, window, $ ) {

  var ButtonController = function( timeout ) {
    this.timeout = timeout; // how often to update the ui

    this.autoUpdateThread = null; // store the setTimeout
  }

  // ensure that a button's class is correct
  // based on its action
  ButtonController.prototype.updateButtonContent = function( element ) {
    $.ajax({
      type: 'GET',
      url: '/api/' + $(element).data('route') + '/',
      success: function( data ) {
        if ( data === 'true' ) {
          // it's running
          // class: btn-success
          // action: stop
          $(element)
            .removeClass('btn-danger')
            .addClass('btn-success')
            .data('action', 'stop');
        } else {
          // it's not running
          // class: btn-success
          // action: stop
          $(element)
            .removeClass('btn-success')
            .addClass('btn-danger')
            .data('action', 'start');
        }
      }
    });
  }

  ButtonController.prototype.updateAllButtons = function() {
    var buttons = $('.service-button');

    // update each individual button
    buttons.each(function(i, button) {
      this.updateButtonContent( button );
    }.bind(this));

    return false; // prevent event bubbling
  }

  // configure each button so when clicked, it'll perform the appropriate action
  ButtonController.prototype.attachActionToButtons = function( buttons ) {
    var bc = this;

    buttons.click(function() {
      var route = $(this).data('route'),
          action = $(this).data('action'),
          element = $(this);

      $.ajax({
        type: 'POST',
        url: [ '/api', route, action ].join('/'),
        data: '',
        success: function() {
          // zap the next UI update and force timer to start over
          clearInterval( this.autoUpdateThread );
          this.timedUpdateAllButtons();
        }.bind(bc),

        error: function() {
          alert('ERROR');
        }
      });

      return false;
    });
  }

  ButtonController.prototype.timedUpdateAllButtons = function() {
    this.updateAllButtons();

    this.autoUpdateThread = setTimeout( this.timedUpdateAllButtons.bind(this), this.timeout );
  }

  $(function() {
    var bc = new ButtonController( 5000 );

    bc.updateAllButtons();
    bc.attachActionToButtons( $('.service-button') );
    bc.timedUpdateAllButtons();

    $('#refresh-button').click(bc.updateAllButtons.bind(bc));
  });

})( document, window, jQuery );
<commit_msg>a little JS refactoring to make it nicer.<commit_after>(function( document, window, $ ) {

  var ButtonController = function( timeout ) {
    this.timeout = timeout; // how often to update the ui

    this.autoUpdateThread = null; // store the setTimeout
  }

  // ensure that a button's class is correct
  // based on its action (make it red if it's gonna start it, green to stop)
  ButtonController.prototype.updateButtonContent = function( element ) {
    $.ajax({
      type: 'GET',
      url: '/api/' + $(element).data('route') + '/',
      success: function( data ) {
        if ( data === 'true' ) {
          // it's running
          // class: btn-success
          // action: stop
          $(element)
            .removeClass('btn-danger')
            .addClass('btn-success')
            .data('action', 'stop');
        } else {
          // it's not running
          // class: btn-success
          // action: stop
          $(element)
            .removeClass('btn-success')
            .addClass('btn-danger')
            .data('action', 'start');
        }
      }
    });
  }

  // updates the state of all buttons
  ButtonController.prototype.updateAllButtons = function() {
    var buttons = $('.service-button');

    // update each individual button
    buttons.each(function(i, button) {
      this.updateButtonContent( button );
    }.bind(this));
  }

  // configure each button so when clicked, it'll perform the appropriate action
  ButtonController.prototype.attachActionToButtons = function( buttons ) {
    var bc = this;

    buttons.click(function() {
      var route = $(this).data('route'),
          action = $(this).data('action'),
          element = $(this);

      $.ajax({
        type: 'POST',
        url: [ '/api', route, action ].join('/'),
        data: '',

        success: function() {
          // zap the next UI update and force timer to start over
          clearInterval( this.autoUpdateThread );
          this.timedUpdateAllButtons();
        }.bind(bc),

        error: function() {
          alert('ERROR');
        }
      });

      return false;
    });
  }

  ButtonController.prototype.timedUpdateAllButtons = function() {
    this.updateAllButtons();

    this.autoUpdateThread = setTimeout( this.timedUpdateAllButtons.bind(this), this.timeout );
  }

  $(function() {
    var bc = new ButtonController( 5000 );

    bc.updateAllButtons();
    bc.attachActionToButtons( $('.service-button') );
    bc.timedUpdateAllButtons();

    $('#refresh-button').click( function() {
      bc.updateAllButtons.bind(bc)
      return false;
    });
  });

})( document, window, jQuery );

{'fraction_non_alphanumeric': 0.1448797517455392, 'fraction_numerical': 0.0015515903801396431, 'mean_word_length': 2.1618638871857754, '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': '5853402', 'n_tokens_mistral': 1437, 'n_tokens_neox': 1404, 'n_words': 356}