<commit_before>var QuerySchemaModel = require('../../../data/query-schema-model');
var _ = require('underscore');

function TableStats (opts) {
  if (opts.user && opts.api_key) {
    this.user = opts.username;
    this.api_key = opts.api_key;
  } else if (opts.configModel) {
    this.configModel = opts.configModel;
  }
  this.tables = {};
  this.queue = {};
}

TableStats.prototype = {

  graphFor: function (tableName, column, callback) {
    this._getPgStats(tableName, function (stats) {
      var graph = new ColumnGraph(stats[column]);
      callback(graph);
    });
  },

  _getPgStats: function (table, callback) {
    var userModel = window.userModel;
    var self = this;
    if (this.tables[table]) {
      if (!_.isEmpty(this.tables[table])) {
        callback(this.tables[table]);
      } else {
        this.queue[table].push(callback);
      }
    } else {
      this.tables[table] = {};
      this.queue[table] = [callback];
      var schemaModel = new QuerySchemaModel(null, {
        configModel: this.configModel
      });
      var schema = userModel.isInsideOrg() ? userModel.username : 'public';
      schemaModel.set('query', 'with a as (select reltuples from pg_class where relname = \'' + table + '\'), b as (select * from pg_stats where tablename = \'' + table + '\' and schemaname = \'' + schema + '\') select * from a,b');
      schemaModel.on('change:status', function (model, status) {
        if (status === 'fetched') {
          var rows = schemaModel.rowsSampleCollection.map(function (m) { return m.attributes; }).map(function (r) {
            var count = r.reltuples;
            var bounds = self._reformatToJSON(r.histogram_bounds);
            if (bounds) {
              var avgs = bounds.reduce(function (p, c, i) {
                if (i < bounds.length - 1) {
                  p.push((bounds[i + 1] + c) / 2);
                }
                return p;<commit_msg>gets schemaname from user model<commit_after>var QuerySchemaModel = require('../../../data/query-schema-model');
var _ = require('underscore');

function TableStats (opts) {
  if (opts.user && opts.api_key) {
    this.user = opts.username;
    this.api_key = opts.api_key;
  } else if (opts.configModel) {
    this.configModel = opts.configModel;
  }
  this.tables = {};
  this.queue = {};
}

TableStats.prototype = {

  graphFor: function (tableName, column, callback) {
    this._getPgStats(tableName, function (stats) {
      var graph = new ColumnGraph(stats[column]);
      callback(graph);
    });
  },

  _getPgStats: function (table, callback) {
    var userModel = window.userModel;
    var self = this;
    if (this.tables[table]) {
      if (!_.isEmpty(this.tables[table])) {
        callback(this.tables[table]);
      } else {
        this.queue[table].push(callback);
      }
    } else {
      this.tables[table] = {};
      this.queue[table] = [callback];
      var schemaModel = new QuerySchemaModel(null, {
        configModel: this.configModel
      });
      var schema = userModel.getSchemaName();
      schemaModel.set('query', 'with a as (select reltuples from pg_class where relname = \'' + table + '\'), b as (select * from pg_stats where tablename = \'' + table + '\' and schemaname = \'' + schema + '\') select * from a,b');
      schemaModel.on('change:status', function (model, status) {
        if (status === 'fetched') {
          var rows = schemaModel.rowsSampleCollection.map(function (m) { return m.attributes; }).map(function (r) {
            var count = r.reltuples;
            var bounds = self._reformatToJSON(r.histogram_bounds);
            if (bounds) {
              var avgs = bounds.reduce(function (p, c, i) {
                if (i < bounds.length - 1) {
                  p.push((bounds[i + 1] + c) / 2);
                }
                return p;
{'fraction_non_alphanumeric': 0.15047268907563024, 'fraction_numerical': 0.0015756302521008404, 'mean_word_length': 2.45018115942029, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5219713', 'n_tokens_mistral': 1186, 'n_tokens_neox': 1160, 'n_words': 265}