Home > Back-end >  Test multiple parsing outputs instead of hardcoding each of them into a constant Node js
Test multiple parsing outputs instead of hardcoding each of them into a constant Node js

Time:07-09

So in my application I am cron parsing multiple database data and I have it right now to where I create a const run for each data to find the dag_id and test the determineCron one at a time manually. How would I make an array so that the "determineCron()" at the bottom will test every run instead of me just manually coding determineCron(run), determineCron(run2), determineCron(run3), determineCron(run4) etc.

const run = {
    start_date: '2022-07-01T13:45:01.221636 00:00',
        end_date: '2022-07-01T14:11:01.864293 00:00',
    state: 'success',
    dag_run_id: 'scheduled__2022-06-30T13:45:00 00:00',
    dag_id: 'IPP_CYCLE_PARMS',
}

const run2 = {
    start_date: '2022-06-26T23:00:00.742495 00:00',
    end_date: '2022-06-27T14:10:23.108401 00:00',
    state: 'failed',
    dag_run_id: 'scheduled__2022-06-25T23:00:00 00:00',
    dag_id: 'EFS-Winning-Route-daily-batch'
}

async function determineCron(result){
    /*dagID = result?.[0]?.dag_id || 0*/
    dagID = result ? result.dag_id : 0
    console.log(dagID)
    job = await bqConnection().query(`SELECT * FROM \`np-inventory-planning-thd.IPP_SLA.expected_sla\` where dag_id = "${dagID}"`)
    console.log(job[0][0]);
    cronTime = job[0][0].cron_time
    var interval = parser.parseExpression(cronTime);
    console.log('Date: ', interval.next().toString());
}
determineCron(run)
determineCron(run2)

CodePudding user response:

const runs = [
{
    start_date: '2022-07-01T13:45:01.221636 00:00',
        end_date: '2022-07-01T14:11:01.864293 00:00',
    state: 'success',
    dag_run_id: 'scheduled__2022-06-30T13:45:00 00:00',
    dag_id: 'IPP_CYCLE_PARMS',
},
{
    start_date: '2022-06-26T23:00:00.742495 00:00',
    end_date: '2022-06-27T14:10:23.108401 00:00',
    state: 'failed',
    dag_run_id: 'scheduled__2022-06-25T23:00:00 00:00',
    dag_id: 'EFS-Winning-Route-daily-batch'
}
]
runs.forEach(determineCron)

CodePudding user response:

Put your runs into an array and then use array.forEach, like this:

interface CronRun {
  start_date: string;
  end_date: string;
  state: string;
  dag_run_id: string;
  dag_id: string;
}

const runs: CronRun[] = [
  {
    start_date: '2022-07-01T13:45:01.221636 00:00',
    end_date: '2022-07-01T14:11:01.864293 00:00',
    state: 'success',
    dag_run_id: 'scheduled__2022-06-30T13:45:00 00:00',
    dag_id: 'IPP_CYCLE_PARMS',
  },
  {
    start_date: '2022-06-26T23:00:00.742495 00:00',
    end_date: '2022-06-27T14:10:23.108401 00:00',
    state: 'failed',
    dag_run_id: 'scheduled__2022-06-25T23:00:00 00:00',
    dag_id: 'EFS-Winning-Route-daily-batch'
  }
];

async function determineCron(result: ChronRun){
    /*dagID = result?.[0]?.dag_id || 0*/
    dagID = result ? result.dag_id : 0
    console.log(dagID)
    job = await bqConnection().query(`SELECT * FROM \`np-inventory-planning-thd.IPP_SLA.expected_sla\` where dag_id = "${dagID}"`)
    console.log(job[0][0]);
    cronTime = job[0][0].cron_time
    var interval = parser.parseExpression(cronTime);
    console.log('Date: ', interval.next().toString());
}

runs.forEach(determineCron);

like your original code, this will run them all in parallel. If you want them in serial you'll have to do

runs.forEach(await determineCron);

instead.

CodePudding user response:

You could create an array with the references of the constants, but it's tricky after that, because if you use any built-in array method like map, forEach, reduce... to execute the function it will not await the previous finish to execute the next call. Therefore, what you should do is to use for and their respective variations..

// if you are in a async scope you can remove that self invoked function

(async function () {
  const runs = [
    {
      start_date: "2022-07-01T13:45:01.221636 00:00",
      end_date: "2022-07-01T14:11:01.864293 00:00",
      state: "success",
      dag_run_id: "scheduled__2022-06-30T13:45:00 00:00",
      dag_id: "IPP_CYCLE_PARMS",
    },
    {
      start_date: "2022-06-26T23:00:00.742495 00:00",
      end_date: "2022-06-27T14:10:23.108401 00:00",
      state: "failed",
      dag_run_id: "scheduled__2022-06-25T23:00:00 00:00",
      dag_id: "EFS-Winning-Route-daily-batch",
    },
  ];

  async function determineCron(result) {
    /*dagID = result?.[0]?.dag_id || 0*/
    dagID = result ? result.dag_id : 0;
    console.log(dagID);
    job = await bqConnection().query(
      `SELECT * FROM \`np-inventory-planning-thd.IPP_SLA.expected_sla\` where dag_id = "${dagID}"`
    );
    console.log(job[0][0]);
    cronTime = job[0][0].cron_time;
    var interval = parser.parseExpression(cronTime);
    console.log("Date: ", interval.next().toString());
  }

  for (let run of runs) {
    await determineCron(run);
  }
})();
  • Related