From ac5a2a23c5f5c13815c3a603d908e54ef8360669 Mon Sep 17 00:00:00 2001 From: Abel Date: Wed, 13 Mar 2024 02:24:10 +0100 Subject: [PATCH] Added call to the question store service API for storing the generated questions (pending for testing) --- docker-compose.yml | 2 + .../package-lock.json | 93 +++++++++++++++++++ .../questiongeneratorservice/package.json | 1 + .../questiongenerator-service.js | 47 +++++----- 4 files changed, 122 insertions(+), 21 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 0fd03c84..a0a48ea2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -57,12 +57,14 @@ services: build: ./questionsservice/questiongeneratorservice depends_on: - mongodb_wiki + - storequestionservice ports: - "8007:8006" networks: - mynetwork environment: MONGODB_URI: mongodb://mongodb_wiki:27017/questions + STORE_QUESTION_SERVICE_URL: http://storequestionservice:8004 gatewayservice: container_name: gatewayservice-${teamname:-defaultASW} diff --git a/questionsservice/questiongeneratorservice/package-lock.json b/questionsservice/questiongeneratorservice/package-lock.json index 3dbf397a..2240421b 100644 --- a/questionsservice/questiongeneratorservice/package-lock.json +++ b/questionsservice/questiongeneratorservice/package-lock.json @@ -9,6 +9,8 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "axios": "^1.6.5", + "cors": "^2.8.5", "express": "^4.18.2", "mongoose": "^8.2.0" }, @@ -73,6 +75,21 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==" }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, + "node_modules/axios": { + "version": "1.6.7", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.7.tgz", + "integrity": "sha512-/hDJGff6/c7u0hDkvkGxR/oy6CbCs8ziCsC7SqmhjfozqiJGc8Z11wrv9z9lYfY4K8l+H9TpjcMDX0xOZmx+RA==", + "dependencies": { + "follow-redirects": "^1.15.4", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -191,6 +208,17 @@ "fsevents": "~2.3.2" } }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -229,6 +257,18 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" }, + "node_modules/cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -253,6 +293,14 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/depd": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", @@ -385,6 +433,38 @@ "node": ">= 0.8" } }, + "node_modules/follow-redirects": { + "version": "1.15.5", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz", + "integrity": "sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/forwarded": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", @@ -900,6 +980,14 @@ "node": ">=0.10.0" } }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/object-inspect": { "version": "1.13.1", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.1.tgz", @@ -956,6 +1044,11 @@ "node": ">= 0.10" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/pstree.remy": { "version": "1.1.8", "resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz", diff --git a/questionsservice/questiongeneratorservice/package.json b/questionsservice/questiongeneratorservice/package.json index a55addc6..026b5970 100644 --- a/questionsservice/questiongeneratorservice/package.json +++ b/questionsservice/questiongeneratorservice/package.json @@ -20,6 +20,7 @@ "homepage": "https://github.com/arquisoft/wiq_es6c#readme", "dependencies": { "express": "^4.18.2", + "axios": "^1.6.5", "cors": "^2.8.5", "mongoose": "^8.2.0" }, diff --git a/questionsservice/questiongeneratorservice/questiongenerator-service.js b/questionsservice/questiongeneratorservice/questiongenerator-service.js index 88aec913..505a7a99 100644 --- a/questionsservice/questiongeneratorservice/questiongenerator-service.js +++ b/questionsservice/questiongeneratorservice/questiongenerator-service.js @@ -1,5 +1,6 @@ const express = require('express'); const cors = require('cors'); +const axios = require('axios'); const mongoose = require('mongoose'); const { Question } = require('./questiongenerator-model') const { Request } = require('./questiongenerator-model') @@ -7,7 +8,9 @@ const { Request } = require('./questiongenerator-model') const app = express(); const port = 8006; -const WikiQueries = require('./wikidataExtractor/wikidataQueries') +const questionHistoryServiceUrl = process.env.QUESTION_HISTORY_SERVICE_URL || 'http://localhost:8004'; + +const WikiQueries = require('./wikidataExtractor/wikidataQueries'); // Connect to MongoDB const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/questions'; mongoose.connect(mongoUri); @@ -21,9 +24,9 @@ app.use(express.json()); // Middleware to enable CORS (cross-origin resource sharing). In order for the API to be accessible by other origins (domains). app.use(cors()); -var mockedQuestions = [] -var isWikiChecked = false -var elementos +var mockedQuestions = []; +var isWikiChecked = false; +var elementos; function shuffle(array) { let currentIndex = array.length; @@ -31,12 +34,12 @@ function shuffle(array) { // Mientras queden elementos para mezclar. while (currentIndex > 0) { - // Escoge un elemento aleatorio. - randomIndex = Math.floor(Math.random() * currentIndex); - currentIndex--; + // Escoge un elemento aleatorio. + randomIndex = Math.floor(Math.random() * currentIndex); + currentIndex--; - // Intercambia el elemento actual con el elemento aleatorio. - [array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]]; + // Intercambia el elemento actual con el elemento aleatorio. + [array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]]; } return array; @@ -44,17 +47,17 @@ function shuffle(array) { const generateQuestion = async () => { if (!isWikiChecked) { - elementos = await WikiQueries.obtenerPaisYCapital() - isWikiChecked = true + elementos = await WikiQueries.obtenerPaisYCapital(); + isWikiChecked = true; } - elementos = shuffle(elementos) - console.log("caca", elementos) + elementos = shuffle(elementos); + console.log("caca", elementos); mockedQuestions = [{ pregunta: "¿Cual es la capital de " + elementos[0].countryLabel + "?", respuesta_correcta: elementos[0].capitalLabel, respuestas_incorrectas: [elementos[1].capitalLabel, elementos[2].capitalLabel, elementos[3].capitalLabel] - }] - console.log(mockedQuestions) + }]; + console.log(mockedQuestions); } @@ -129,11 +132,11 @@ async function getQuestions(req) { // return mockedQuestions.slice(0, 4); // } // const response = []; - await generateQuestion() - // for (let i = 0; i < preguntas; i++) { - // response.push(mockedQuestions[i % 11]); - // } - return mockedQuestions + await generateQuestion(); + // for (let i = 0; i < preguntas; i++) { + // response.push(mockedQuestions[i % 11]); + // } + return mockedQuestions; } // Route for getting questions @@ -141,7 +144,9 @@ app.get('/questions', async (req, res) => { try { // TODO: Implement logic to fetch questions from MongoDB and send response // const questions = await Question.find() - const defaultQuestion = await getQuestions(req) + const defaultQuestion = await getQuestions(req); + + const questionsHistoryResponse = await axios.post(questionHistoryServiceUrl + '/history/questions', defaultQuestion); res.json(defaultQuestion); } catch (error) { // res.status(500).json({ message: error.message })