<commit_before>$(document).ready(function () {
  if ($.isController('statistics') && $('.graph#user-activity').isPresent()) {
    var CHART_START = window.vis ? vis.moment().add(-1, 'minute') : undefined;
    var DEFAULT_REFRESH_INTERVAL = 10000;

    var refreshInterval;

    var initialData;
    var dataset;
    var graph;
    var groups;

    var buildChartGroups = function() {
      initialData = initialData.sort(function (a, b) {return a.data - b.data});
      return _.map(initialData, function(element, index) {
        return {
          content: element.name + (element.unit ? ' [' + element.unit + ']' : ''),
          id: element.key,
          visible: false,
          options: {
            yAxisOrientation: index >= initialData.length / 2 ? 'right' : 'left'
          }
        };
      });
    };

    var initializeChart = function() {
      dataset = new vis.DataSet();
      groups = new vis.DataSet(buildChartGroups());
      graph = new vis.Graph2d(document.getElementById('user-activity'), dataset, groups, {
        dataAxis: {
          customRange: {
            left: {
              min: 0
            },
            right: {
              min: 0
            }
          },
          showMinorLabels: true
        },
        drawPoints: {
          style: 'circle'
        },
        end: vis.moment(),
        legend: true,
        shaded: true,
        start: CHART_START
      });
    };

    var refreshChart = function() {
      var now = vis.moment();
      var window = graph.getWindow();
      var interval = window.end - window.start;
      graph.setWindow(now - interval, now);
    };

    var refreshData = function(callback) {
      if (! ($.isController('statistics') && $('#user-activity').isPresent())) {
        clearInterval(refreshInterval);
      } else {
        var jqxhr = $.ajax('graphs/user-activity', {
          dataType: 'json',
          method: 'GET'
        });
        jqxhr.done(function(response) {
          (callback || _.noop)(response);
          setGroupVisibility(response);
          updateChartData(response);
          requestAnimationFrame(refreshChart);
        });
      }
    };

    var setGroupVisibility = function(response) {
      _.each(response, function(data) {
        groups.update({
          id: data.key,
          visible: true
        });
      });
    };

    var updateChartData = function(response) {
      _.each(response, function(data) {
        dataset.add({
          group: data.key,
          x: vis.moment(),
          y: data.data
        });
      });
    };

    refreshData(function (data) {
      initialData = data;
      $('#user-activity').parent().find('.spinner').hide();
      initializeChart();

      var refresh_interval = location.search.match(/interval=(\d+)/) ? parseInt(RegExp.$1) : DEFAULT_REFRESH_INTERVAL;
      refreshInterval = setInterval(refreshData, refresh_interval);
    });
  }
});<commit_msg>Enable multiple graphs to be created<commit_after>$(document).ready(function () {
  if ($.isController('statistics') && $('.graph#user-activity').isPresent()) {

    function manageGraph(containerId, url, refreshAfter) {
      var CHART_START = window.vis ? vis.moment().add(-1, 'minute') : undefined;
      var DEFAULT_REFRESH_INTERVAL = refreshAfter * 1000 || 10000;

      var refreshInterval;

      var initialData;
      var dataset;
      var graph;
      var groups;

      var buildChartGroups = function() {
        initialData = initialData.sort(function (a, b) {return a.data - b.data});
        return _.map(initialData, function(element, index) {
          return {
            content: element.name + (element.unit ? ' [' + element.unit + ']' : ''),
            id: element.key,
            visible: false,
            options: {
              yAxisOrientation: index >= initialData.length / 2 ? 'right' : 'left'
            }
          };
        });
      };

      var initializeChart = function() {
        dataset = new vis.DataSet();
        groups = new vis.DataSet(buildChartGroups());
        graph = new vis.Graph2d(document.getElementById(containerId), dataset, groups, {
          dataAxis: {
            customRange: {
              left: {
                min: 0
              },
              right: {
                min: 0
              }
            },
            showMinorLabels: true
          },
          drawPoints: {
            style: 'circle'
          },
          end: vis.moment(),
          legend: true,
          shaded: true,
          start: CHART_START
        });
      };

      var refreshChart = function() {
        var now = vis.moment();
        var window = graph.getWindow();
        var interval = window.end - window.start;
        graph.setWindow(now - interval, now);
      };

      var refreshData = function(callback) {
        if (! ($.isController('statistics') && $('#' + containerId).isPresent())) {
          clearInterval(refreshInterval);
        } else {
          var jqxhr = $.ajax(url, {
            dataType: 'json',
            method: 'GET'
          });
          jqxhr.done(function(response) {
            (callback || _.noop)(response);
            setGroupVisibility(response);
            updateChartData(response);
            requestAnimationFrame(refreshChart);
          });
        }
      };

      var setGroupVisibility = function(response) {
        _.each(response, function(data) {
          groups.update({
            id: data.key,
            visible: true
          });
        });
      };

      var updateChartData = function(response) {
        _.each(response, function(data) {
          dataset.add({
            group: data.key,
            x: vis.moment(),
            y: data.data
          });
        });
      };

      refreshData(function (data) {
        initialData = data;
        $('#' + containerId).parent().find('.spinner').hide();
        initializeChart();

        var refresh_interval = location.search.match(/interval=(\d+)/) ? parseInt(RegExp.$1) : DEFAULT_REFRESH_INTERVAL;
        refreshInterval = setInterval(refreshData, refresh_interval);
      });
    }

    manageGraph('user-activity', 'graphs/user-activity', 10);
    manageGraph('rfc-activity', 'graphs/rfc-activity', 30);
  }
});
{'fraction_non_alphanumeric': 0.138328530259366, 'fraction_numerical': 0.004803073967339097, 'mean_word_length': 1.8331065759637188, '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': '6635772', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1683, 'n_words': 339}