<commit_before>var _ = require('lodash');
var util = require('../../core/util.js');
var config = util.getConfig();
var log = require(util.dirs().core + 'log');

var handle = require('./handle');
var postgresUtil = require('./util');

var Reader = function() {
  _.bindAll(this);
  this.db = handle;
}

// returns the furtherst point (up to `from`) in time we have valid data from
Reader.prototype.mostRecentWindow = function(from, to, next) {
  to = to.unix();
  from = from.unix();

  var maxAmount = to - from + 1;

  var query = this.db.query(`
  SELECT start from ${postgresUtil.table('candles')}
  WHERE start <= ${to} AND start >= ${from}
  ORDER BY start DESC
  `, function (err, result) {
    if (err) {
      // bail out if the table does not exist
      if (err.message.indexOf(' does not exist') !== -1)
        return next(false);

      log.error(err);
      return util.die('DB error while reading mostRecentWindow');
    }
  });

  var rows = [];
  query.on('row', function(row) {
    rows.push(row);
  });

  // After all data is returned, close connection and return results
  query.on('end', function() {

    // no candles are available
    if(rows.length === 0) {
      return next(false);
    }

    if(rows.length === maxAmount) {

      // full history is available!

      return next({
        from: from,
        to: to
      });
    }

    // we have at least one gap, figure out where
    var mostRecent = _.first(rows).start;

    var gapIndex = _.findIndex(rows, function(r, i) {
      return r.start !== mostRecent - i * 60;
    });

    // if there was no gap in the records, but
    // there were not enough records.
    if(gapIndex === -1) {
      var leastRecent = _.last(rows).start;
      return next({
        from: leastRecent,
        to: mostRecent
      });
    }

    // else return mostRecent and the
    // the minute before the gap
    return next({
      from: rows[ gapIndex - 1 ].start,
      to: mostRecent
    });

  });
}

Reader.prototype.tableExists = function(name, next) {

  const query = this.db.query(`
    SELECT COUNT(*) FROM ${postgresUtil.table(name)}
  `);

  const rows = [];

  query.on('row', (row) => {
    rows.push(row);
  });

  query.on('end', () => {
    next(null, rows.length === 1 && rows[0].count > 0);
  });
}
<commit_msg>Adds tableExist() function to query if a table exists.<commit_after>var _ = require('lodash');
var util = require('../../core/util.js');
var config = util.getConfig();
var log = require(util.dirs().core + 'log');

var handle = require('./handle');
var postgresUtil = require('./util');

var Reader = function() {
  _.bindAll(this);
  this.db = handle;
}

// returns the furtherst point (up to `from`) in time we have valid data from
Reader.prototype.mostRecentWindow = function(from, to, next) {
  to = to.unix();
  from = from.unix();

  var maxAmount = to - from + 1;

  var query = this.db.query(`
  SELECT start from ${postgresUtil.table('candles')}
  WHERE start <= ${to} AND start >= ${from}
  ORDER BY start DESC
  `, function (err, result) {
    if (err) {
      // bail out if the table does not exist
      if (err.message.indexOf(' does not exist') !== -1)
        return next(false);

      log.error(err);
      return util.die('DB error while reading mostRecentWindow');
    }
  });

  var rows = [];
  query.on('row', function(row) {
    rows.push(row);
  });

  // After all data is returned, close connection and return results
  query.on('end', function() {

    // no candles are available
    if(rows.length === 0) {
      return next(false);
    }

    if(rows.length === maxAmount) {

      // full history is available!

      return next({
        from: from,
        to: to
      });
    }

    // we have at least one gap, figure out where
    var mostRecent = _.first(rows).start;

    var gapIndex = _.findIndex(rows, function(r, i) {
      return r.start !== mostRecent - i * 60;
    });

    // if there was no gap in the records, but
    // there were not enough records.
    if(gapIndex === -1) {
      var leastRecent = _.last(rows).start;
      return next({
        from: leastRecent,
        to: mostRecent
      });
    }

    // else return mostRecent and the
    // the minute before the gap
    return next({
      from: rows[ gapIndex - 1 ].start,
      to: mostRecent
    });

  });
}

Reader.prototype.tableExists = function(name, next) {
  this.db.query(`
    SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_name='${postgresUtil.table(name)}';
  `, function(err, result) {
    if(err) {
      return util.die('DB error at `tableExists`');
    }

    next(null, result.rows.length === 1);
  });
}

{'fraction_non_alphanumeric': 0.15430647574268005, 'fraction_numerical': 0.0038469758495405003, 'mean_word_length': 2.5135135135135136, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '942294', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1451, 'n_words': 450}