Skip to content

Commit

Permalink
Merge branch 'develop' into newstatsservice
Browse files Browse the repository at this point in the history
  • Loading branch information
CANCI0 authored Mar 18, 2024
2 parents b9cba48 + 22316ca commit e27117d
Show file tree
Hide file tree
Showing 25 changed files with 568 additions and 491 deletions.
15 changes: 15 additions & 0 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ app.get("/questions", async (req, res) => {
}
});

app.post("/questions", async (req, res) => {
try {
// Forward the question request to the question service
const questionResponse = await axios.post(
questionServiceUrl + "/questions",
{ body: req.body }
);
res.json(questionResponse.data);
} catch (error) {
res
.status(error.response.status)
.json({ error: error.response.data.error });
}
});

app.get("/stats", async (req, res) => {
try {
// Forward the stats request to the stats service
Expand Down
51 changes: 40 additions & 11 deletions questionservice/question-service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// user-service.js
const express = require("express");
const cors = require('cors');
const cors = require("cors");
const bodyParser = require("body-parser");
const cron = require("node-cron");
const GeneratorChooser = require("./questionGen/GeneratorChooser");
Expand All @@ -14,20 +14,26 @@ const MAX_QUESTIONS = 10000;

// Middleware to parse JSON in request body
app.use(bodyParser.json());
// support encoded bodies
app.use(bodyParser.urlencoded({ extended: true }));

app.use(cors());

app.use((req, res, next) => {
if (!generadoresCargados) {
return res.status(500).json({ error: "Los generadores de preguntas aún no se han cargado. Por favor, inténtalo de nuevo más tarde." });
return res
.status(500)
.json({
error:
"Los generadores de preguntas aún no se han cargado. Por favor, inténtalo de nuevo más tarde.",
});
}
next();
});

app.set("json spaces", 40);

app.get("/questions", async (req, res) => {
console.log(req.query)
if (req.query.n > MAX_QUESTIONS) {
res
.status(400)
Expand All @@ -43,16 +49,39 @@ app.get("/questions", async (req, res) => {
}
});

app.post("/questions", async (req, res) => {
const { tematicas, n } = req.body.body;
if (!n || n > MAX_QUESTIONS) {
res
.status(400)
.json({ error: `El límite de preguntas son ${MAX_QUESTIONS}` });
return;
}
try {
const temas = tematicas ? JSON.parse(tematicas) : [];
const tematicasValidas =
temas.length !== 0
? temas
: ["paises", "literatura", "cine", "arte", "programacion"];
const cantidadPreguntas = parseInt(n, 10);
const data = gen.getQuestionsPost(tematicasValidas, cantidadPreguntas);
res.json(data);
} catch (error) {
res.status(400).json({ error: error.message });
}
});

const server = app.listen(port, async () => {
console.log(`Question Service listening at http://localhost:${port}`);
gen.loadGenerators()
.then(() => {
console.log("Generators loaded successfully!");
generadoresCargados = true;
})
.catch((error) => {
console.error("Error al cargar los generadores de preguntas:", error);
});
gen
.loadGenerators()
.then(() => {
console.log("Generators loaded successfully!");
generadoresCargados = true;
})
.catch((error) => {
console.error("Error al cargar los generadores de preguntas:", error);
});
});

cron.schedule("0 3 * * *", async () => {
Expand Down
103 changes: 60 additions & 43 deletions questionservice/questionGen/GeneratorChooser.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,70 @@
const GenericGenerator = require('./GenericGenerator')
const fs = require('fs');

class GeneratorChooser{
constructor(){
this.generators = new Map();
this.tematicas = [];
this.leer_json("./data/tematicas.json");
}
const GenericGenerator = require("./GenericGenerator");
const fs = require("fs");

class GeneratorChooser {
constructor() {
this.generators = new Map();
this.tematicas = [];
this.leer_json("./data/tematicas.json");
}

leer_json(ruta){
const datos = fs.readFileSync(ruta);
var tematicas = JSON.parse(datos);

for(let i in tematicas){
var tematica = tematicas[i];
this.tematicas.push(i);
this.generators.set(i,
new GenericGenerator(tematica.entity, tematica.props, tematica.types, tematica.preguntas)
);
}
leer_json(ruta) {
const datos = fs.readFileSync(ruta);
var tematicas = JSON.parse(datos);

for (let i in tematicas) {
var tematica = tematicas[i];
this.tematicas.push(i);
this.generators.set(
i,
new GenericGenerator(
tematica.entity,
tematica.props,
tematica.types,
tematica.preguntas
)
);
}
}

getQuestions(tematica, n){
if(tematica === "all"){
var questions = [];
for(let i = 0 ; i < n ; i++){
let rand = Math.floor(Math.random() * this.tematicas.length)
let randTematica =this.tematicas[rand];
let q = this.generators.get(randTematica).generateRandomQuestions(1);
questions.push(q);
}
return questions.flat();
}else{
return this.generators.get(tematica).generateRandomQuestions(n);
}
getQuestions(tematica, n) {
if (tematica === "all") {
var questions = [];
for (let i = 0; i < n; i++) {
let rand = Math.floor(Math.random() * this.tematicas.length);
let randTematica = this.tematicas[rand];
let q = this.generators.get(randTematica).generateRandomQuestions(1);
questions.push(q);
}
return questions.flat();
} else {
return this.generators.get(tematica).generateRandomQuestions(n);
}
}

async loadGenerators(){
for(let i = 0 ; i < this.tematicas.length ; i++){
var gen = this.generators.get(this.tematicas[i]);
console.log("Cargando temática: " + this.tematicas[i]);
await gen.getData();
await this.#sleep(10000);
}
getQuestionsPost(tematicas, n) {
var questions = [];
for (let i = 0; i < n; i++) {
let rand = Math.floor(Math.random() * tematicas.length);
let randTematica = tematicas[rand];
let q = this.generators.get(randTematica).generateRandomQuestions(1);
questions.push(q);
}
return questions.flat();
}

#sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
async loadGenerators() {
for (let i = 0; i < this.tematicas.length; i++) {
var gen = this.generators.get(this.tematicas[i]);
console.log("Cargando temática: " + this.tematicas[i]);
await gen.getData();
await this.#sleep(10000);
}
}

#sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
}

module.exports = GeneratorChooser;
module.exports = GeneratorChooser;
9 changes: 9 additions & 0 deletions webapp/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,12 @@ a:hover {
-webkit-box-shadow: 0 0 0 0 #000, 0 0 0 2px var(--shadow);
box-shadow: 0 0 0 0 #000, 0 0 0 2px var(--shadow);
}

.logo {
font-family: oswald, sans-serif;
width: 100%;
font-size: 3rem;
font-weight: 700;
margin: 0;
text-shadow: 4px 4px 0 var(--shadow);
}
2 changes: 2 additions & 0 deletions webapp/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "./App.css";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import { ProtectedRoute } from "./routers/ProtectedRoute.js";
import Sobre from "./pages/Sobre/Sobre.js";
import Config from "./pages/Config/Config.js";

function App() {
return (
Expand All @@ -25,6 +26,7 @@ function App() {
<Route path="/home/clasico" element={<Clasico />} />
<Route path="/home/bateria" element={<Bateria />} />
<Route path="/stats" element={<Stats />} />
<Route path="/config" element={<Config />} />
</Route>

{/* Ruta por defecto */}
Expand Down
4 changes: 4 additions & 0 deletions webapp/src/components/Footer/Footer.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.footer{
border-top: 3px solid var(--borders);
width: 100%;
}
2 changes: 1 addition & 1 deletion webapp/src/components/Footer/Footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import './Footer.css';

const Footer = () => {
return(
<footer>
<footer className="footer">
<h2>WIQ!</h2>
<p>Copyright 2024 ® Grupo 1A de Arquitectura del Software</p>
</footer>
Expand Down
6 changes: 6 additions & 0 deletions webapp/src/components/Login/Login.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
.main .logo{
margin-top: 3rem;
}

.login-container {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
justify-content: center;
margin: auto 0;
}

.login-container h1{
Expand Down
Loading

0 comments on commit e27117d

Please sign in to comment.