<commit_before>var Q = require('q');
var uuid = require('node-uuid');

var Secrets = require('../db/configdb.js').Secrets;

exports = module.exports = {
  today: '',
  yesterday: ''
}

var findSecrets = Q.nbind(Secrets.find, Secrets);
var createSecret = Q.nbind(Secrets.create, Secrets);

// Return a Date object for today's date, set at midnight.
Date.prototype.toMidnight = function() {
  this.setHours(0, 0, 0, 0);
  return this;
}

function dateForToday() {
  return new Date().toMidnight();
};

function dateForYesterday() {
  var date = new Date().toMidnight();
  date.setDate(date.getDate() - 1);
  return date;
};

function setSecret() {
  var today = dateForToday();
  var yesterday = dateForYesterday();

  findSecrets({$or: [{ date: today }, {date: yesterday} ]})
    .then(function(secrets) {
      if( secrets[0].date.getTime() !== today.getTime() ) {
        createSecret({
          secret: uuid.v4(),
          date: today
        })
          .then(setSecret);
      }
      else {
        exports.today = secrets[0].secret;
        if( secrets[1].date.getTime() === yesterday.getTime() ) {
          exports.yesterday = secrets[1].secret;
        } else {
          createSecret({
            secret: uuid.v4(),
            date: yesterday
          })
            .then(setSecret);
        }
      } 
    })
      
      
    .fail(function(err) {
      console.log(err);
    });
}

function setSecretDaily() {
  var msInOneDay = 86400000;
  var msUntilMidnight = msInOneDay - ( new Date() - dateForToday() );
  setTimeout(function() {
    settingSecretDaily = setInterval(setSecret, 86400000);
  }, msUntilMidnight);
}

setSecret();
setSecretDaily();
<commit_msg>Fixed bug with secrets controller when server has not run in a couple days<commit_after>var Q = require('q');
var uuid = require('node-uuid');

var Secrets = require('../db/configdb.js').Secrets;

exports = module.exports = {
  today: '',
  yesterday: ''
}

var findSecrets = Q.nbind(Secrets.find, Secrets);
var createSecret = Q.nbind(Secrets.create, Secrets);

// Return a Date object for today's date, set at midnight.
Date.prototype.toMidnight = function() {
  this.setHours(0, 0, 0, 0);
  return this;
}

function dateForToday() {
  return new Date().toMidnight();
};

function dateForYesterday() {
  var date = new Date().toMidnight();
  date.setDate(date.getDate() - 1);
  return date;
};

function setSecret() {
  var today = dateForToday();
  var yesterday = dateForYesterday();

  findSecrets({$or: [{ date: today }, {date: yesterday} ]})
    .then(function(secrets) {
      if( secrets[0] && secrets[0].date.getTime() !== today.getTime() ) {
        createSecret({
          secret: uuid.v4(),
          date: today
        })
          .then(setSecret);
      }
      else {
        exports.today = secrets[0].secret;

        if( secrets[1] && secrets[1].date.getTime() === yesterday.getTime() ) {
          exports.yesterday = secrets[1].secret;
        } else {
          createSecret({
            secret: uuid.v4(),
            date: yesterday
          })
            .then(setSecret);
        }
      } 
    })
      
      
    .fail(function(err) {
      console.log(err);
    });
}

function setSecretDaily() {
  var msInOneDay = 86400000;
  var msUntilMidnight = msInOneDay - ( new Date() - dateForToday() );
  setTimeout(function() {
    settingSecretDaily = setInterval(setSecret, 86400000);
  }, msUntilMidnight);
}

setSecret();
setSecretDaily();

{'fraction_non_alphanumeric': 0.1545138888888889, 'fraction_numerical': 0.016203703703703703, 'mean_word_length': 2.6659597030752917, 'pattern_counts': {'":': 0, '<': 3, '<?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': '230355', 'n_tokens_mistral': 1165, 'n_tokens_neox': 1107, 'n_words': 239}