/**
 * 每天运行一次，每次放开100条记录
 */

const models = require('../../models');

const openCount = 100; // 每天开放的内容量

async function run() {
  let done = 0;
  let duration = 0;
  let hour, minute, second;
  let start = Date.now();

  let docs = await models.Art.find({ open: false }).sort({ lastMod: 1 }).limit(openCount);

  let total = docs.length;
  console.log('total:', total);

  for (let doc of docs) {
    doc.open = true;
    await doc.save();

    done++;
    duration = (Date.now() - start) / 1000;
    hour = (Math.floor(duration / 60 / 60)).toString().padStart(2, '0');
    minute = (Math.floor(duration / 60) % 60).toString().padStart(2, '0');
    second = (Math.floor(duration) % 60).toString().padStart(2, '0');

    console.log('progress: ' + Math.floor((100 * done / total)) + '% used time: ' + hour + ':' + minute + ':' + second);

  }
}

module.exports = { run }


if (require.main == module) {
  run();
}