optimzation

This commit is contained in:
2022-09-07 22:23:59 +02:00
parent cdd47327b1
commit b264e5830a
4 changed files with 53 additions and 45 deletions
+31 -31
View File
@@ -1,59 +1,59 @@
const {_android: android} = require('playwright');
const ExcelUtil = require("./src/excel/ExcelUtil");
const CommandorPage = require("./src/workers/CommandorPage");
const MongoManager = require("./src/workers/mongo_manager");
function delay(delayInms) {
return new Promise(resolve => {
setTimeout(() => {
resolve(2);
}, delayInms);
});
}
const {MongoManager, formatDate} = require("./src/workers/mongo_manager");
const mongoManager = new MongoManager();
let excelUtil = new ExcelUtil();
let contactList = excelUtil.readContacts();
mongoManager.connect().then(r => console.log("mongo connected"))
// Connect to the device.
async function needToBook(contact, mongoManager) {
let collectionName = formatDate(new Date())
let alreadBooked = await mongoManager.getAllSuccessfulItemsForDay(collectionName)
let toReturn = true;
await alreadBooked.forEach((bookedItem) => {
if (bookedItem.email === contact.mail) {
toReturn = false;
}
}
)
return toReturn
}
async function startBook(contactPojo, device) {
console.log(`Model: ${device.model()}`);
console.log(`Serial: ${device.serial()}`);
let commandor = new CommandorPage(contactPojo, device, mongoManager);
//read contacts form excel
return await commandor.loadPage();
if (await needToBook(contactPojo, mongoManager)) {
let commandor = new CommandorPage(contactPojo, device, mongoManager);
//read contacts form excel
return await commandor.loadPage();
} else {
console.log("do not send request --> skip")
}
}
mongoManager.connect().then(r => console.log("mongo connected"))
async function printFiles() {
let devices = await android.devices();
await contactList.reduce(async (promise, contactPojo) => {
async function startWithList(contacts, device) {
await contacts.reduce(async (promise, contactPojo) => {
// This line will wait for the last async function to finish.
// The first iteration uses an already resolved Promise
// so, it will immediately continue.
await promise;
const contents = await startBook(contactPojo, devices[0]);
const contents = await startBook(contactPojo, device);
console.log(contents);
}, Promise.resolve());
}
(async () => {
printFiles()
android.devices().then((devices) => {
let segmentNumber = contactList.length / devices.length;
for (let i = 0; i < devices.length; i++) {
startWithList(contactList.slice(i * segmentNumber, segmentNumber * (i + 1)), devices[i]);
}
})
})()
// android.devices().then((device_list) => {
// contactList.forEach((contactPojo) => {
// device_list.forEach((device) => {
// (async () => {
// await startBook(contactPojo, device)
// })()
// }
// )
// }
// )
// })