<commit_before><!doctype html>
<!--
Copyright 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/ui/analysis/analysis_sub_view.html">

<polymer-element name="tr-ui-a-sampling-panel" extends="tr-ui-a-sub-view">
  <template>
    <style>
    :host { display: block; }
    </style>

    <tr-ui-b-table id="table">
    </tr-ui-b-table>
  </template>

  <script>
  'use strict';

  (function() {
    var SamplingTreeNode = function(frame) {
      this.parent = undefined;
      this.children = [];
      this.childFrameMap = {};
      this.frame = frame;
      this.accumulatedSamplesCount = 0;
    };

    SamplingTreeNode.prototype = {
      getOrCreateChild: function(frame) {
        if (this.childFrameMap[frame.title] !== undefined)
          return this.childFrameMap[frame.title];

        var node = new SamplingTreeNode(frame);
        node.parent = this;

        this.childFrameMap[frame.title] = node;
        this.children.push(node);

        return node;
      },

      get subRows() {
        return this.children;
      },

      get samplesCount() {
        return this.accumulatedSamplesCount;
      },

      get name() {
        return this.frame.title;
      },

      get category() {
        return this.frame !== undefined ? this.frame.category : 'root';
      }
    };

    Polymer({
      ready: function() {
        this.selection_ = undefined;
      },

      get selection() {
        return this.selection_;
      },

      set selection(selection) {
        this.selection_ = selection;
        this.samplingData_ = this.createSamplingSummary_(selection);
        this.updateContents_();
      },

      createSamplingSummary_: function(selection) {
        var root = new SamplingTreeNode();
        var samples = selection.getEventsOrganizedByBaseType().sample;

        function addSample(sample) {
          var stack = sample.stackTrace;

          // Sort the stacks reverse so we can see the stacks in descending
          // order.
          stack.reverse();

          var currentNode = root;
          for (var i = 0; i < stack.length; i++) {
            var frame = stack[i];
            currentNode.accumulatedSamplesCount++;
            currentNode = currentNode.getOrCreateChild(frame);
          }
          currentNode.accumulatedSamplesCount++;
        }

        for (var i = 0; i < samples.length; i++) {
          addSample(samples[i]);
        }

        return root;
      },

      updateContents_: function() {
        var columns = [
        {
          title: 'Percent',
          value: function(row) {
            var percent = row.samplesCount / this.samplingData_.samplesCount;

            var span = document.createElement('tr-ui-u-scalar-span');
            span.value = (percent * 100).toFixed(2);
            span.percentage = percent;
            span.unit = tr.b.u.Units.unitlessNumber;
            return span;

          }.bind(this),
          width: '60px',
          cmp: function(a, b) {
            return a.samplesCount - b.samplesCount;
          }
        },
        {
          title: 'Samples',
          value: function(row) { return row.samplesCount; },
          width: '60px',
          cmp: function(a, b) {
            return a.samplesCount - b.samplesCount;
          }
        },
        {
          title: 'Symbol',
          value: function(row) { return row.name; },
          width: '250px',
          cmp: function(a, b) {
            return a.name.toString().localeCompare(b.name.toString());
          },
          showExpandButtons: true
        }];

        this.$.table.tableColumns = columns;
        this.$.table.sortColumnIndex = 1;
        this.$.table.sortDescending = true;
        this.$.table.tableRows = this.samplingData_.subRows;
        this.$.table.rebuild();
      }
    });
  })();
  </script>
</polymer-element>

<commit_msg>Fixes a bug in sampling summary<commit_after><!doctype html>
<!--
Copyright 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/ui/analysis/analysis_sub_view.html">

<polymer-element name="tr-ui-a-sampling-panel" extends="tr-ui-a-sub-view">
  <template>
    <style>
    :host { display: block; }
    </style>

    <tr-ui-b-table id="table">
    </tr-ui-b-table>
  </template>

  <script>
  'use strict';

  (function() {
    var SamplingTreeNode = function(frame) {
      this.parent = undefined;
      this.children = [];
      this.childFrameMap = new Map();
      this.frame = frame;
      this.accumulatedSamplesCount = 0;
    };

    SamplingTreeNode.prototype = {
      getOrCreateChild: function(frame) {
        if (this.childFrameMap.has(frame.title))
          return this.childFrameMap.get(frame.title);

        var node = new SamplingTreeNode(frame);
        node.parent = this;

        this.childFrameMap.set(frame.title, node);
        this.children.push(node);

        return node;
      },

      get subRows() {
        return this.children;
      },

      get samplesCount() {
        return this.accumulatedSamplesCount;
      },

      get name() {
        return this.frame.title;
      },

      get category() {
        return this.frame !== undefined ? this.frame.category : 'root';
      }
    };

    Polymer({
      ready: function() {
        this.selection_ = undefined;
      },

      get selection() {
        return this.selection_;
      },

      set selection(selection) {
        this.selection_ = selection;
        this.samplingData_ = this.createSamplingSummary_(selection);
        this.updateContents_();
      },

      createSamplingSummary_: function(selection) {
        var root = new SamplingTreeNode();
        var samples = selection.getEventsOrganizedByBaseType().sample;

        function addSample(sample) {
          var stack = sample.stackTrace;

          // Sort the stacks reverse so we can see the stacks in descending
          // order.
          stack.reverse();

          var currentNode = root;
          for (var i = 0; i < stack.length; i++) {
            var frame = stack[i];
            currentNode.accumulatedSamplesCount++;
            currentNode = currentNode.getOrCreateChild(frame);
          }
          currentNode.accumulatedSamplesCount++;
        }

        for (var i = 0; i < samples.length; i++) {
          addSample(samples[i]);
        }

        return root;
      },

      updateContents_: function() {
        var columns = [
        {
          title: 'Percent',
          value: function(row) {
            var percent = row.samplesCount / this.samplingData_.samplesCount;

            var span = document.createElement('tr-ui-u-scalar-span');
            span.value = (percent * 100).toFixed(2);
            span.percentage = percent;
            span.unit = tr.b.u.Units.unitlessNumber;
            return span;

          }.bind(this),
          width: '60px',
          cmp: function(a, b) {
            return a.samplesCount - b.samplesCount;
          }
        },
        {
          title: 'Samples',
          value: function(row) { return row.samplesCount; },
          width: '60px',
          cmp: function(a, b) {
            return a.samplesCount - b.samplesCount;
          }
        },
        {
          title: 'Symbol',
          value: function(row) { return row.name; },
          width: '250px',
          cmp: function(a, b) {
            return a.name.toString().localeCompare(b.name.toString());
          },
          showExpandButtons: true
        }];

        this.$.table.tableColumns = columns;
        this.$.table.sortColumnIndex = 1;
        this.$.table.sortDescending = true;
        this.$.table.tableRows = this.samplingData_.subRows;
        this.$.table.rebuild();
      }
    });
  })();
  </script>
</polymer-element>


{'fraction_non_alphanumeric': 0.12484300427028384, 'fraction_numerical': 0.0047726701833710125, 'mean_word_length': 1.8633585041352032, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 29, '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': '5827491', 'n_tokens_mistral': 2355, 'n_tokens_neox': 2225, 'n_words': 490}