Skip to content

Commit

Permalink
Improvement by preloading data questions
Browse files Browse the repository at this point in the history
  • Loading branch information
UO287687 committed Feb 12, 2024
1 parent 07541a0 commit bcba025
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 23 deletions.
7 changes: 5 additions & 2 deletions question_generator/cities/citiesQuestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CitiesQuestions{
constructor(){
this.cities={};
}
async getRandomCities(numberOfCities){
async loadData(){
if (Object.keys(this.cities).length === 0) {//Se obtienen 100 ciudades relevantes
const query=`
SELECT ?city ?cityLabel ?population ?countryLabel ?elevation_above_sea_level
Expand Down Expand Up @@ -56,10 +56,13 @@ class CitiesQuestions{
};
}

this.cities[cityId].elevation_above_sea_level.push(elevationAboveSeaLevel);
this.cities[cityId].elevation_above_sea_level.push(parseFloat(elevationAboveSeaLevel));
});

}
}
async getRandomCities(numberOfCities){
await this.loadData();
const citiesArray = Object.values(this.cities);
const randomResults = citiesArray.sort(() => Math.random() - 0.5).slice(0, numberOfCities);
return randomResults
Expand Down
6 changes: 5 additions & 1 deletion question_generator/cities/citiesTemplates.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const cities=require('./citiesQuestions');
const citiesQuery=cities.getInstance();
function loadData(){
citiesQuery.loadData();
}
const templates=[
async ()=>
{
Expand Down Expand Up @@ -31,4 +34,5 @@ const templates=[


]
module.exports = () => templates[Math.floor(Math.random()*templates.length)]() //se obtiene una pregunta aleatoria de los templates
module.exports.getRandomQuestion = () => templates[Math.floor(Math.random()*templates.length)]();
module.exports.loadData = () =>loadData();
47 changes: 34 additions & 13 deletions question_generator/planets/planetsQuestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ class PlanetsQuestions{
}
return this.planetsQuestions;
}
async getBiggestPlanet(){
//Se obtiene el id del planeta, su nombre y su radio
const query= `
constructor(){
this.planets={}
}
async loadData(){
if(Object.keys(this.planets).length==0){
const query= `
SELECT ?planet ?planetLabel (SAMPLE(?radius) AS ?radius)
WHERE {
?categ wdt:P361 wd:Q337768.
Expand All @@ -25,24 +28,42 @@ class PlanetsQuestions{
GROUP BY ?planet ?planetLabel
`;
const results=await queryExecutor.execute(query)
//Escoge cuatro planetas aleatorios
const randomResults = results.sort(() => Math.random() - 0.5).slice(0,4);
const formattedResults = await randomResults.map(result => {
return {
item: result.planetLabel.value,
value:parseFloat(result.radius.value),
};
}).sort((a, b) => b.value - a.value);
results.forEach(planet => {
const planetId = planet.planet.value;
const planetName = planet.planetLabel.value;
const radius = planet.radius.value;

if (!this.planets[planetId]) {
this.planets[planetId] = {
planetId: planetId,
planetName: planetName,
radius: []
};
}

this.planets[planetId].radius.push(parseFloat(radius));
});
}
}
async getRandomPlanets(number) {
await this.loadData();
const array = Object.values(this.planets);
const randomResults = array.sort(() => Math.random() - 0.5).slice(0, number);
return randomResults
}
async getBiggestPlanet(){
const results=await this.getRandomPlanets(4);
const formattedResults = await results.sort((a, b) => b.radius[0] - a.radius[0]);
var finalResults={
correct: null,
incorrects:[]
}
for(let i = 0; i < Math.min(formattedResults.length,4); i++) {
if(i==0){
finalResults.correct=formattedResults[i].item;
finalResults.correct=formattedResults[i].planetName;
}
else{
finalResults.incorrects.push(formattedResults[i].item);
finalResults.incorrects.push(formattedResults[i].planetName);
}
}
return finalResults;
Expand Down
6 changes: 5 additions & 1 deletion question_generator/planets/planetsTemplates.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const planetsQuestions=require('./planetsQuestions');
const planetsQuery=planetsQuestions.getInstance();
function loadData(){
planetsQuery.loadData();
}
const templates=[
async ()=>
{
Expand All @@ -13,4 +16,5 @@ const templates=[


]
module.exports = () => templates[Math.floor(Math.random()*templates.length)]() //se obtiene una pregunta aleatoria de los templates
module.exports.getRandomQuestion = () => templates[Math.floor(Math.random()*templates.length)]();
module.exports.loadData = ()=>loadData();
9 changes: 6 additions & 3 deletions question_generator/questionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ const port = 8002;

app.use(bodyParser.json());


app.get('/api/questions/create', async (req, res) => {
res.status(200).json(await generalTemplate())
res.status(200).json(await generalTemplate.getRandomQuestion())
});
app.get('/api/questions/planets/create', async (req, res) => {
res.status(200).json(await planetTemplate())
res.status(200).json(await planetTemplate.getRandomQuestion())
});
app.get('/api/questions/cities/create', async (req, res) => {
res.status(200).json(await citiesTemplate())
res.status(200).json(await citiesTemplate.getRandomQuestion())
});

generalTemplate.loadData();

const server = app.listen(port, () => {
console.log(`Listening at http://localhost:${port}`);
});
11 changes: 8 additions & 3 deletions question_generator/questionTemplate.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const planetsTemplates=require('./planets/planetsTemplates');
const citiesTemplates=require('./cities/citiesTemplates')
function loadData(){
citiesTemplates.loadData()
planetsTemplates.loadData()
}
const templates=[
planetsTemplates,
citiesTemplates
planetsTemplates.getRandomQuestion,
citiesTemplates.getRandomQuestion

]
module.exports = () => templates[Math.floor(Math.random()*templates.length)]() //se obtiene una pregunta aleatoria de los templates
module.exports.getRandomQuestion = () => templates[Math.floor(Math.random()*templates.length)]();
module.exports.loadData = ()=>loadData();

0 comments on commit bcba025

Please sign in to comment.