From e0624cfffe0a7304cce11519207a5d909c6461e2 Mon Sep 17 00:00:00 2001 From: Abel Date: Mon, 29 Apr 2024 02:24:51 +0200 Subject: [PATCH] Trying to clean my code --- .../questiongenerator.js | 23 ++++----- .../wikidataextractor-service.js | 48 ++----------------- 2 files changed, 13 insertions(+), 58 deletions(-) diff --git a/questionsservice/questiongeneratorservice/questiongenerator.js b/questionsservice/questiongeneratorservice/questiongenerator.js index 3de28955..73b4ce1c 100644 --- a/questionsservice/questiongeneratorservice/questiongenerator.js +++ b/questionsservice/questiongeneratorservice/questiongenerator.js @@ -141,8 +141,6 @@ class QuestionGenerator { static async generateQuestionNonDuplicatedAnswers(plantilla, respuestas) { - // console.log("\nPlantilla:"); - // console.log(plantilla); const randomAnswer = await plantilla.modelo.aggregate([ { $match: plantilla.filtro }, { $sample: { size: 1 } } @@ -151,7 +149,7 @@ class QuestionGenerator { console.error(`Not enought data found to generate a question`); throw new Error(`Not enought data found to generate a question`); } - var randomDecoys = []; + let randomDecoys = []; if (respuestas > 1){ randomDecoys = await plantilla.modelo.aggregate([ { $match: plantilla.filtro_decoys(randomAnswer[0]) }, @@ -162,9 +160,8 @@ class QuestionGenerator { console.error(`Not enought data found to generate a question`); throw new Error(`Not enought data found to generate a question`); } - // console.log("\nFind:"); - // console.log(randomDecoys); - var retQuestion = { + + const retQuestion = { pregunta: plantilla.pregunta(randomAnswer[0][plantilla.campo_pregunta]), respuesta_correcta: randomAnswer[0][plantilla.campo_respuesta], respuestas_incorrectas: Array.from({ length: respuestas-1 }, (_, i) => randomDecoys[i][plantilla.campo_respuesta]) @@ -173,8 +170,7 @@ class QuestionGenerator { } static async generateQuestion1to1Relation(plantilla, respuestas) { - // console.log("\nPlantilla:"); - // console.log(plantilla); + const randomDocs = await plantilla.modelo.aggregate([ { $match: plantilla.filtro }, { $sample: { size: respuestas } } @@ -183,9 +179,8 @@ class QuestionGenerator { console.error(`Not enought data found to generate a question`); throw new Error(`Not enought data found to generate a question`); } - // console.log("\nFind:"); - // console.log(randomDocs); - var retQuestion = { + + const retQuestion = { pregunta: plantilla.pregunta(randomDocs[0][plantilla.campo_pregunta]), respuesta_correcta: randomDocs[0][plantilla.campo_respuesta], respuestas_incorrectas: Array.from({ length: respuestas-1 }, (_, i) => randomDocs[i+1][plantilla.campo_respuesta]) @@ -195,7 +190,7 @@ class QuestionGenerator { static async generateQuestions(preguntas, respuestas, temas) { const plantillasDisponibles = this.getAvailableTemplates(temas); - var retQuestions = []; + let retQuestions = []; for (let i = 0; i < preguntas; i++) { let index = Math.floor(Math.random() * plantillasDisponibles.length); let plantilla = this.plantillas[plantillasDisponibles[index]]; @@ -210,7 +205,7 @@ class QuestionGenerator { if (temas.length == 0) { return Array.from({ length: this.plantillas.length }, (_, i) => i); } - var templates = []; + let templates = []; console.log("Temas a utilizar:") temas.forEach(tema => { if (this.temas.has(tema)) { @@ -218,7 +213,7 @@ class QuestionGenerator { console.log(`\t${tema}`); } else { - console.error(`\tThe topic \'${tema}\' is not currently defined`); + console.error(`\tThe topic '${tema}' is not currently defined`); } }); if (templates.length == 0) { diff --git a/questionsservice/wikidataExtractor/wikidataextractor-service.js b/questionsservice/wikidataExtractor/wikidataextractor-service.js index 2147ec08..1201b089 100644 --- a/questionsservice/wikidataExtractor/wikidataextractor-service.js +++ b/questionsservice/wikidataExtractor/wikidataextractor-service.js @@ -70,17 +70,16 @@ const templates = [ async function extractData(template) { console.log("Actualizando los datos sobre:") - var data = await template.extractMethod(); + const data = await template.extractMethod(); console.log(data); - var transactions = data.map(function (element) { - var transaction = { + const transactions = data.map(function (element) { + let transaction = { updateOne: { filter: template.filtro(element), update: template.campo_actualizar(element), upsert: true } }; - // console.log(transaction); return transaction; }); await template.saveMethod(transactions); @@ -90,7 +89,7 @@ async function extractData(template) { const minutes = 30; const totalQueries = templates.length; -var query = 0; +let query = 0; cron.schedule(`*/${minutes} * * * *`, () => { try { console.log(`Running a task every ${minutes} minutes: ${Date()}`); @@ -102,45 +101,6 @@ cron.schedule(`*/${minutes} * * * *`, () => { }); - /* - ALL ROUTES ARE ONLY FOR DEVELOPING PURPOSES, THEY SHOULD GET DELETED IN PRODUCTION - THIS SERVICE SHOULD NOT BE ACCESSIBLE FROM OUTSIDE - */ - -// Route for extracting countries -// app.get('/extract', async (req, res) => { -// try { -// res.json(await extractData(templates[1])); -// } catch (error) { -// console.error(error.message); -// res.status(500).json({ message: error.message }) -// // res.status(500).json({ error: 'Internal Server Error' }); -// } -// }); - -// // // Route for geting countries -// app.get('/countries', async (req, res) => { -// try { -// const paises = await Pais.find({}) -// res.json(paises); -// } catch (error) { -// res.status(500).json({ message: error.message }) -// // res.status(500).json({ error: 'Internal Server Error' }); -// } -// }); - -// // // Route for deleting countries -// app.delete('/countries', async (req, res) => { -// try { -// const paises = await Pais.deleteMany({}) -// res.json(paises); -// } catch (error) { -// res.status(500).json({ message: error.message }) -// // res.status(500).json({ error: 'Internal Server Error' }); -// } -// }); - - app.use((err, req, res, next) => { console.error(`An error occurred: ${err}`); res.status(500).send(`An error occurred: ${err.message}`);