<commit_before>
(function ($) {

OpenSpending.AggregateTable = function (elem, context, state) {
  var self = this;

  var resources = [OpenSpending.scriptRoot + "/lib/vendor/underscore.js",
                 OpenSpending.scriptRoot + "/lib/aggregator.js",
                 OpenSpending.scriptRoot + "/lib/utils/utils.js",
                 OpenSpending.scriptRoot + "/lib/vendor/datatables/js/jquery.dataTables.js",
                 OpenSpending.scriptRoot + "/lib/vendor/datatables/css/jquery.dataTables.css",
                 OpenSpending.scriptRoot + "/app/data_table/openspending.data_table.js"
                 ];

  this.context = context;
  this.state = state;

  this.configure = function(endConfigure) {
    self.$qb.empty();
    var qb = new OpenSpending.Widgets.QueryBuilder(
      self.$qb, self.update, endConfigure, self.context, [
            {
              variable: 'drilldowns',
              label: 'Columns:',
              type: 'select',
              'default': self.state.drilldowns,
              help: 'Each selected dimension will display as an additional level of bubbles.'
            },
            {
              variable: 'cuts',
              label: 'Filters:',
              type: 'cuts',
              'default': self.state.cuts,
              help: 'Limit the set of data to display.'
            }
          ]
    );
  };

  this.serialize = function() { return self.state; };

  this.update = function(state) {
    //console.log(state);
    self.$e.empty();
    self.state = state;

    var cuts = [];
    for (var field in self.state.cuts) {
      cuts.push(field + ':' + self.state.cuts[field]);
    }
    if (cuts.length) {
      cuts = cuts.join('|');
    }

    var drilldowns = self.state.drilldowns || [];
    var columns = _.map(drilldowns, function(d) {
      return {name: d, label: d};
    });
    drilldowns = drilldowns.join('|');
    columns.push({
          'name': 'amount',
          'label': 'Amount'
        });
    console.log(columns);

    self.dataTable = new OpenSpending.DataTable(self.$e, {
      source: context.siteUrl + '/api/2/aggregate',
      fullCount: function(data) {return data.summary.num_drilldowns;},
      resultCollection: 'drilldown',
      sorting: [],
      tableOptions: {
        bFilter: false
      },
      defaultParams: {
        dataset: context.dataset,
        drilldown: drilldowns,
        cut: cuts
      },
      columns: columns
    });

    self.dataTable.init();
    
  };
  
  this.init = function() {
    self.$e = elem;
    self.$e.before('<div class="table-qb"></div>');
    self.$qb = elem.prev();
    self.$e.addClass("table-widget");
    this.update(self.state);
    
};

  // The rest of this function is suitable for copypasta into other<commit_msg>Load the data model to fill dimension names; integrate further with query builder. <commit_after>
(function ($) {

OpenSpending.AggregateTable = function (elem, context, state) {
  var self = this;

  var resources = [OpenSpending.scriptRoot + "/lib/vendor/underscore.js",
                 OpenSpending.scriptRoot + "/lib/aggregator.js",
                 OpenSpending.scriptRoot + "/lib/utils/utils.js",
                 OpenSpending.scriptRoot + "/lib/vendor/datatables/js/jquery.dataTables.js",
                 OpenSpending.scriptRoot + "/lib/vendor/datatables/css/jquery.dataTables.css",
                 OpenSpending.scriptRoot + "/app/data_table/openspending.data_table.js"
                 ];

  this.context = context;
  this.state = state;

  this.configure = function(endConfigure) {
    self.$qb.empty();
    var qb = new OpenSpending.Widgets.QueryBuilder(
      self.$qb, self.update, endConfigure, self.context, [
            {
              variable: 'drilldowns',
              label: 'Columns:',
              type: 'select',
              'default': self.state.drilldowns,
              help: 'Each selected dimension will display as a column, and values will be drilled down by it.'
            },
            {
              variable: 'cuts',
              label: 'Filters:',
              type: 'cuts',
              'default': self.state.cuts,
              help: 'Limit the set of data to display.'
            }
          ]
    );
  };

  this.serialize = function() {
    return self.state;
  };

  this.update = function(state) {
    //console.log(state);
    self.$e.empty();
    self.state = state;

    var cuts = [];
    for (var field in self.state.cuts) {
      cuts.push(field + ':' + self.state.cuts[field]);
    }
    if (cuts.length) {
      cuts = cuts.join('|');
    }

    var drilldowns = self.state.drilldowns || [];
    var columns = _.map(drilldowns, function(d) {
      return {name: d, label: self.mapping[d].label};
    });
    drilldowns = drilldowns.join('|');
    columns.push({
          'name': 'amount',
          'label': self.mapping['amount'].label,
          'render': function(coll, obj) {
            return OpenSpending.Utils.formatAmountWithCommas(obj || 0);
          }
        });

    self.dataTable = new OpenSpending.DataTable(self.$e, {
      source: context.siteUrl + '/api/2/aggregate',
      fullCount: function(data) {return data.summary.num_drilldowns;},
      resultCollection: 'drilldown',
      sorting: [],
      tableOptions: {
        bFilter: false
      },
      defaultParams: {
        dataset: context.dataset,
        drilldown: drilldowns,
        cut: cuts
      },
      columns: columns
    });
    self.dataTable.init();
    
  };
  
  this.init = function() {
    self.$e = elem;
    self.$e.empty();
    self.$e.before('<div class="table-qb"></div>');
    self.$qb = elem.prev();
    self.$e.addClass("table-widget");

    $.get(context.siteUrl + '/' + context.dataset + '/model.json', function(data) {
      self.mapping = data.mapping;
      self.update(self.state);
    });
};

  // The rest of this function is suitable for copypasta into other
{'fraction_non_alphanumeric': 0.1433418150975403, 'fraction_numerical': 0.0005089058524173028, 'mean_word_length': 2.042311661506708, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7198268', 'n_tokens_mistral': 1831, 'n_tokens_neox': 1798, 'n_words': 359}