const db = require('../db');
const xlsx = require('node-xlsx').default;
const fs = require('fs');
const _ = require('lodash');
const moment = require('moment');
 var readline = require('linebyline'),
      rl = readline('../latest.log');
async function task(argument) {

  const calltasks = await db.getNewCollection('calltasks');
  let count = 0;
  let binds = [];
  rl.on('line', async function(line, lineCount, byteCount) {
    //console.dir(line)
    if(line.indexOf('2018122715284246afbc72e628') >= 0){
      console.dir(line)
    }

    if(line.indexOf('2018/12/27') >= 0 && line.indexOf('result') >= 0) {
      console.log("||||||||||||||" + count ++ );
      let json = JSON.parse(line.substr(20,line.length).trim());
      console.dir({
         bindID: json.bindID,
            caller: json.caller,
            duration: Number(json.duration || 0),
            url:json.record,
            result: json.record,
            minutes: Math.ceil(Number(json.duration || 0) / 60),
            cron: false
        });
/*     await calltasks.update({
        bindID: json.bindID
      }, {
        $set: {
            caller: json.caller,
            duration: Number(json.duration || 0),
            url:json.record,
            result: json.record,
            minutes: Math.ceil(Number(json.duration || 0) / 60),
            cron: false
        }
      })*/
    }

  })
  .on('error', function(e) {
    // something went wrong
  });

}

function ISODateRange(date) {
    const startAt = moment(date).format(`YYYY-MM-DD 00:00:00`)
        , endAt = moment(date).add(1, 'd').format(`YYYY-MM-DD 00:00:00`)
    return [new Date(startAt), new Date(endAt)];
}

task();