Skip to content

Commit

Permalink
Trying to clean my code
Browse files Browse the repository at this point in the history
  • Loading branch information
AbelMH1 committed Apr 29, 2024
1 parent 12b75bd commit e0624cf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 58 deletions.
23 changes: 9 additions & 14 deletions questionsservice/questiongeneratorservice/questiongenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }
Expand All @@ -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]) },
Expand All @@ -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])
Expand All @@ -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 } }
Expand All @@ -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])
Expand All @@ -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]];
Expand All @@ -210,15 +205,15 @@ 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)) {
templates = templates.concat(this.temas.get(tema));
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) {
Expand Down
48 changes: 4 additions & 44 deletions questionsservice/wikidataExtractor/wikidataextractor-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()}`);
Expand All @@ -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}`);
Expand Down

0 comments on commit e0624cf

Please sign in to comment.