<commit_before>/*
 * Copyright 2018 ThoughtWorks, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

(function () {
  "use strict";

  var currentNodeIndex = function currentNodeIndex(vsmGraph) {
    if (vsmGraph.current_material) return 0;

    return _.findIndex(vsmGraph.levels, function (level) {
      return _.find(level.nodes, function (node) {
        return node.id === vsmGraph.current_pipeline;
      });
    });
  };

  var Node = function Node(type, id, index) {
    this.id    = id;
    this.type  = type;
    this.index = index;
  };

  var MaterialNode = function MaterialNode(fingerprint, name, index) {
    Node.call(this, 'material', fingerprint, index);
    this.name = name;
  };

  var PipelineNode = function PipelineNode(name, index) {
    Node.call(this, 'pipeline', name, index);
    this.name = name;
  };

  var currentNode = function currentNode(vsmGraph) {
    if ($j.trim(vsmGraph.current_pipeline)) {
      return new PipelineNode(vsmGraph.current_pipeline, currentNodeIndex(vsmGraph));
    } else {
      return new MaterialNode(vsmGraph.current_material, vsmGraph.current_material, currentNodeIndex(vsmGraph));
    }
  };

  function defineHandlers(chartId) {
    PluginEndpointRequestHandler.defineLinkHandler();
    const models    = {};
    models[chartId] = {
      fetch: function (url, handler) {
        const splitURL = url.split('?');
        const search   = splitURL[1];
        const jsonData = JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}');
        $j.ajax({
          url:      splitURL[0],
          type:     "POST",
          dataType: "json",
          data:     jsonData
        }).done(function (r) {
          handler(r.data, null);
        });
      }
    };

    PluginEndpointRequestHandler.defineFetchAnalyticsHandler(models);
    PluginEndpoint.ensure("v1");
  }

  function showAnalytics(options) {
    var div = document.createElement("div");
    defineHandlers(options.vsmAnalyticsChart.id);
    $j(div).addClass("vsm_modal");
    $j("#analytics-overlay").append(div);

    $j.ajax({
      url:      options.vsmAnalyticsChart.url,
      dataType: "json",
      data:     options.data,
      type:     options.type || "GET"
    }).done(function (r) {
      var frame     = document.createElement("iframe");
      frame.sandbox = "allow-scripts";

      frame.onload = function (_e) {
        PluginEndpoint.init(frame.contentWindow, {
          uid:         options.vsmAnalyticsChart.id,
          pluginId:    options.vsmAnalyticsChart.plugin_id,
          initialData: r.data
        });
      };

      div.appendChild(frame);
      frame.setAttribute("src", "/go/" + r.view_path);
    }).fail(function (xhr) {
      if (xhr.getResponseHeader("content-type").indexOf("text/html") !== -1) {
        var frame = document.createElement("iframe");
        frame.src = "data:text/html;charset=utf-8," + xhr.responseText;
        div.appendChild(frame);
        return;
      }
      var errorEl = document.createElement("div");
      $(errorEl).addClass("error");
      errorEl.textContent = xhr.responseText;
      div.appendChild(errorEl);
    });
  };

  function VSMAnalytics(data, graphRenderer, vsmAnalyticsChart, analyticsPanel, analyticsButton) {
    var self              = this;
    var panel             = analyticsPanel;
    var analyticsButton   = analyticsButton;
    var vsmGraph          = VSMGraph.fromJSON(data);
    var current           = currentNode(vsmGraph);
    var graphRenderer     = graphRenderer;
    var vsmAnalyticsChart = vsmAnalyticsChart;

    var otherNode;

    var source = function source() {
      return current.index < otherNode.index ? current.id : otherNode.id;
    };

    var destination = function destination() {
      return otherNode.index > current.index ? otherNode.id : current.id;
    };

    var renderAnalytics = function renderAnalytics() {
      $j("#analytics-overlay").removeClass("hide");
      $j("body").addClass("noscroll");
      showAnalytics({
        vsmAnalyticsChart: vsmAnalyticsChart,
        type:              'POST',
        data:              self.jsonData()
      });
    };
<commit_msg>Write vsm_analytics code in es5<commit_after>/*
 * Copyright 2018 ThoughtWorks, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

(function () {
  "use strict";

  var currentNodeIndex = function currentNodeIndex(vsmGraph) {
    if (vsmGraph.current_material) return 0;

    return _.findIndex(vsmGraph.levels, function (level) {
      return _.find(level.nodes, function (node) {
        return node.id === vsmGraph.current_pipeline;
      });
    });
  };

  var Node = function Node(type, id, index) {
    this.id    = id;
    this.type  = type;
    this.index = index;
  };

  var MaterialNode = function MaterialNode(fingerprint, name, index) {
    Node.call(this, 'material', fingerprint, index);
    this.name = name;
  };

  var PipelineNode = function PipelineNode(name, index) {
    Node.call(this, 'pipeline', name, index);
    this.name = name;
  };

  var currentNode = function currentNode(vsmGraph) {
    if ($j.trim(vsmGraph.current_pipeline)) {
      return new PipelineNode(vsmGraph.current_pipeline, currentNodeIndex(vsmGraph));
    } else {
      return new MaterialNode(vsmGraph.current_material, vsmGraph.current_material, currentNodeIndex(vsmGraph));
    }
  };

  function defineHandlers(chartId) {
    PluginEndpointRequestHandler.defineLinkHandler();
    var models = {};
    models[chartId] = {
      fetch: function fetch(url, handler) {
        var splitURL = url.split('?');
        var search = splitURL[1];
        var jsonData = JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}');
        $j.ajax({
          url: splitURL[0],
          type: "POST",
          dataType: "json",
          data: jsonData
        }).done(function (r) {
          handler(r.data, null);
        });
      }
    };

    PluginEndpointRequestHandler.defineFetchAnalyticsHandler(models);
    PluginEndpoint.ensure("v1");
  }

  function showAnalytics(options) {
    var div = document.createElement("div");
    defineHandlers(options.vsmAnalyticsChart.id);
    $j(div).addClass("vsm_modal");
    $j("#analytics-overlay").append(div);

    $j.ajax({
      url:      options.vsmAnalyticsChart.url,
      dataType: "json",
      data:     options.data,
      type:     options.type || "GET"
    }).done(function (r) {
      var frame     = document.createElement("iframe");
      frame.sandbox = "allow-scripts";

      frame.onload = function (_e) {
        PluginEndpoint.init(frame.contentWindow, {
          uid:         options.vsmAnalyticsChart.id,
          pluginId:    options.vsmAnalyticsChart.plugin_id,
          initialData: r.data
        });
      };

      div.appendChild(frame);
      frame.setAttribute("src", "/go/" + r.view_path);
    }).fail(function (xhr) {
      if (xhr.getResponseHeader("content-type").indexOf("text/html") !== -1) {
        var frame = document.createElement("iframe");
        frame.src = "data:text/html;charset=utf-8," + xhr.responseText;
        div.appendChild(frame);
        return;
      }
      var errorEl = document.createElement("div");
      $(errorEl).addClass("error");
      errorEl.textContent = xhr.responseText;
      div.appendChild(errorEl);
    });
  };

  var VSMAnalytics = function VSMAnalytics(data, graphRenderer, vsmAnalyticsChart, analyticsPanel, analyticsButton) {
    var self              = this;
    var panel             = analyticsPanel;
    var analyticsButton   = analyticsButton;
    var vsmGraph          = VSMGraph.fromJSON(data);
    var current           = currentNode(vsmGraph);
    var graphRenderer     = graphRenderer;
    var vsmAnalyticsChart = vsmAnalyticsChart;

    var otherNode;

    var source = function source() {
      return current.index < otherNode.index ? current.id : otherNode.id;
    };

    var destination = function destination() {
      return otherNode.index > current.index ? otherNode.id : current.id;
    };

    var renderAnalytics = function renderAnalytics() {
      $j("#analytics-overlay").removeClass("hide");
      $j("body").addClass("noscroll");
      showAnalytics({
        vsmAnalyticsChart: vsmAnalyticsChart,
        type:              'POST',
        data:              self.jsonData()
      });
    };

{'fraction_non_alphanumeric': 0.12900148399406403, 'fraction_numerical': 0.003073987704049184, 'mean_word_length': 2.7381141045958794, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4671553', 'n_tokens_mistral': 2793, 'n_tokens_neox': 2733, 'n_words': 630}